GCJ 2010 - Fair Warning

Below is the description of a problem called 'Fair Warning', from 'Google Code Jam - 2010':

Problem
-------

On our planet, Jamcode IX, three Great Events occurred. They happened 26000, 11000 and 6000 slarboseconds ago. In 4000 slarboseconds, the amount of time since all of those events will be multiples of 5000 slarboseconds, the largest possible amount... and the apocalypse will come.

Luckily for you, you live on Jamcode X! The apocalypse came on Jamcode IX less than a year ago. But Jamcode X has a worrying prophecy: "After the moment of reckoning, on the first optimum anniversary of the N Great Events, the apocalypse will come. 64 bits will not save you. You have been warned."

The people of Jamcode X are very concerned by this prophecy. All of the Great Events have already happened, and their times have been measured to the nearest slarbosecond; but nobody knows when their optimum anniversary will occur. After studying the diary of a scientist from Jamcode IX, scientists working on the problem have come up with a theory:

The moment of reckoning is now, the moment you solve this problem. At some time y ≥ 0 slarboseconds from now, the number of slarboseconds since each of the Great Events will be divisible by some maximum number T. If you can find the smallest value of y that gives this largest possible T, that will give you the optimum anniversary when the apocalypse will come.

On Jamcode IX, for example, there were 3 Great Events and they happened 26000, 11000 and 6000 slarboseconds before the moment of reckoning. 4000 slarboseconds later, the amount of time since each event was a multiple of T=5000 slarboseconds, and the apocalypse came.

Your job is to compute the amount of time until the apocalypse comes. But remember the prophecy: even though the people of Jamcode X have been solving problems for two years, and 64-bit integers have always been enough, they might not always be enough now or in the future.

Input
-----

The first line of the input gives the number of test cases, C. C lines follow. Each starts with a single integer N, which is followed by a space and then N space-separated integers ti, the number of slarboseconds since Great Event i occurred.

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 minimum number of slarboseconds until ti + y is a multiple of the largest possible integer factor T for all i.

Limits
------

1 ≤ C ≤ 100.
ti ≠ tj for some i, j.

Small dataset
-------------

2 ≤ N ≤ 3.
1 ≤ ti ≤ 108.

Large dataset
-------------

2 ≤ N ≤ 1000.
1 ≤ ti ≤ 1050.

Sample Input
------------
3
3 26000000 11000000 6000000
3 1 10 11
2 800000000000000000001 900000000000000000001

Output
------

Case #1: 4000000
Case #2: 0
Case #3: 99999999999999999999


Below is my java solution:

GCJ 2010 - Snapper Chain

Below is the description of a problem called 'Snapper Chain', from the 'Google Code Jam - 2010':

Problem
-------

The Snapper is a clever little device that, on one side, plugs its input plug into an output socket, and, on the other side, exposes an output socket for plugging in a light or other device.

When a Snapper is in the ON state and is receiving power from its input plug, then the device connected to its output socket is receiving power as well. When you snap your fingers -- making a clicking sound -- any Snapper receiving power at the time of the snap toggles between the ON and OFF states.

In hopes of destroying the universe by means of a singularity, I have purchased N Snapper devices and chained them together by plugging the first one into a power socket, the second one into the first one, and so on. The light is plugged into the Nth Snapper.

Initially, all the Snappers are in the OFF state, so only the first one is receiving power from the socket, and the light is off. I snap my fingers once, which toggles the first Snapper into the ON state and gives power to the second one. I snap my fingers again, which toggles both Snappers and then promptly cuts power off from the second one, leaving it in the ON state, but with no power. I snap my fingers the third time, which toggles the first Snapper again and gives power to the second one. Now both Snappers are in the ON state, and if my light is plugged into the second Snapper it will be on.

I keep doing this for hours. Will the light be on or off after I have snapped my fingers K times? The light is on if and only if it's receiving power from the Snapper it's plugged into.

Input
-----

The first line of the input gives the number of test cases, T. T lines follow. Each one contains two integers, N and K.

Output
------

For each test case, output one line containing "Case #x: y", where x is the case number (starting from 1) and y is either "ON" or "OFF", indicating the state of the light bulb.

Limits
------

1 ≤ T ≤ 10,000.

Small dataset
-------------

1 ≤ N ≤ 10;
0 ≤ K ≤ 100;

Large dataset
-------------

1 ≤ N ≤ 30;
0 ≤ K ≤ 108;

Sample Input
------------
4
1 0
1 1
4 0
4 47

Sample Output
-------------
Case #1: OFF
Case #2: ON
Case #3: OFF
Case #4: ON


The description of the problem might be a bit confusing, but if you spend some time and clearly understand about what it really wants, then its really easy to code the solution for that. In my case, I wrote it down on paper and looked at the pattern of the snappers, for different values of n (number of snapper devices)....After looking at the different patterns, it was easy for me to figure out that the number of minimum snaps required to power the end device is equal to "(2 power n) - 1". Ok, this is just the minimum number of snaps! After the minimum number of snaps, it takes 1 snap to totally power off all the snappers...and it again takes "minimum number of snaps', to power the end device. So, after the 'minimum number of snaps', it takes (2 power n) to power the end device. Below is the solution, that I coded in Java:



The solution for this problem can be coded in different ways...In the 'Content Analysis' section of Code Jam site, there is another interesting solution of using XOR operation of bits.