Advertisement
Madiyar

Untitled

Jan 5th, 2013
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.98 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. import java.math.*;
  4.  
  5. public class Main {
  6.     static BufferedReader in;
  7.     static StringTokenizer strtok;
  8.    
  9.     static String next() throws IOException {
  10.         while (strtok == null || !strtok.hasMoreTokens())
  11.             strtok = new StringTokenizer(in.readLine());
  12.         return strtok.nextToken();
  13.     }
  14.  
  15.     static int nextInt() throws IOException {
  16.         return Integer.parseInt(next());
  17.     }
  18.    
  19.     static long nextLong() throws IOException {
  20.         return Long.parseLong(next());
  21.     }
  22.    
  23.     static double nextDouble() throws IOException {
  24.         return Double.parseDouble(next());
  25.     }
  26.     static long a, b, c;
  27.  
  28.     static BigInteger sum2(long n) {
  29.         --n;
  30.         return BigInteger.valueOf(n).multiply(BigInteger.valueOf(n + 1)).multiply(BigInteger.valueOf(2 * n + 1)).divide(BigInteger.valueOf(6));
  31.     }
  32.  
  33.     static BigInteger sum(long n) {
  34.             return BigInteger.valueOf(n - 1).multiply(BigInteger.valueOf(n)).divide(BigInteger.valueOf(2));
  35.     }
  36.     static boolean Ok(long n) {
  37.         BigInteger va = BigInteger.valueOf(a);
  38.         BigInteger vb = BigInteger.valueOf(b);
  39.         BigInteger vc = BigInteger.valueOf(c);
  40.  
  41.         BigInteger vn = BigInteger.valueOf(n);
  42.         BigInteger A, B, C;
  43.         A = vn.multiply(va);
  44.         B = vn.multiply(vb).add(va.multiply(BigInteger.valueOf(2).multiply(sum(n))));
  45.         C = va.multiply(sum2(n)).add(vb.multiply(sum(n))).add(vn.multiply(vc));
  46.         return B.multiply(B).subtract(BigInteger.valueOf(4).multiply(A).multiply(C)).compareTo(BigInteger.ZERO) < 0;
  47.  
  48.     }
  49.  
  50.     public static void main(String args[]) throws IOException {
  51.         in = new BufferedReader(new InputStreamReader(System.in));
  52.         PrintWriter out = new PrintWriter(System.out);
  53.         int tests = nextInt();
  54.         for (int test = 1; test <= tests; ++test) {
  55.             a = nextLong();
  56.             b = nextLong();
  57.             c = nextLong();
  58.             long le = 0, ri = (long)1e18;
  59.             while (ri - le > 1) {
  60.                 long mid = (le + ri) / 2;
  61.                 if (Ok(mid)) ri = mid; else
  62.                 le = mid;
  63.             }
  64.             if (!Ok(ri)) out.println("Infinity"); else
  65.             out.println(ri);
  66.         }
  67.         out.close();   
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement