Problem Statement
Bear Limak has a fair coin. (Whenever he tosses the coin, it will land on each side with probability 50%.) Initially, both sides of the coin are empty.
Limak is going to play a game. At the beginning of the game, Limak's score is 0. The game will consist of K turns. Each turn will look as follows:
- Limak tosses the coin.
- If the visible side of the coin is empty, Limak chooses an arbitrary (possibly negative or extremely big) integer and writes it on that side of his coin.
- Limak adds the integer written on the visible side of the coin to his score.
Limak will win the game if his final score (after all K turns) is exactly S. Limak is smart and always follows a strategy that maximizes the probability of winning the game.
You are given the
Definition
- Class:
- BearEmptyCoin
- Method:
- winProbability
- Parameters:
- int, int
- Returns:
- long
- Method signature:
- long winProbability(int K, int S)
- (be sure your method is public)
Constraints
- K will be between 1 and 60, inclusive.
- S will be between -1,000,000,000 and 1,000,000,000, inclusive.
Examples
1
17
Returns: 2
After tossing a coin, Limak should write 17 on the visible side. His final score will be 17. The probability of winning is 1. You should return 1 * 2K = 2.
2
-50
Returns: 4
An optimal strategy looks as follows: Toss the coin for the first time. Write -25 on the visible side. Add the number -25 to your score. Your score is now -25. Toss the coin for the second time. There are now two possibilities: either the visible side is empty, or it contains the -25 we wrote on the coin in the first round. If the visible side is empty, write -25 on that side as well. In either case, add -25 to your score. Your score is now -50. When following this strategy you are guaranteed to win the game. Again, you should return 1 * 2K.
2
-49
Returns: 2
In this case you cannot guarantee to win the game. The best strategy will give you a 50% probability of winning. The return value should be 0.5 * 2K = 2. One optimal strategy looks as follows: After the first coin toss, write 80,000,000,000 (i.e. 8 * 1010) on the visible side. Then, there are two possibilities for the second coin toss. If you see 80,000,000,000 again, your final score is 160,000,000,000 and you lose. If you see the other (still empty) side of the coin, you can write -80,000,000,049 and win the game beucase your final score is -49. Obviously, there are other optimal strategies as well: you can write any integer x after the first coin toss, and then hope to see the other side on the coin and write (-49-x) there.
4
42
Returns: 8
4
-123456789
Returns: 6
18
123456
Returns: 49870
60
0
Returns: 1152921504606846976
20
-2845018
Returns: 187942
40
1
Returns: 138834194756
45
7
Returns: 4245049529812
46
-4
Returns: 8300338116572
48
1
Returns: 32333691386100
48
2
Returns: 32467381649194
48
3
Returns: 32444728845612
48
4
Returns: 32484044416434
48
536870912
Returns: 32484083199532
48
-12
Returns: 32497456808804
50
0
Returns: 1125899906842624
55
1331
Returns: 3941269902101972
55
-10240
Returns: 3943804317714000
56
1
Returns: 7688091247051004
56
14
Returns: 7697975326484038
57
584141
Returns: 15448986573412072
57
29317
Returns: 15448988483966872
57
3
Returns: 15486421747060896
57
-9
Returns: 15486421747060896
57
57
Returns: 144115188075855872
58
2149567
Returns: 30634274042643224
58
-1974706
Returns: 30256308000358456
58
-13805936
Returns: 30256308000358456
58
438190
Returns: 288230376151711744
59
990182101
Returns: 60851070403400216
60
950123071
Returns: 118346099267169764
60
25279
Returns: 118346099267169764
60
36902
Returns: 118660374228977932
60
2700003
Returns: 118809561469934696
60
680246
Returns: 118660374228977932
60
-122400444
Returns: 118978535620999900
60
73264952
Returns: 118938508471432060
60
-13451264
Returns: 118938508471432060
60
892020
Returns: 1152921504606846976
60
32271040
Returns: 118943684750808136
1
0
Returns: 2
1
-1
Returns: 2
1
1000000000
Returns: 2
1
36419354
Returns: 2
1
-15694274
Returns: 2
58
0
Returns: 288230376151711744
58
1
Returns: 28102293705839768
58
-1000000000
Returns: 30256308000358456
58
67002942
Returns: 30256308000358456
58
-647250060
Returns: 30256308000358456
59
0
Returns: 576460752303423488
59
-1
Returns: 60851070403400216
59
1000000000
Returns: 60851070403400216
59
934128330
Returns: 60851070403400216
59
-778734568
Returns: 60851070403400216
60
0
Returns: 1152921504606846976
60
1
Returns: 118346099267169764
60
-1000000000
Returns: 118943684750808136
60
718731665
Returns: 118516609160695776
60
-134586090
Returns: 120845846627388478
60
545465338
Returns: 118660374228977932
60
-44536346
Returns: 118660374228977932
60
1
Returns: 118346099267169764
60
2
Returns: 118660374228977932
60
3
Returns: 118809561469934696
60
17
Returns: 118346099267169764
60
15
Returns: 118831589107963608
60
-1
Returns: 118346099267169764