Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.59 KB | None | 0 0
  1. /** Team Proof
  2. Java template
  3. */
  4.  
  5. import java.io.*;
  6. import java.util.*;
  7. import java.math.*;
  8.  
  9. public class Main
  10. {
  11.     BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  12.     StringTokenizer tokenizer=null;
  13.    
  14.     public static void main(String[] args) throws IOException
  15.     {
  16.         new Main().execute();
  17.     }
  18.    
  19.     void debug(Object...os)
  20.     {
  21.         System.out.println(Arrays.deepToString(os));
  22.     }
  23.    
  24.     int ni() throws IOException
  25.     {
  26.         return Integer.parseInt(ns());
  27.     }
  28.    
  29.     long nl() throws IOException
  30.     {
  31.         return Long.parseLong(ns());
  32.     }
  33.    
  34.     double nd() throws IOException
  35.     {
  36.         return Double.parseDouble(ns());
  37.     }
  38.        
  39.     String ns() throws IOException
  40.     {
  41.         while (tokenizer == null || !tokenizer.hasMoreTokens())
  42.             tokenizer = new StringTokenizer(br.readLine());
  43.         return tokenizer.nextToken();
  44.     }
  45.    
  46.     String nline() throws IOException
  47.     {
  48.         tokenizer=null;
  49.         return br.readLine();
  50.     }
  51.        
  52.    
  53.  
  54.     //Main Code starts Here
  55.     int totalCases, testNum;   
  56.    
  57.  
  58.    
  59.     BigInteger g,x,y;
  60.     void extgcd(BigInteger a,BigInteger b)
  61.     {
  62.         if(b.equals(BigInteger.ZERO))
  63.         {
  64.             g=a;
  65.             x=BigInteger.ONE;
  66.             y=BigInteger.ZERO;
  67.         }
  68.         else
  69.         {
  70.             BigInteger div = a.divide(b);
  71.             extgcd(b,a.subtract(b.multiply(div)));
  72.             BigInteger temp=x;
  73.             x=y;
  74.             y=temp.subtract((div).multiply(y));
  75.         }
  76.     }
  77.    
  78.     BigInteger ans1,ans2;
  79.     void CRT(BigInteger m1,BigInteger m2)
  80.     {
  81.         extgcd(m1,m2);
  82.        
  83.         ans1=x.multiply(m1);
  84.         if(ans1.compareTo(BigInteger.ZERO) < 0)
  85.             ans1=ans1.add(pow10);
  86.         ans2=y.multiply(m2);
  87.         if(ans2.compareTo(BigInteger.ZERO) < 0)
  88.             ans2=ans2.add(pow10);
  89.  
  90.     }
  91.    
  92.  
  93.     void execute() throws IOException
  94.     {
  95.         preprocess();
  96.         totalCases = ni();
  97.         for(testNum = 1; testNum <= totalCases; testNum++)
  98.         {
  99.             if(!input())
  100.                 break;
  101.             solve();
  102.         }
  103.     }
  104.    
  105.     int N;
  106.     BigInteger two,five,ten;
  107.     BigInteger pow2,pow5,pow10;
  108.     String[] ans=new String[505];
  109.    
  110.    
  111.     void preprocess()
  112.     {
  113.         two=BigInteger.valueOf(2);
  114.         five=BigInteger.valueOf(5);
  115.         ten=BigInteger.valueOf(10);
  116.         pow2=two;
  117.         pow5=five;
  118.         pow10=ten;
  119.        
  120.         for(int i=2;i<=500;i++)
  121.         {
  122.             pow2=pow2.multiply(two);
  123.             pow5=pow5.multiply(five);
  124.             pow10=pow10.multiply(ten);
  125.             CRT(pow2,pow5);
  126.             if(ans1.compareTo(ans2) < 0)
  127.                 ans[i] = ans1.toString() + " " + ans2.toString();
  128.             else
  129.                 ans[i] = ans2.toString() + " " + ans1.toString();
  130.         }
  131.     }
  132.        
  133.  
  134.     void solve()
  135.     {
  136.         if(N==1)
  137.         {
  138.             System.out.println("Case #"+testNum+": 0 1 5 6");
  139.             return;
  140.         }
  141.        
  142.         System.out.print("Case #"+testNum+": ");
  143.         System.out.println(ans[N]);
  144.     }
  145.  
  146.     boolean input() throws IOException
  147.     {
  148.         N=ni();
  149.         return true;
  150.     }
  151.  
  152.  
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement