Problem:


Score : 700 points

Problem Statement

Today, Snuke will eat B pieces of black chocolate and W pieces of white chocolate for an afternoon snack.

He will repeat the following procedure until there is no piece left:

Choose black or white with equal probability, and eat a piece of that color if it exists.

For each integer i from 1 to B+W (inclusive), find the probability that the color of the i-th piece to be eaten is black.
It can be shown that these probabilities are rational, and we ask you to print them modulo 10^9 + 7, as described in Notes.

Notes

When you print a rational number, first write it as a fraction \frac{y}{x}, where x, y are integers and x is not divisible by 10^9 + 7
(under the constraints of the problem, such representation is always possible).
Then, you need to print the only integer z between 0 and 10^9 + 6, inclusive, that satisfies xz \equiv y \pmod{10^9 + 7}.

Constraints

All values in input are integers.

1 \leq B,W \leq 10^{5}

Input

Input is given from Standard Input in the following format:

B W

Output

Print the answers in B+W lines. In the i-th line, print the probability that the color of the i-th piece to be eaten is black, modulo 10^{9}+7.

Sample Input 1

2 1

Sample Output 1

500000004
750000006
750000006

There are three possible orders in which Snuke eats the pieces:

white, black, black

black, white, black

black, black, white

with probabilities \frac{1}{2}, \frac{1}{4}, \frac{1}{4}, respectively. Thus, the probabilities of eating a black piece first, second and third are \frac{1}{2},\frac{3}{4} and \frac{3}{4}, respectively.

Sample Input 2

3 2

Sample Output 2

500000004
500000004
625000005
187500002
187500002

They are \frac{1}{2},\frac{1}{2},\frac{5}{8},\frac{11}{16} and \frac{11}{16}, respectively.

Sample Input 3

6 9

Sample Output 3

500000004
500000004
500000004
500000004
500000004
500000004
929687507
218750002
224609377
303710940
633300786
694091802
172485353
411682132
411682132



Sample input:
2 1


Sample output:
500000004
750000006
750000006

Submission status: Accepted

Solution code:
b,w=map(int,input().split())
n=b+w
mod=10**9+7
fra=[1]*(n+2)
inv=[1]*(n+2)
p2=[1]*(n+2)
t=1
for i in range(1,n+2):
    t*=i
    t%=mod
    fra[i]=t
t=pow(fra[n+1],mod-2,mod)
p=pow(pow(2,n+1,mod),mod-2,mod)
for i in range(n+1,0,-1):
    inv[i]=t
    t*=i
    t%=mod
    p2[i]=p
    p*=2
    p%=mod
def comb(n,r):
    return fra[n]*inv[n-r]*inv[r]%mod
nb=0
nw=0
for i in range(1,n+1):
    print(((1-nb-nw)*inv[2]+nw)%mod)
    if b<=i:
        nb+=comb(i-1,b-1)*p2[i]
        nb%=mod
    if w<=i:
        nw+=comb(i-1,w-1)*p2[i]
        nw%=mod

Submission status: Accepted

Solution code:
from fractions import Fraction

class Combination:
    '''計算量：階乗・逆元テーブルの作成O(N)
       nCkを求めるO(1)'''

    def __init__(self, n, MOD):
        self.fact = [1]
        for i in range(1, n + 1):
            self.fact.append(self.fact[-1] * i % MOD)
        self.inv_fact = [pow(self.fact[i] ,MOD - 2 ,MOD) for i in range(n + 1)]
        self.MOD = MOD

    def factorial(self, k):
        '''k!を求める'''
        return self.fact[k]

    def inverse_factorial(self, k):
        '''k!の逆元を求める'''
        return self.inv_fact[k]

    def combination(self, k, r):
        '''kCrを求める'''
        return (self.fact[k] * self.inv_fact[k - r] * self.inv_fact[r]) % self.MOD


b, w = map(int,input().split())
MOD = 10**9 + 7
comb = Combination(b+w, MOD)

#黒が(b+w-i)個残る確率 := pb
#白が(b+w-i)個残る確率 := pw
pb = [0]*(b+w+1)
pw = [0]*(b+w+1)

for i in range(b+w):
    if b+w-i > b:
        pb[b+w-i] = 0
        result_b = 0
    else:
        result_b = pb[b+w-i+1] + pow(pow(2, i, MOD), MOD-2, MOD) * comb.combination(i-1, w-1)
        result_b %= MOD
        pb[b+w-i] = result_b
        
    if b+w-i > w:
        pw[b+w-i] = 0
        result_w = 0
    else:
        result_w = pw[b+w-i+1] + pow(pow(2, i, MOD), MOD-2, MOD) * comb.combination(i-1, b-1)
        result_w %= MOD
        pw[b+w-i] = result_w
        
    ans = result_b + pow(2, MOD-2, MOD) * (1 - result_b - result_w)
    print(ans % MOD)

Submission status: Accepted

Solution code:
from math import factorial

B,W = map(int, input().split())
mod = 10**9 + 7

def comb(n,r):
  if n >= r:
    numer = factorial(n) % mod
    denom_inv = pow(((factorial(r) % mod)* (factorial(n-r) % mod)), mod-2, mod)
    return (numer * denom_inv) % mod
  else:
    return 0

ans = [0] * (B + W + 1)
ans_change = [0] * (B + W + 1)
change = (comb(B-1,0) * pow(2, W, mod)) % mod
two_inv = pow(2, mod-2, mod)
for i in range(W+1):
  if i < W:
    if i > 0:
      change = (change * (B+i-1) * pow(i, mod-2, mod) * two_inv) % mod
    change2 = (change * (B-1) * pow(B+i-1, mod-2, mod)) % mod
    ans_change[1] = (ans_change[1] + change2) % mod if B+i-1 > 0 else ans_change[1] + change
    ans_change[B+i] = (ans_change[B+i] + change - change2) % mod if B+i-1 > 0 else ans_change[B+i]
    ans_change[B+i+1] = (ans_change[B+i+1] - change) % mod
  else:
    change = (comb(W-1, 0) * pow(2, B, mod)) % mod
    for j in range(B):
      if j > 0:
        change = (change * (W+j-1) * pow(j, mod-2, mod) * two_inv) % mod
      change2 = (change * j * pow(W+j-1, mod-2, mod)) % mod
      ans_change[1] = (ans_change[1] + change2) % mod if W+j-1 > 0 else ans_change[1]
      ans_change[W+j] = (ans_change[W+j] - change2) % mod if W+j-1 > 0 else ans_change[W+j]
      ans_change[W+j+1] = (ans_change[W+j+1] + change) % mod
for i in range(B+W+1):
  ans[i] = (ans[i-1] + ans_change[i]) % mod
x = pow(2, B+W, mod)
for y in ans[1:]:
  print(int((pow(x, mod-2, mod) * y) % mod))

Submission status: Accepted

Solution code:
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
R = 10**9 + 7
DBG = True  and False
def ddprint(x):
  if DBG:
    print(x)

# extended gcd and modinv
# x = y*(x/y) + x%y
#   ->  g = ax+by = a(y*(x/y) + x%y) + by
#         = (a*(x/y) + b)*y + a*(x%y)
#
# while  g=ax+by, b==0  ->  g=x, a=1

def exgcd(x,y):
    if y == 0:
        return (x,1,0)
    else:
        g,b,a = exgcd(y, x%y)
        return (g, a, b-(x//y)*a)

# return y s.t. x*y mod p == 1
def modinv(x,p):
    g,a,b = exgcd(x,p)  # ax+bp = g, g=1 if mutually prime
    return a%p

def pw(x,e):
    if e==0:
        return 1
    elif e%2==0:
        return (pw(x,e//2)**2)%R
    else:
        return (x * pw(x,e//2)**2)%R

def rmod(i,qb,qw):
    global two2i, itwo2i
    numer = (two2i[i] - qb + qw)
    iden = itwo2i[i+1]
    ddprint("num {} idn {}".format(numer, iden))
    return (numer*iden)%R

b,w = inm()
ddprint("b {} w {}".format(b,w))

inv2 = modinv(2,R)
fact = [1] * (b+w+2)
for i in range(2,b+w+1):
    fact[i] = (i*fact[i-1])%R
ifact = [1] * (b+w+2)
ifact[b+w] = modinv(fact[b+w],R)
for i in range(b+w-1,0,-1):
    ifact[i] = ((i+1)*ifact[i+1])%R
two2i = [1] * (b+w+2)
itwo2i = [1] * (b+w+2)
itwo2i[1] = inv2
for i in range(1,b+w+1):
    x = (2*two2i[i-1])
    two2i[i] = x if x < R else x-R
    itwo2i[i] = (inv2*itwo2i[i-1]) % R
ddprint(fact[0:5])
ddprint(ifact[0:5])

cb = cw = 1
qb = qw = 0
print(500000004)
for i in range(1,b+w):
    qb = 0 if i<b else 1 if i==b else (2*qb+cb) % R
    qw = 0 if i<w else 1 if i==w else (2*qw+cw) % R
    cb = 1 if i<b else (fact[i]*ifact[b-1]*ifact[i-b+1]) % R
    cw = 1 if i<w else (fact[i]*ifact[w-1]*ifact[i-w+1]) % R
    ddprint("i {} cb {} cw {} qb {} qw {}".format(i,cb,cw,qb,qw))
    print(rmod(i,qb,qw))

Submission status: Accepted

Solution code:
MOD = 10**9+7
fact = [1]
invfact = [1]
for i in range(1,2*10**5 + 100):
    fact.append((fact[i-1] * i) % MOD)
    invI = pow(i,MOD-2,MOD)
    invfact.append((invfact[i-1] * invI) % MOD)

B,W = map(int,input().split())
N = B+W

pcSum = [1 for _ in range(B+W+1)]
qcSum = [1 for _ in range(B+W+1)]
def modComb(n,m):
    if m < 0 or m > n: return(0)
    ans = fact[n] * invfact[m] 
    ans %= MOD
    ans *= invfact[n-m]
    ans %= MOD
    return(ans)

for i in range(B+W-1):
    pcSum[i+1] = 2*pcSum[i] - modComb(i,B-1)
    pcSum[i+1] %= MOD
    if (i+1-W) <= B-1:
        qcSum[i+1] = 2*qcSum[i] - modComb(i,B-1) - modComb(i,i+1-W)
        qcSum[i+1] %= MOD
    else:
        qcSum[i+1] = 0

p = []
q = []
powOfTwo = []
value = 1
invTwo = pow(2,MOD-2,MOD)
for _ in range(N):
    powOfTwo.append(value)
    value *= invTwo
    value %= MOD
for i in range(B+W):
    # coefficient = pow(2,i*(MOD-2),MOD)
    coefficient = powOfTwo[i]
    pvalue = pcSum[i] * coefficient
    pvalue %= MOD
    p.append(pvalue)
    qvalue = qcSum[i] * coefficient
    qvalue %= MOD
    q.append(qvalue)
for i in range(N):
    answer = p[i] - invTwo * q[i]
    answer %= MOD
    print(answer)

Submission status: Accepted

Solution code:
B, W = map(int, input().split())

MOD = 10 ** 9 + 7
M = 200005
fact = [0] * M
fact[0] = 1
for i in range(1, M):
    fact[i] = fact[i - 1] * i
    fact[i] %= MOD

rfact = [0] * M
rfact[M - 1] = pow(fact[M - 1], MOD - 2, MOD)
for i in range(M - 2, -1, -1):
    rfact[i] = rfact[i + 1] * (i + 1)
    rfact[i] %= MOD

pow2 = [0] * M
pow2[0] = 1
for i in range(1, M):
    pow2[i] = pow2[i - 1] * 2
    pow2[i] %= MOD


def comb(n, k):
    if k < 0 or k > n:
        return 0
    return fact[n] * rfact[n - k] * rfact[k] % MOD


num = [0] * 200005
f = [0] * 200005
for i in range(W):
    q = pow(pow2[i + B], MOD - 2, MOD) * comb(i + B - 1, B - 1) % MOD
    f[i + B] = q

g = [0] * 200005
for i in range(B):
    q = pow(pow2[i + W], MOD - 2, MOD) * comb(i + W - 1, W - 1) % MOD
    g[i + W] = q
    num[i + W + 1] = q


inv2 = pow(2, MOD - 2, MOD)
pB, pW = 0, 0
for i in range(1, B + W + 1):
    d = 1 - (pB + pW) + 2 * MOD
    d %= MOD
    pB += f[i]
    pB %= MOD
    pW += g[i]
    pW %= MOD

    num[i] += num[i - 1]
    num[i] %= MOD
    print((inv2 * d + num[i]) % MOD)

Submission status: Accepted

Solution code:
B, W = list(map(int, input().split()))

MOD = 10**9+7

facts = [1,1] #階乗
invs = [1,1] #整数の逆元
invfacts = [1,1] #階乗の逆元
for ind in range(1, B+W):
    i = ind+1
    facts.append(facts[-1]*i % MOD)
    invs.append( -(MOD//i)*invs[MOD%i] % MOD )
    invfacts.append( invfacts[-1]*invs[i] % MOD )

def Comb(n, r, MOD=MOD):
    if n < r:
        return 0
    return facts[n]*invfacts[n-r]*invfacts[r] % MOD

y_pi = 0 #i回目までにBを食べ尽くした確率piの分子
y_qi = 0 #i回目までにWを食べ尽くした確率qiの分子
pow2 = 1
invxi = 1
inv2 = invs[2]
for i in range(B+W):
    #y_pi = 0 if i < B else 2*y_pi + ((facts[i-1]*invfacts[i-B] % MOD)*invfacts[B-1] % MOD)
    y_pi = (2*y_pi + Comb(i-1, B-1))%MOD
    #y_qi = 0 if i < W else 2*y_qi + ((facts[i-1]*invfacts[i-W] % MOD)*invfacts[W-1] % MOD)
    y_qi = (2*y_qi + Comb(i-1, W-1))%MOD
    invxi = invxi * inv2 % MOD
    yi = (pow2-y_pi+y_qi) % MOD
    z = yi*invxi % MOD
    print(z)
    pow2 = pow2*2 %MOD

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys

MOD = 1000000007  # type: int

def get_z(y, x): # y/x (mod 1000000007)
    inv_x = pow(x, MOD - 2, MOD)
    return y * inv_x % MOD

def _get_z(y, x): # y / x
    #inv_x = pow(x, MOD - 2, MOD)
    #return y * inv_x % MOD
    while y % x > 0:
        y += MOD
    return y // x

def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)

def reduce(y, x):
    tmp = gcd(x, y)
    return (y // tmp, x // tmp)

def make_comb(n, k):
    fact = [1] * (n + 1)
    for i in range(2, n + 1):
        fact[i] = (fact[i - 1] * i) % MOD

    ret = [0] * (n + 1)
    for i in range(k, n + 1):
        a = fact[i]
        b = (fact[k] * fact[i - k]) % MOD
        ret[i] = get_z(a, b)
    return ret

def _make_comb(n, k):
    ret = [0] * n
    ret[k] = 1
    for i in range(k + 1, n):
        ret[i] = (ret[i - 1] * i) // (i - k)
        ret[i] %= MOD
    return ret

def solve(b: int, w: int):
    n = b + w
    bb = 0
    ww = 0
    b_comb = make_comb(n, b - 1)
    w_comb = make_comb(n, w - 1)
    par = 1
    for i in range(1, n + 1):
        #print(' ======== i: %d ==============' % i)
        other = par - bb - ww
        x = (par * 2) % MOD
        y = (other + bb * 2) % MOD
        #x = (par * 2)
        #y = other + bb * 2
        #print("bb : %d, ww : %d, other : %d" % (bb, ww, other))
        #print(y, ' / ', x)
        #y, x = reduce(y, x)
        ret = get_z(y, x)
        print(ret)
        #bb = (bb * 2 + w_comb[i - 1])
        #ww = (ww * 2 + b_comb[i - 1])
        #par = (par * 2)
        bb = (bb * 2 + w_comb[i - 1]) % MOD
        ww = (ww * 2 + b_comb[i - 1]) % MOD
        par = (par * 2) % MOD
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    B = int(next(tokens))  # type: int
    W = int(next(tokens))  # type: int
    solve(B, W)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
B,W = map(int, input().split())
N = B+W
mod = 10**9+7

factorial = [1]
for i in range(N+1):
    factorial.append( factorial[-1] * (i+1) % mod)
# print(factorial)



de_factorial = [0] * (N+1)
de_factorial[N] = pow(  factorial[N] , mod-2, mod  )

for i in range( N, 0, -1):
    de_factorial[i-1] = de_factorial[i] * i % mod
    
def comb(n,r):
    if n < r: return 0
    if n == r: return 1
    return (factorial[n] * de_factorial[r] * de_factorial[n-r]) % mod


p = [0] * (N+1) # Bを食べきった確率
q = [0] * (N+1) # Wを食べきった確率

de2 = de_factorial[2]
for i in range(1, N+1):
    p[i] = p[i-1] + comb(i-1 ,B-1) * de2 % mod 
    q[i] = q[i-1] + comb(i-1 ,W-1) * de2  % mod 
    de2 = de2 * de_factorial[2] % mod

    tmp = ( q[i-1] + 1-p[i-1] ) * de_factorial[2] % mod
    
    print(tmp)

Submission status: Accepted

Solution code:
b, w = [int(i) for i in input().split()]
p = 10 ** 9 + 7

def fact(n, p=10**9 + 7):
    f = [1]
    for i in range(1, n+1):
        f.append(f[-1]*i%p)
    return f

def invfact(n, f, p=10**9 + 7):
    inv = [pow(f[n], p-2, p)]
    for i in range(n, 0, -1):
        inv.append(inv[-1]*i%p)
    return inv[::-1]

def get_inv(n, p=10**9 + 7):
    inv = [0, 1]
    for i in range(2, n+1):
        inv.append(-(p//i * inv[p%i]) % p)
    return inv

f = fact(b + w + 1)
invf = invfact(b + w + 1, f)

def comb(a, b):
    if a < b:
        return 0
    return f[a] * invf[b] * invf[a-b] % p

P = [[0] * 2 for i in range(b + w + 1)]
B = 0
W = 1
inv_2 = invf[2]

for i in range(1, b + w + 1):
    P[i][B] = P[i - 1][B] + comb(i - 1, b - 1) * inv_2  #i番目になくなってる確率
    P[i][W] = P[i - 1][W] + comb(i - 1, w - 1) * inv_2
    inv_2 = inv_2 * invf[2] % p 
    print((1 - P[i - 1][B] + P[i - 1][W]) * invf[2] % p)

Submission status: Accepted

Solution code:
def prepare(n, MOD):
    f = 1
    factorials = [1]
    for m in range(1, n + 1):
        f *= m
        f %= MOD
        factorials.append(f)
    inv = pow(f, MOD - 2, MOD)
    invs = [1] * (n + 1)
    invs[n] = inv
    for m in range(n, 1, -1):
        inv *= m
        inv %= MOD
        invs[m - 1] = inv
    return factorials, invs


b, w = map(int, input().split())
MOD = 10 ** 9 + 7
I2 = 500000004  # 2^(-1) mod MOD
factorials, inverses = prepare(b + w, MOD)
buf = []
denom = I2
pi, qi = 0, 0
for i in range(b + w):
    if i >= b:
        pi *= 2
        pi += factorials[i - 1] * inverses[i - b] * inverses[b - 1]
        pi %= MOD
    if i >= w:
        qi *= 2
        qi += factorials[i - 1] * inverses[i - w] * inverses[w - 1]
        qi %= MOD
    buf.append((I2 - (pi - qi) * denom) % MOD)
    denom = denom * I2 % MOD
print('\n'.join(map(str, buf)))

Submission status: Accepted

Solution code:
B, W = map(int,input().split())
Mod = 10**9+7
magick = 500000004

inv_t = [0]+[1]
for i in range(2,B+W+1):
   inv_t += [inv_t[Mod % i] * (Mod - int(Mod / i)) % Mod]

consts_B, consts_W = [0]*(B-1)+ [1], [0]*(W-1)+[1]
for i in range(B+W):
  if i >= B: consts_B += [consts_B[-1]*(i)*inv_t[i-B+1]%Mod]
  if i >= W: consts_W += [consts_W[-1]*(i)*inv_t[i-W+1]%Mod]

p, q = 0, 0
inv = 1
for i in range(B+W):
  print((1-p+q)*magick%Mod)
  inv *= magick
  inv = inv%Mod
  p += consts_B[i]*inv
  q += consts_W[i]*inv

Submission status: Accepted

Solution code:
# 入力
B, W = map(int, input().split())

# ModInt定義
MOD = 10**9 + 7


class ModInt:
    def __init__(self, x):
        self.x = x % MOD

    def __str__(self):
        return str(self.x)

    def __repr__(self):
        return str(self.x)

    def __add__(self, other):
        return (
            ModInt(self.x + other.x) if isinstance(other, ModInt) else
            ModInt(self.x + other)
        )

    def __sub__(self, other):
        return (
            ModInt(self.x - other.x) if isinstance(other, ModInt) else
            ModInt(self.x - other)
        )

    def __mul__(self, other):
        return (
            ModInt(self.x * other.x) if isinstance(other, ModInt) else
            ModInt(self.x * other)
        )

    def __truediv__(self, other):
        return (
            ModInt(
                self.x * pow(other.x, MOD - 2, MOD)
            ) if isinstance(other, ModInt) else
            ModInt(self.x * pow(other, MOD - 2, MOD))
        )

    def __pow__(self, other):
        return (
            ModInt(pow(self.x, other.x, MOD)) if isinstance(other, ModInt) else
            ModInt(pow(self.x, other, MOD))
        )

    __radd__ = __add__
    __rsub__ = __sub__
    __rmul__ = __mul__
    __rtruediv__ = __truediv__
    __rpow__ = __pow__


p = ModInt(0)
q = ModInt(0)
cp = ModInt(0)
cq = ModInt(0)
for i in range(1, B + W + 1):
    # i番目の解は i-1番目までに白チョコレートが枯渇している確率 +
    # i-1番目までにどちらのチョコレートも枯渇しない確率 / 2
    ans = (1 + q - p) / 2
    # 出力
    print(ans)
    cp = (
        ModInt(0) if i < B else
        ModInt(1) if i == B else
        cp * (i - 1) / (i - B)
    )
    p = (
        ModInt(0) if i < B else
        ModInt(1) / ModInt(2)**i if i == B else
        p + cp / ModInt(2)**i
    )
    cq = (
        ModInt(0) if i < W else
        ModInt(1) if i == W else
        cq * (i - 1) / (i - W)
    )
    q = (
        ModInt(0) if i < W else
        ModInt(1) / ModInt(2)**i if i == W else
        q + cq / ModInt(2)**i
    )

Submission status: Accepted

Solution code:
MOD = 10**9+7
MAX = 2*(10**5)+1
fac = [1 for i in range(MAX)]
finv = [1 for i in range(MAX)]
inv = [1 for i in range(MAX)]
b,w = map(int,input().split())
if (b >= w):
    flag = 0
else:
    flag = 1
    b,w = w,b

for i in range(2,MAX):
    fac[i] = fac[i-1]*i%MOD
    inv[i] = MOD - inv[MOD%i]*(MOD // i)%MOD
    finv[i]=finv[i-1]*inv[i]%MOD

def com(n,k):
    if(n < k): return 0
    return fac[n]*(finv[k]*finv[n-k]%MOD)%MOD

def pow(n,k,m):#n^k mod m
    ans = 1
    while k>0:
        if(k & 1):ans = (ans*n) % m
        n = (n*n)%m
        k >>= 1
    return ans

for i in range(w):
    print(inv[2])

p1 = 1
p2 = 0
for i in range(1,w+1):
    p2 = (p2+ com(w,i)) % MOD

deno = pow(inv[2],w,MOD)
denodeno = pow(2,w,MOD)
for i in range(b-w):
    p1 = (p1*2)%MOD
    deno = (deno* inv[2]) % MOD
    denodeno = (denodeno*2) % MOD
    if(flag == 0):print(((p1+p2)*deno)%MOD)
    else : print(((denodeno-p1-p2)*deno)%MOD)
    p2 = (p2*2-com(w+i,i+1)) % MOD
    p1 = (p1+com(w+i,i+1)) % MOD
    
    


p2 -= 1

for i in range(b+1,b+w+1):
    p1 *= 2
    deno = (deno* inv[2]) % MOD
    denodeno = (denodeno*2) % MOD
    if(flag == 0):print(((p1+p2)*deno)%MOD)
    else : print(((denodeno-p1-p2)*deno)%MOD)
    p1 = (p1+com(i-1,w-1)) % MOD
    p2 = (p2*2-com(i-1,w-1)-com(i-1,i-b)) % MOD

Submission status: Accepted

Solution code:
import sys
write = sys.stdout.write

B, W = map(int, sys.stdin.readline().split())
MOD = 10**9 + 7

L = B+W
fact = [1]*(L+1)
rfact = [1]*(L+1)
for i in range(L):
    fact[i+1] = r = fact[i] * (i+1) % MOD
rfact[L] = pow(fact[L], MOD-2, MOD)
for i in range(L, 0, -1):
    rfact[i-1] = rfact[i] * i % MOD

rev2 = pow(2, MOD-2, MOD)

p = q = 0

L = min(B, W)
r2 = "%d\n" % rev2
for i in range(L):
    write(r2)
base = pow(rev2, L-1, MOD)

rB = rfact[B-1]; rW = rfact[W-1]
for i in range(L, B+W):
    base = rev2 * base % MOD
    if i >= B:
        p = (p + (fact[i-1]*rfact[i-B] % MOD) * (rB * base % MOD) % MOD) % MOD
    if i >= W:
        q = (q + (fact[i-1]*rfact[i-W] % MOD) * (rW * base % MOD) % MOD) % MOD
    write("%d\n" % ((1-p+q)*rev2 % MOD))

Submission status: Accepted

Solution code:
import sys

B, W = map(int, sys.stdin.readline().split())
MOD = 10**9 + 7

L = B+W
fact = [1]*(L+1)
rfact = [1]*(L+1)
for i in range(L):
    fact[i+1] = r = fact[i] * (i+1) % MOD
    rfact[i+1] = pow(r, MOD-2, MOD)

rev2 = pow(2, MOD-2, MOD)

p = q = 0

L = min(B, W)
r2 = "%d\n" % rev2
for i in range(L):
    sys.stdout.write(r2)
base = pow(rev2, L-1, MOD)

rB = rfact[B-1]; rW = rfact[W-1]
for i in range(L, B+W):
    base = rev2 * base % MOD
    if i >= B:
        p = (p + (fact[i-1]*rfact[i-B] % MOD) * (rB * base % MOD) % MOD) % MOD
    if i >= W:
        q = (q + (fact[i-1]*rfact[i-W] % MOD) * (rW * base % MOD) % MOD) % MOD
    sys.stdout.write("%d\n" % ((1-p+q)*rev2 % MOD))

Submission status: Accepted

Solution code:
import __pypy__
import sys

B, W = map(int, sys.stdin.readline().split())
MOD = 10**9 + 7

L = B+W
fact = [1]*(L+1)
rfact = [1]*(L+1)
for i in range(L):
    fact[i+1] = r = fact[i] * (i+1) % MOD
    rfact[i+1] = pow(r, MOD-2, MOD)

rev2 = pow(2, MOD-2, MOD)

p = q = 0
base = 1
bl = __pypy__.builders.StringBuilder(B+W)
bl.append("%d\n" % rev2)
for i in range(1, B+W):
    base = rev2 * base % MOD
    if i-B >= 0:
        p = (p + ((fact[i-1]*rfact[B-1] % MOD)*rfact[i-B] % MOD)*base % MOD) % MOD
    if i-W >= 0:
        q = (q + ((fact[i-1]*rfact[W-1] % MOD)*rfact[i-W] % MOD)*base % MOD) % MOD
    bl.append("%d\n" % ((1-p+q)*rev2 % MOD))
sys.stdout.write(bl.build())

Submission status: Accepted

Solution code:
import sys

B, W = map(int, sys.stdin.readline().split())
MOD = 10**9 + 7

L = B+W
fact = [1]*(L+1)
rfact = [1]*(L+1)
for i in range(L):
    fact[i+1] = r = fact[i] * (i+1) % MOD
    rfact[i+1] = pow(r, MOD-2, MOD)

rev2 = pow(2, MOD-2, MOD)

p = q = 0
base = 1
ans = ["%d\n" % rev2]
for i in range(1, B+W):
    base = rev2 * base % MOD
    if i-B >= 0:
        p = (p + ((fact[i-1]*rfact[B-1] % MOD)*rfact[i-B] % MOD)*base % MOD) % MOD
    if i-W >= 0:
        q = (q + ((fact[i-1]*rfact[W-1] % MOD)*rfact[i-W] % MOD)*base % MOD) % MOD
    ans.append("%d\n" % ((1-p+q)*rev2 % MOD))
sys.stdout.writelines(ans)

Submission status: Accepted

Solution code:
import sys

B, W = map(int, sys.stdin.readline().split())
MOD = 10**9 + 7

L = B+W
fact = [1]*(L+1)
rfact = [1]*(L+1)
for i in range(L):
    fact[i+1] = r = fact[i] * (i+1) % MOD
    rfact[i+1] = pow(r, MOD-2, MOD)

rev2 = pow(2, MOD-2, MOD)

p = q = 0
base = 1
ans = ["%d\n" % rev2]
for i in range(1, B+W):
    base = rev2 * base % MOD
    if i-B >= 0:
        p = (p + (fact[i-1]*rfact[B-1]*rfact[i-B] % MOD)*base % MOD) % MOD
    if i-W >= 0:
        q = (q + (fact[i-1]*rfact[W-1]*rfact[i-W] % MOD)*base % MOD) % MOD
    ans.append("%d\n" % ((1-p+q)*rev2 % MOD))
sys.stdout.writelines(ans)

Submission status: Accepted

Solution code:
# 入力
B, W = map(int, input().split())

# ModInt定義
MOD = 10**9 + 7


class ModInt:
    def __init__(self, x):
        self.x = x % MOD

    def __str__(self):
        return str(self.x)

    def __repr__(self):
        return str(self.x)

    def __add__(self, other):
        return (
            ModInt(self.x + other.x) if isinstance(other, ModInt) else
            ModInt(self.x + other)
        )

    def __sub__(self, other):
        return (
            ModInt(self.x - other.x) if isinstance(other, ModInt) else
            ModInt(self.x - other)
        )

    def __mul__(self, other):
        return (
            ModInt(self.x * other.x) if isinstance(other, ModInt) else
            ModInt(self.x * other)
        )

    def __truediv__(self, other):
        return (
            ModInt(
                self.x * pow(other.x, MOD - 2, MOD)
            ) if isinstance(other, ModInt) else
            ModInt(self.x * pow(other, MOD - 2, MOD))
        )

    def __pow__(self, other):
        return (
            ModInt(pow(self.x, other.x, MOD)) if isinstance(other, ModInt) else
            ModInt(self.x * pow(other, MOD - 2, MOD))
        )

    __radd__ = __add__
    __rsub__ = __sub__
    __rmul__ = __mul__
    __rtruediv__ = __truediv__
    __rpow__ = __pow__


p = ModInt(0)
q = ModInt(0)
cp = ModInt(0)
cq = ModInt(0)
for i in range(1, B + W + 1):
    # i番目の解は i-1番目までに白チョコレートが枯渇している確率 +
    # i-1番目までにどちらのチョコレートも枯渇しない確率 / 2
    ans = (1 + q - p) / 2
    # 出力
    print(ans)
    cp = (
        ModInt(0) if i < B else
        ModInt(1) if i == B else
        cp * (i - 1) / (i - B)
    )
    p = (
        ModInt(0) if i < B else
        ModInt(1) / ModInt(2**i) if i == B else
        p + cp / pow(2, i, MOD)
    )
    cq = (
        ModInt(0) if i < W else
        ModInt(1) if i == W else
        cq * (i - 1) / (i - W)
    )
    q = (
        ModInt(0) if i < W else
        ModInt(1) / ModInt(2**i) if i == W else
        q + cq / pow(2, i, MOD)
    )

Submission status: Accepted

Solution code:
b, w = map(int, input().split())
MOD = 10 ** 9 + 7
list_size = 200001

f_list = [1] * list_size
f_r_list = [1] * list_size

for i in range(list_size - 1):
	f_list[i + 1] = int((f_list[i] * (i + 2)) % MOD)

f_r_list[-1] = pow(f_list[-1], MOD - 2, MOD)

for i in range(2, list_size + 1):
	f_r_list[-i] = int((f_r_list[-i + 1] * (list_size + 2 - i)) % MOD)

def comb(n, r):
	if n < r:
		return 0
	elif n == 0 or r == 0 or n == r:
		return 1
	else:
		return (((f_list[n - 1] * f_r_list[n - r - 1]) % MOD) * f_r_list[r - 1]) % MOD 

two_pow = [1]
for i in range(b+w+1):
	two_pow.append((two_pow[-1] * 500000004) % MOD)

ans = 500000004
print(ans)
for i in range(1, b+w):
	if i-1 >= b-1:
		ans -= comb(i-1, b-1) * two_pow[i+1]
	if i-w >= 0:
		ans += comb(i-1, i-w) * two_pow[i+1]
	ans %= MOD
	print(ans)

Submission status: Accepted

Solution code:
B, W = map(int,input().split())
Mod = 10**9+7
magick = 500000004

inv_t = [0]+[1]
inv_2 = [0]+[magick]
for i in range(2,B+W+1):
  inv_t += [inv_t[Mod % i] * (Mod - int(Mod / i)) % Mod]
  inv_2 += [inv_2[-1]*magick % Mod]

consts_B, consts_W = [0]*(B-1)+ [1], [0]*(W-1)+[1]
for i in range(B+W):
  if i >= B: consts_B += [consts_B[-1]*(i)*inv_t[i-B+1]%Mod]
  if i >= W: consts_W += [consts_W[-1]*(i)*inv_t[i-W+1]%Mod]

p, q = 0, 0
for i in range(B+W):
  print((1-p+q)*magick%Mod)
  p += consts_B[i]*inv_2[i+1]
  q += consts_W[i]*inv_2[i+1]

Submission status: Accepted

Solution code:
P = 10**9+7
B, W = map(int, input().split())

inv2 = [1]
fact = [1]
for i in range(B+W+10):
    inv2.append((inv2[-1] + (inv2[-1] % 2) * P)//2)
    fact.append(fact[-1] * (i+1) % P)

def C(a, b):
    return fact[a]*pow(fact[b]*fact[a-b]%P, P-2, P) % P

L, R = 0, 0
for i in range(B+W):
    if i >= B:
        L *= 2
        L += C(i-1, B-1)
        L %= P
    if i >= W:
        R *= 2
        R += C(i-1, W-1)
        R %= P
    print(((R-L)*inv2[i+1]+inv2[1])%P)

Submission status: Accepted

Solution code:
P = 10**9+7
B, W = map(int, input().split())

inv2 = [1]
fact = [1]
for i in range(B+W+10):
    inv2.append((inv2[-1] + (inv2[-1] % 2) * P)//2)
    fact.append(fact[-1] * (i+1) % P)

def C(a, b):
    return fact[a]*pow(fact[b]*fact[a-b]%P, P-2, P) % P

L, R = 0, 0
for i in range(B+W):
    if i >= B:
        L *= 2
        L += C(i-1, B-1)
        L %= P
    if i >= W:
        R *= 2
        R += C(i-1, W-1)
        R %= P
    if i < min(B, W):
        aa = print(inv2[1])
    else:
        bb = print(((R-L)*inv2[i+1]+inv2[1])%P)

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10**9)
mod=10**9+7
b,w=map(int,input().split())
n=b+w

def power(x,n):
    if n==0:
        return 1
    elif n%2:
        return power(x,n//2)**2*x%mod
    else:
        return power(x,n//2)**2%mod
def modinv(n):
    return power(n,mod-2)

factorial=[1]
for i in range(1,n+1):
    factorial.append(factorial[i-1]*i%mod)
inverse=[0]*(n+1)
inverse[-1]=modinv(factorial[-1])
for i in range(n)[::-1]:
    inverse[i]=inverse[i+1]*(i+1)%mod
def comb(n,r):
    if n<r or r<0:
        return 0
    return factorial[n]*inverse[r]*inverse[n-r]%mod

Pb,Pw=[0]*n,[0]*n
inv2=modinv(2)
P=[1]*(n+1)
for i in range(1,n+1):
    P[i]=P[i-1]*inv2%mod
for i in range(1,n):
    Pb[i]=(Pb[i-1]+comb(i-1,b-1)*P[i])%mod
    Pw[i]=(Pw[i-1]+comb(i-1,w-1)*P[i])%mod
for i in range(n):
    print((1-Pb[i]+Pw[i])*inv2%mod)

Submission status: Accepted

Solution code:
B,W=map(int,input().split())
N=B+W+2
mod=10**9+7
table=[1]*(N+3)
t=1
for i in range(1,N+3):
    t*=i
    t%=mod
    table[i]=t
rtable=[1]*(N+3)
t=1
for i in range(1,N+3):
    t*=pow(i,mod-2,mod)
    t%=mod
    rtable[i]=t
p=[0]*(B+W+1)
q=[0]*(B+W+1)
t=pow(2,mod-2,mod)
for i in range(W,B+W+1):
    if i==W:
        p[i]=pow(t,i,mod)
    else:
        p[i]=(p[i-1]+table[i-1]*rtable[W-1]*rtable[i-W]*pow(t,i,mod))%mod
            #(table[i]*rtable[W]*table[i-W]*pow(t,i,mod))%mod
for i in range(B,B+W+1):
    if i==B:
        q[i]=pow(t,i,mod)
    else:
        q[i]=(q[i-1]+table[i-1]*rtable[B-1]*rtable[i-B]*pow(t,i,mod))%mod
#print(p,q)
for i in range(B+W):
    ans=((1+p[i]-q[i])*pow(2,mod-2,mod))%mod
    print(ans)

Submission status: Accepted

Solution code:
B,W=map(int,input().split())
mod=10**9+7
kaijo={0:1}
Xdiv={0:1}
two=pow(2,mod-2,mod)
for i in range(1,1+B+W):
    kaijo[i]=(kaijo[i-1]*i)%mod
    Xdiv[i]=(Xdiv[i-1]*two)%mod
gyaku={B+W:pow(kaijo[B+W],mod-2,mod)}
for i in range(B+W,0,-1):
    gyaku[i-1]=(gyaku[i]*i)%mod
def conb(a,b):
    if a<b:return 0
    return (kaijo[a]*gyaku[a-b]*gyaku[b])%mod
Blost,Wlost=0,0
for k in range(1,B+W):
    print(((1+Wlost-Blost)*two)%mod)
    Blost=(Blost+conb(k-1,B-1)*Xdiv[k])%mod
    Wlost=(Wlost+conb(k-1,W-1)*Xdiv[k])%mod
print(((1+Wlost-Blost)*two)%mod)

Submission status: Accepted

Solution code:
Q = 10**9+7
def getInv(N):#Qはmod
    inv = [0] * (N + 1)
    inv[0] = 1
    inv[1] = 1
    for i in range(2, N + 1):
        inv[i] = (-(Q // i) * inv[Q%i]) % Q
    return inv

B, W = map( int, input().split())
Z = getInv(B+W)
I = [1]*(B+W+1)
for i in range(1,B+W+1):
    I[i] = (I[i-1]*Z[i])%Q

F = [1]*(B+W+1)
for i in range(1,B+W+1):
    F[i] = F[i-1]*i%Q
w = 0
b = 0
for i in range(1,B+W+1):
    if i - 1 < B and i - 1 < W:
        print(I[2])
    # elif i - 1 == W and i - 1< B:
    #     w = pow(I[2],i-1,Q)
    #     print(((1 -  w)%Q*I[2]%Q + w)%Q)
    elif i - 1 >= W and i - 1< B: #i > B
        w += F[i-2]*I[i-1-W]%Q*I[W-1]%Q*pow(I[2],i-1,Q)%Q
        w %= Q
        print(((1 -  w)%Q*I[2]%Q + w)%Q)
    # elif i - 1 == B and i - 1 < W:
    #     b = pow(I[2],i-1,Q)
    #     print((1-b)%Q*I[2]%Q)
    elif i - 1 >= B and i - 1 < W: #i > W
        b += F[i-2]*I[i-1-B]%Q*I[B-1]%Q*pow(I[2],i-1,Q)%Q
        b %= Q
        print((1-b)%Q*I[2]%Q)
    # elif i - 1 == B and i - 1 == W:
    #     w = pow(I[2],i-1,Q)
    #     b = pow(I[2],i-1,Q)
    #     print(((1 -  w - b)%Q*I[2]%Q + w)%Q)
    else:
        w += F[i-2]*I[i-1-W]%Q*I[W-1]%Q*pow(I[2],i-1,Q)%Q
        w %= Q
        b += F[i-2]*I[i-1-B]%Q*I[B-1]%Q*pow(I[2],i-1,Q)%Q
        b %= Q
        print(((1 -  w - b)%Q*I[2]%Q + w)%Q)

Submission status: Accepted

Solution code:
mod=10**9+7
inv2=5*(10**8)+4
B,W=map(int,input().split())
 
Factorial=[1]*(B+W+1)
for i in range(1,B+W+1):
  Factorial[i]=Factorial[i-1]*(i)%mod
def power(x,y):
  if y==0:
    return 1
  elif y==1:
    return x%mod
  elif y%2==0:
    return power(x,y//2)**2%mod
  else:
    return (power(x,y//2)**2)*x%mod
 
inverseFactorial=[1]*(B+W+1)
inverseFactorial[B+W]=power(Factorial[B+W],mod-2)
for i in range(0,B+W)[::-1]:
  inverseFactorial[i]=(inverseFactorial[i+1]*(i+1))%mod
  
def comb(x,y):
  if x<y or y<0:
    return 0
  else:
    return (Factorial[x]*inverseFactorial[y]*inverseFactorial[x-y])%mod
w,b=[0]*(B+W+1),[0]*(B+W+1)
p2=[1]*(B+W+1)
for i in range(1,B+W+1):
  p2[i]=(p2[i-1]*2)%mod
bunbo=power(inv2,B+W+1)  

for i in range(1,B+W+1):
  w[i]=(w[i-1]+comb(i-1,W-1)*p2[B+W-i])%mod
  b[i]=(b[i-1]+comb(i-1,B-1)*p2[B+W-i])%mod
ans=[0]*(B+W+1)
#print(w)
#print(b)
for i in range(1,B+W+1):
  ans[i]=(p2[B+W]+w[i-1]-b[i-1])*bunbo
  print(ans[i]%mod)