Problem Statement
PBG is a shooter game for bears. The next round of this game will involve P polar bears (including Limak), B brown bears and G grizzly bears. We will use N to denote the total number of bears, that is, N = P + B + G.
The PBG game is played in rounds. In each round, a pair of bears is chosen uniformly at random. The two chosen bears fight each other. The loser of the fight is eliminated from the game, the winner remains.
The three bear species can be ordered by strength. Grizzly bears are the strongest, polar bears are in the middle, and brown bears are the weakest. A bear from a stronger species will always beat a bear of a weaker species in a fight. Whenever two bears of the same species fight, each of them has a 50 percent chance to win the fight.
The game ends when there is only one bear left. After the game, each bear is assigned a place: the winner's place is 1 and the other bears are on places 2 to N in reversed elimination order. (That is, the bear that lost the very last fight is in place 2, and the bear that got eliminated first is in place N.)
Limak is one of the polar bears in the game. Find the expected value of his place in the game. Express this value as a reduced fraction X/Y. Return the value X*Y^(-1) modulo 1000000007.
Definition
- Class:
- PBG
- Method:
- findEV
- Parameters:
- int, int, int
- Returns:
- int
- Method signature:
- int findEV(int P, int B, int G)
- (be sure your method is public)
Notes
- Given the constraints used in this problem, if Limak's expected place is a reduced fraction is X/Y, the number Y will never be divisible by (10^9 + 7) = 1,000,000,007.
- The notation Y^(-1) represents the inverse element to Y modulo 10^9 + 7. The previous note implies that this value always exists.
Constraints
- P will be between 1 and 2000, inclusive.
- B will be between 0 and 2000, inclusive.
- G will be between 0 and 2000, inclusive.
Examples
5
0
0
Returns: 3
There are five polar bears and each of them is equally likely to take any place from 1 to 5. The expected value of Limak's place is (1+2+3+4+5)/5 = 3.
1
1
1
Returns: 333333338
There is one bear of each species. There are three possibilities: In the first round Limak (a polar bear) meets a brown bear. Since polar bears are a stronger species, Limak wins. In the second round he will then lose to the grizzly bear. Limak's place: 2. In the first round Limak meets the grizzly bear and loses. Limak's place: 3. In the first round the other two bears fight. The grizzly wins. In the second round the grizzly defeats Limak as well. Limak's place: 2. Each of the three scenarios is equally likely, so the answer is (2 + 3 + 2) / 3 = 7 / 3. The correct return value is therefore (7 * 3^(-1)) mod (10^9 + 7) = (7 * 333,333,336) mod (10^9 + 7) = 333,333,338.
1
3
0
Returns: 1
There is one polar bear (Limak) and three brown bears. Limak is the strongest of the four bears, so he will always get first place.
2
3
4
Returns: 672193888
1
0
3
Returns: 333333339
1
0
0
Returns: 1
2000
2000
2000
Returns: 246923693
1
0
0
Returns: 1
1
0
1
Returns: 2
1
1
0
Returns: 1
1
1
1
Returns: 333333338
2
0
0
Returns: 500000005
2
0
1
Returns: 500000006
2
1
0
Returns: 666666673
2
1
1
Returns: 833333342
2
3
1
Returns: 945000010
2
2
5
Returns: 568452391
3
0
5
Returns: 971428584
4
3
0
Returns: 260000005
9
6
2
Returns: 391916956
8
2
3
Returns: 902631442
8
10
6
Returns: 592476738
5
5
0
Returns: 906651560
5
1
7
Returns: 816136378
11
1
1
Returns: 333333343
1
17
19
Returns: 221220387
56
20
32
Returns: 323654840
28
7
2
Returns: 854100311
39
94
35
Returns: 263758373
179
416
199
Returns: 65041808
68
321
261
Returns: 33090732
440
7
433
Returns: 303920351
1449
1026
753
Returns: 630620618
1355
896
1085
Returns: 810771595
580
1980
1631
Returns: 567693850
1
2000
0
Returns: 1
1
0
2000
Returns: 666668006
1
2000
2000
Returns: 577100435
2000
0
0
Returns: 500001004
2000
2000
0
Returns: 763051726
2000
0
2000
Returns: 236952282
2000
2000
2000
Returns: 246923693
1970
1974
1970
Returns: 86044590
1977
1958
1979
Returns: 714064297
1982
1987
1978
Returns: 710469079
1996
1984
1979
Returns: 271920889
1951
1992
1983
Returns: 882397518
1877
2000
1412
Returns: 101740193
17
1977
1761
Returns: 140597095
2000
39
1999
Returns: 674942010
1999
1998
3
Returns: 763322305
1998
2000
1999
Returns: 18742738
2000
2000
1999
Returns: 345358891
1999
2000
1998
Returns: 456420402