Skip to content

Commit 8a95258

Browse files
author
shiv
committed
found a better equation. smaller
1 parent 2713e06 commit 8a95258

File tree

4 files changed

+24
-24
lines changed

4 files changed

+24
-24
lines changed

.ip.txt.swp

4 KB
Binary file not shown.

.win_the_game.py.swp

12 KB
Binary file not shown.

ip.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
3
1+
5
2+
2 2
23
2 1
34
1 1
45
10 0
6+
12 45

win_the_game.py

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,30 @@
88
10 0
99
1010
'''
11-
import math
12-
11+
from math import factorial
12+
import sys
13+
sys.setrecursionlimit(50000)
1314
tn = int(input())
1415

1516
memo = {}
16-
def solve(player, rn, gn):
17-
n = rn+gn
18-
if (player, rn, gn) in memo:
19-
return memo[player, rn, gn]
20-
if n <= 1:
21-
return 0
22-
c1, c2 = 0, 0
23-
if player == 1:
24-
c1 = rn*(n-1)
25-
c2 = solve(2, rn, gn-1)
26-
elif player == 2:
27-
c1 = gn*(n-1)
28-
c2 = solve(1, rn-1, gn)
29-
memo[player, rn,gn] = c1+c2
30-
return c1+c2
3117

18+
def solve(r, g):
19+
if g ==0 and r>0 or r ==0 and g>0:
20+
return 1.0
21+
if g <0 or r<0:
22+
return 0.0
23+
n = r+g-1
24+
now = factorial(n)//(factorial(r-1))
25+
now //= factorial(g)
26+
return now + solve(r-1, g-2)
27+
3228
for i in range(tn):
29+
memo = {}
3330
r, g = list(map(int, input().split()))
34-
if r == 0 or g == 0:
35-
print(1.0)
36-
continue
37-
x = solve(1, r, g)
38-
print(round(x/math.factorial(r+g), 6))
39-
31+
x = solve(r, g)
32+
sl_sp = factorial(r+g)//factorial(r)
33+
sl_sp //= factorial(g)
34+
#print(x, sl_sp)
35+
print(x/sl_sp)
36+
#print(round(x/sl_sp, 6))
37+
print("-----------------------------")

0 commit comments

Comments
 (0)