Problem Links:
Google Code Jam, Round1 A 2013, Problem AProblem:
Problem Bullseye
Maria has been hired by the Ghastly Chemicals Junkies (GCJ) company to help them manufacture bullseyes. A bullseye consists of a number of concentric rings (rings that are centered at the same point), and it usually represents an archery target. GCJ is interested in manufacturing black-and-white bullseyes.

Maria starts with t millilitres of black paint, which she will use to draw rings of thickness 1cm (one centimetre). A ring of thickness 1cm is the space between two concentric circles whose radii differ by 1cm.

Maria starts with t millilitres of black paint, which she will use to draw rings of thickness 1cm (one centimetre). A ring of thickness 1cm is the space between two concentric circles whose radii differ by 1cm.
Maria draws the first black ring around a white circle of radius r cm. Then she repeats the following process for as long as she has enough paint to do so:
- Maria imagines a white ring of thickness 1cm around the last black ring.
- Then she draws a new black ring of thickness 1cm around that white ring.
The area of a disk with radius 1cm is π cm2. One millilitre of paint is required to cover area π cm2. What is the maximum number of black rings that Maria can draw? Please note that:
- Maria only draws complete rings. If the remaining paint is not enough to draw a complete black ring, she stops painting immediately.
- There will always be enough paint to draw at least one black ring.
Input
The first line of the input gives the number of test cases, T. T test cases follow. Each test case consists of a line containing two space separated integers: r and t.
Output
For each test case, output one line containing "Case #x: y", where x is the case number (starting from 1) and y is the maximum number of black rings that Maria can draw.
Limits
Small dataset
1 ≤ T ≤ 1000.
1 ≤ r, t ≤ 1000.
1 ≤ r, t ≤ 1000.
Large dataset
1 ≤ T ≤ 6000.
1 ≤ r ≤ 1018.
1 ≤ t ≤ 2 × 1018.
1 ≤ r ≤ 1018.
1 ≤ t ≤ 2 × 1018.
Sample
Solution:
At first thought, I believed this was a math problem, so I can retrieve the fomular, then from the problem statement, we can see for the ith black ring we will paint will cost us, 2*r + 4*i - 3; So if we have x rings to paint, we will have Sum(2*r + 4*i - 3), i range from 1 to x, so the total will be 2x^2 + x*(2*r-1) <= t; So we got x <= (1-2*r + SQRT((2*r-1)^2 + 8*t))/2. So we only need to check the x, x-1, and x+1 these three possible answers. but the problem is if I use this approach, none of the primitive number could represent the value when number went to big. This approach might even not pass the first small-input test sets. So for the second thought, use BigInteger would be a great idea to solve this problem, while the problem is there is no function like Math.SQRT for BigInteger. Above so, we have the situation which we have to use BigInteger to represent our numbers, while we can't just use that fomular to solve our problem, this formula might work for other languages like c or c++, but definitely not for Java. So here we go, binary search is our helper. which could find our solution with O(lgt) time, where t is the input you have.Source Code:
/** * */ package com.googlecodejam.yr2013.round1A; import java.io.BufferedReader; import java.io.FileReader; import java.io.FileWriter; import java.io.PrintWriter; import java.math.BigInteger; /** * @author antonio081014 * @time Apr 26, 2013, 5:21:43 PM */ public class A { BigInteger two = new BigInteger("2"); public static void main(String[] args) throws Exception { A main = new A(); main.run(); System.exit(0); } private void run() throws Exception { BufferedReader in = new BufferedReader(new FileReader("input.in")); PrintWriter out = new PrintWriter(new FileWriter("output.txt")); int T = Integer.parseInt(in.readLine()); for (int t = 1; t <= T; t++) { out.write("Case #" + t + ": "); String[] lines = in.readLine().split("\\s"); long r = Long.parseLong(lines[0]); long amount = Long.parseLong(lines[1]); String ret = solve(r, amount); System.out.println(ret); out.write("" + ret + "\n"); } in.close(); out.close(); } private String solve(long r, long amount) { BigInteger rr = new BigInteger(Long.toString(r)); BigInteger tt = new BigInteger(Long.toString(amount)); BigInteger left = BigInteger.ZERO; BigInteger right = tt; while (left.compareTo(right) < 0) { BigInteger mid = (right.add(left).add(BigInteger.ONE)).divide(two); if (check(mid, rr, tt)) { left = mid; } else right = mid.subtract(BigInteger.ONE); } return left.toString(); } private boolean check(BigInteger x, BigInteger r, BigInteger amount) { // BigInteger two = new BigInteger("2"); return x.multiply( x.multiply(two).add( r.multiply(two).subtract(new BigInteger("1")))) .compareTo(amount) <= 0; // return x * (2 * x + 2 * r - 1) <= amount; } // private String solve3(BigInteger r, BigInteger amount) { // BigInteger ret =BigInteger.ONE.subtract(two.multiply(r)).add() // (1.0 - 2.0 * r + Math.sqrt((2.0 * r - 1) // * (2.0 * r - 1) + 8.0 * amount)) / 4; // if (check(ret, r, amount)) // return ret.toString(); // return ret.subtract(BigInteger.ONE).toString(); // } }