submission_id
string
problem_id
string
status
string
code
string
input
string
output
string
problem_description
string
s026120904
p04046
Accepted
H,W,A,B = map(int,input().split()) MOD = (10**9)+7 D = [0,2, 9, 11, 14, 15, 17, 19, 20, 23, 24, 25, 27, 28, 29] def inv(a): ans = 1 hoge = a for count in range(31): if (count) in D: ans *= hoge ans %= MOD hoge *= hoge hoge %= MOD return ans g = [1]*(H+W) for i in range(1,H+W): g[i] = (g[i-1]*i%MOD) ans = 0 for i in range(H-A): L = g[B-1+i]*inv(g[B-1])*inv(g[i]) R = g[(W-B-1)+(H-1-i)]*inv(g[(W-B-1)])*inv(g[H-1-i]) ans += L*R%MOD print(ans%MOD)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s342324390
p04046
Accepted
mod=10**9+7 g1=[1,1] g2=[1,1] inverse=[0,1] h,w,a,b=map(int,input().split()) n=h+w for i in range(2,n+1): g1.append((g1[-1]*i)%mod) inverse.append((-inverse[mod % i]*(mod//i))%mod) g2.append((g2[-1]*inverse[-1])%mod) def cmb(n,r): return g1[n]*g2[r]*g2[n-r]%mod ans=0 while a<h and b<w: ans=(ans+cmb(h-1-a+b,b)*cmb(w-1-b+a,a))%mod a+=1 b+=1 print(ans)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s432972669
p04046
Accepted
class Combination: def __init__(self, size=100, mod=10**9 + 7): self.size = size + 2 self.mod = mod self.fact = [1, 1] + [0] * size self.factInv = [1, 1] + [0] * size self.inv = [0, 1] + [0] * size for i in range(2, size + 2): self.fact[i] = self.fact[i - 1] * i % mod self.inv[i] = -self.inv[mod % i] * (mod // i) % mod self.factInv[i] = self.factInv[i - 1] * self.inv[i] % mod def ncr(self, n, r): if n < r or n < 0 or r < 0: return 0 return self.fact[n] * (self.factInv[r] * self.factInv[n - r] % self.mod) % self.mod def sol(): H, W, A, B = map(int, input().split()) MOD = 10**9 + 7 comb = Combination(H + W + 100) ans = 0 for i in range(W - B): ans = (ans + comb.ncr(H - 1 - A + B + i, H - 1 - A) * comb.ncr(A - 1 + W - 1 - B - i, A - 1)) % MOD print(ans) sol()
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s901634067
p04046
Accepted
H, W, A, B = map(int, input().split()) MAX = 200000 MOD = 1000000007 fac = [0]*MAX finv = [0]*MAX inv = [0]*MAX fac[0] = 1 fac[1] = 1 finv[0] = 1 finv[1] = 1 inv[1] = 1 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 # sum_{i=B}^{W-1} C_{H-A-1+i}_{i}C_{A+W-i-2}_{A-1} r = 0 for i in range(B, W): r += fac[H-A-1+i]*(finv[H-A-1]*finv[i]%MOD)%MOD * fac[A+W-i-2]*(finv[A-1]*finv[W-1-i]%MOD)%MOD r = r%MOD print(r)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s004650250
p04046
Accepted
h,w,a,b=map(int,input().split());m=10**9+7;f=[1] for i in range(h+w):f+=[f[-1]*(i+1)%m] def comb(a,b,m):return f[a]*pow(f[b],m-2,m)*pow(f[a-b],m-2,m)%m x=0 for i in range(b,w):x+=comb(h-a+i-1,i,m)*comb(a+w-i-2,a-1,m)%m print(x%m)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s015769866
p04046
Accepted
high, width, y, x = map(int, input().split()) def nCr(n, r): return ((((fact[n]*finv[n-r])%mod)*finv[r])%mod) mod = 10**9 + 7 inv = mod - 2 maxn = high + width - 2 fact = [0] * (maxn+1) finv = [0] * (maxn+1) fact[0] = fact[1] = 1 finv[0] = finv[1] = 1 # 階乗を求める for i in range(2, maxn+1): fact[i] = (fact[i-1] * i) % mod # 階乗の逆元を求める for i in range(2, maxn+1): finv[i] = pow(fact[i], inv, mod) route = 0 for i in range(1, high-y+1): route += (nCr(x+i-2, i-1) * nCr(width+high-x-1-i, high-i)) % mod route = route % mod print(int(route))
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s568419484
p04046
Accepted
h, w, a, b = map( int, input().split() ) SIZE = h+w+2 MOD = 10**9 + 7 MOD_Farmer = 10**9+5 fact = [0] * SIZE inv = [0] * SIZE fact_inv = [0] * SIZE # prepare -------------------------------- inv[0] = 0 inv[1] = 1 fact[0] = fact[1] = 1 fact_inv[0] = fact_inv[1] = 1 for i in range( 2, SIZE ): inv[i] = MOD - (MOD//i)*inv[MOD%i]%MOD fact[i] = fact[i-1] * i % MOD fact_inv[i] = fact_inv[i-1] * inv[i] % MOD # fact_inv[i] = fact[i] **( 10**4 ) \ # * fact[i] **( 10**5 ) \ # * fact[i] ** 5 % MOD % MOD # ---------------------------------------- def comb( n, r ): if r >= 0 and n >= 0: return fact[n] * fact_inv[n-r]%MOD * fact_inv[r]%MOD else : return 0.0 # ---------------------------------------- #print( fact_inv ) ans = 0 for i in range( h-a ): ans += comb(i+b-1, b-1) * comb(h-i-1+w-b-1,w-b-1) ans %= MOD print( ans )
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s969206739
p04046
Accepted
mod=10**9+7 g1=[1,1] g2=[1,1] inverse=[0,1] h,w,a,b=map(int,input().split()) n=h+w for i in range(2,n+1): g1.append((g1[-1]*i)%mod) inverse.append((-inverse[mod % i]*(mod//i))%mod) g2.append((g2[-1]*inverse[-1])%mod) def cmb(n,r): return g1[n]*g2[r]*g2[n-r]%mod ans=0 while a<h and b<w: ans=(ans+cmb(h-1-a+b,b)*cmb(w-1-b+a,a))%mod a+=1 b+=1 print(ans)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s257430025
p04046
Accepted
h,w,a,b=map(int,input().split()) n_func=[None for _ in [0]*200001] n_func[0]=1 for i in range(1,200001): n_func[i]=(i*n_func[i-1])%(10**9+7) def inv_n(n,mod=10**9+7):return pow(n,mod-2,mod) def nCr(n,r,mod=10**9+7):return inv_n(n_func[n-r]*n_func[r]%mod,mod)*n_func[n]%mod cnt=0 for i in range(h-a): cnt=(cnt+nCr(i+b-1,b-1)*nCr(h+w-b-i-2,w-b-1))%(10**9+7) print(cnt)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s396753934
p04046
Accepted
#!/usr/bin/env python3 #ABC42 D import sys import math import bisect sys.setrecursionlimit(1000000000) from heapq import heappush, heappop from collections import defaultdict from itertools import accumulate from collections import Counter from collections import deque from operator import itemgetter from itertools import permutations mod = 10**9 + 7 h,w,a,b = map(int,input().split()) fact = [1]*(200000+1) for i in range(1,200000+1): fact[i] = fact[i-1]*(i) % mod def comb(n,r): return fact[n] * pow(fact[r],mod-2,mod) * pow(fact[n-r],mod-2,mod) % mod ans = 0 for i in range(b,w): x = comb(h-a-1+i,i)*comb(a-1+w-i-1,a-1) % mod ans += x ans %= mod print(ans)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s572632984
p04046
Accepted
MOD=10**9+7 fact=[1] temp=1 for i in range(1,3*10**5+1): temp*=i temp%=MOD fact+=[temp] def bino(a,b): if a<b: return 0 up=fact[a] down=fact[a-b]*fact[b] return (up*pow(down,MOD-2,MOD))%MOD def find(H,W,A,B): temp=0 for x in range(1,H-A+1): temp+=bino(x+B-2,x-1)*bino(W-B-1+H-x,H-x) temp%=MOD return temp H,W,A,B=list(map(int,input().strip().split(" "))) print(find(H,W,A,B))
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s887985046
p04046
Accepted
# coding: utf-8 # Your code here! SIZE=200001; MOD=10**9+7 #998244353 #ここを変更する SIZE += 1 inv = [0]*SIZE # inv[j] = j^{-1} mod MOD fac = [0]*SIZE # fac[j] = j! mod MOD finv = [0]*SIZE # finv[j] = (j!)^{-1} mod MOD inv[1] = 1 fac[0] = fac[1] = 1 finv[0] = finv[1] = 1 for i in range(2,SIZE): inv[i] = MOD - (MOD//i)*inv[MOD%i]%MOD fac[i] = fac[i-1]*i%MOD finv[i]= finv[i-1]*inv[i]%MOD def choose(n,r): # nCk mod MOD の計算 if 0 <= r <= n: return (fac[n]*finv[r]%MOD)*finv[n-r]%MOD else: return 0 import sys sys.setrecursionlimit(10**6) readline = sys.stdin.readline #文字列入力のときは注意 h,w,a,b = [int(i) for i in readline().split()] ans = 0 for i in range(h-a): ans += choose(i+b-1, b-1) * choose(h-i-1+w-b-1,w-b-1) ans %= MOD print(ans)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s111610382
p04046
Accepted
def modinv(a, m): #mod. m でのaの逆元 a^{-1}を計算する b = m u = 1 v = 0 while b: t = a//b a -= t * b a, b = b, a u -= t * v u, v = v, u u %= m if u < 0: u += m return u H, W, A, B = map(int, input().split()) MOD = 10 ** 9 + 7 #O(N) first = [] for i in range(0, H - A): if i == 0: tmp = 1 first.append(tmp) continue tmp = (tmp * (B + i - 1) * modinv(i, MOD)) % MOD first.append(tmp) # print (first) #O(N) second = [] for i in range(0, H): if i == 0: tmp = 1 second.append(tmp) continue tmp = (tmp * ((W - B) + i - 1) * modinv(i, MOD)) % MOD second.append(tmp) # print (second) #O(N) ans = 0 for i in range(H - A): ans = (ans + first[i] * second[H - 1 -i]) % MOD print (ans) #total O(3 * N) --> O(N)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s204553810
p04046
Accepted
H,W,A,B=map(int,input().split()) N=H+W mod=10**9+7 factl=[1] for i in range(1,N+1): factl.append(factl[-1]*i%mod) invl=[pow(factl[-1],mod-2,mod)] for i in range(N,0,-1): invl.append(invl[-1]*i%mod) invl.reverse() def Comb(a,b): return factl[a]*invl[a-b]*invl[b] ans=0 for i in range(H-A): ans+=Comb(B+i-1,i)*Comb((W-B-1)+(H-i-1), H-i-1) ans%=mod print(ans)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s472442999
p04046
Accepted
import math def modinv(a,m): b = m u = 1 v = 0 while(b): t = a // b a -= t * b a,b = b,a u -= t * v u,v = v,u u %= m if u < 0: u += m return u inp = input().split(' ') inpint = [int(i) for i in inp] h = inpint[0] w = inpint[1] a = inpint[2] b = inpint[3] xfac = [0 for r in range(h+w-2)] xfac_inv = [0 for r in range(h+w-2)] for i in range(h+w-2): if i == 0: xfac[i] = 1 else: xfac[i] = (xfac[i-1]*i)%1000000007 for i in reversed(range(h+w-2)): if i == h+w-3: xfac_inv[i] = modinv(math.factorial(i),1000000007) else: xfac_inv[i] = xfac_inv[i+1] * (i+1) % 1000000007 # print(xfac) # print(xfac_inv) # for i in range(h+w-2): # print(xfac[i]*xfac_inv[i]%1000000007) def com(a,b): return (xfac[a] * xfac_inv[b] * xfac_inv[a-b])%1000000007 sum = 0 for i in range(b,w): sum += com(i + h-a-1,h-a-1) * com((a-1)+w-i-1, w-i-1) sum %= 1000000007 print(sum)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s529178804
p04046
Accepted
def frac_with_mod(a,m): outlist = [ 1 for i in range(a+1) ] for i in range(1,a+1): outlist[i] = (outlist[i-1] * i)% m return outlist frac_list = frac_with_mod(2*10**5,10**9+7) def comb_with_mod(a,b,m): global frac_list return ( frac_list[a+b-2] * pow(frac_list[a-1],m-2,m) * pow(frac_list[b-1],m-2,m) ) % m mod = 10**9+7 h, w, a, b = [ int(v) for v in input().split() ] c = h - a d = w - b if c == 1: waypoint_list = [ 1 for i in range(d) ] else: waypoint_list = [ comb_with_mod(b+1+i,c-1,mod) for i in range(d) ] s = comb_with_mod(b,c,mod) for i in range(d): waypoint_list[i] = ( s + waypoint_list[i] ) % mod s = waypoint_list[i] waypoint_list[i] = ( comb_with_mod(a,d-i,mod) * waypoint_list[i] ) % mod print(sum(waypoint_list)%mod)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s844835786
p04046
Accepted
H, W, A, B = list(map(int, input().split())) p = 1000000007 fac = [1] * (H + W + 1) x = 1 for i in range(1, H + W + 1): x *= i x %= p fac[i] = x ifac = [1] * (H + W + 1) x = fac[H + W] q = p - 2 while q > 0: if q & 1: ifac[H + W] *= x % p x *= x % p q >>= 1 for i in range(H + W - 1, 0, -1): ifac[i] = ifac[i + 1] * (i + 1) % p def com(a, b): return (fac[a] * ifac[b] * ifac[a-b]) % p ans = 0 for i in range(H-A): ans += com(B+i-1, i) * com(W-B+H-i-2, H-i-1) ans %= p print(ans)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s637152001
p04046
Accepted
M = 1000000007 h, w, a, b = list(map(int, input().split())) def inv(k): return pow(k, M - 2, M) def comb(n, k): if n < k: return 0 elif k == 0: return 1 else: r = 1 for i in range(k): r = (r * ((n - i) * inv(k - i)) % M) % M return r ans = comb(h + w - 2, h - 1) if a == 1 and b == 1: ans -= 1 elif a == 1: ans -= comb(h + b - 2, h - 1) elif b == 1: ans -= comb(w + a - 2, w - 1) else: memo = [1] * b for i in range(1, b): memo[i] = (memo[i - 1] * (h - a + i) * inv(i)) % M memo2 = [0] * b height = a - 1 width = w - b memo2[b - 1] = comb(height + width, height) for i in range(1, b): memo2[b - i - 1] = (memo2[b - i] * (height + width + i) * inv(width + i)) % M ans -= memo[0] * memo2[0] ans %= M for i in range(1, b): ans -= (memo[i] - memo[i - 1]) * memo2[i] ans %= M print(ans % M)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s028350294
p04046
Accepted
h, w, a, b = map(int, input().split()) MOD = 10 ** 9 + 7 f = [1] * 300005 for i in range(2, 300005): f[i] = (f[i - 1] * i) % MOD def comb(k, n): if n < k: return 0 if k == 0 or n == k: return 1 return (f[n] * pow(f[k], MOD - 2, MOD) * pow(f[n - k], MOD - 2, MOD)) % MOD ans = 0 for i in range(h - a): ans += comb(i, i + b - 1) * comb(w - b - 1, w + h - 2 - i - b) % MOD ans %= MOD print(ans)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s215139442
p04046
Accepted
mod = 10**9 + 7 N = 200000 fact = [None] * (N+1) fact[0] = 1 for i in range(1, N+1): fact[i] = fact[i-1] * i % mod def comb(n, k): return fact[n] * pow(fact[k], mod-2, mod) * pow(fact[n-k], mod-2, mod) % mod H, W, A, B = map(int, input().split()) ans = 0 for b in range(B+1, W+1): tmp = comb(H-A+b-2, b-1) * comb(A+W-b-1, A-1) % mod ans = (ans + tmp) % mod print(ans)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s025302990
p04046
Accepted
H, W, A, B = map(int, input().split()) MOD = 10 ** 9 + 7 fac = [1] inv = [1] for i in range(1, H+W+1) : fac.append(fac[-1] * i % MOD) inv.append(pow(fac[-1], MOD - 2, MOD)) ret = 0 for i in range(H-A) : ret += fac[i+B-1]*inv[i]*inv[B-1]*fac[H+W-i-B-2]*inv[H-i-1]*inv[W-B-1]%MOD ret %= MOD print(ret)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s772888529
p04046
Accepted
import os import sys if os.getenv("LOCAL"): sys.stdin = open("_in.txt", "r") sys.setrecursionlimit(2147483647) INF = float("inf") IINF = 10 ** 18 MOD = 10 ** 9 + 7 H, W, A, B = list(map(int, sys.stdin.readline().split())) # dp = np.ones(W, dtype=int) # for h in range(1, H - A): # dp = dp.cumsum() % MOD # for h in range(H - A, H): # dp[B:] = dp[B:].cumsum() % MOD # print(dp[-1]) def get_factorials(max, mod=None): """ 階乗 0!, 1!, 2!, ..., max! :param int max: :param int mod: :return: """ ret = [1] n = 1 if mod: for i in range(1, max + 1): n *= i n %= mod ret.append(n) else: for i in range(1, max + 1): n *= i ret.append(n) return ret def mod_inv(a, mod): """ a の逆元 :param int a: :param int mod: :return: """ return pow(a, mod - 2, mod) factorials = get_factorials(H + W, MOD) def ncr(n, r, mod=None): """ scipy.misc.comb または scipy.special.comb と同じ 組み合わせの数 nCr :param int n: :param int r: :param int mod: 3 以上の素数であること :rtype: int """ if n < r: return 0 return factorials[n] * mod_inv(factorials[r], mod) * mod_inv(factorials[n - r], mod) % mod ans = 0 for i in range(W - B): ans += ncr((H - A - 1) + (W - i - 1), W - i - 1, MOD) * ncr((A - 1) + i, i, MOD) ans %= MOD print(ans)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s965791230
p04046
Accepted
mod = 1000000007 H, W, A, B = map(int, raw_input().split()) factorial = [1] for n in xrange(1, H+W): factorial.append(factorial[n-1]*n%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 = [0] * (H+W) inverseFactorial[H+W-1] = power(factorial[H+W-1], mod-2) for n in xrange(H+W-2, -1, -1): inverseFactorial[n] = inverseFactorial[n+1] * (n+1) % mod def combi(n, m): return factorial[n] * inverseFactorial[m] * inverseFactorial[n-m] % mod sum = 0 for i in xrange(B+1, W+1): sum = (sum + combi(H-A-1+i-1, i-1) * combi(A-1+W-i, W-i)) % mod print sum
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s301480273
p04046
Accepted
h, w, a, b = map(int, input().split()) mod = 10**9+7 fac = [1] for n in range(1, h+w): fac.append(fac[n-1]*n%mod) def modpow(a,n,mod): r=1 while n>0: if n&1: r = r*(a%mod) a *= a%mod n >>= 1 return r invfac = [0] * (h+w) invfac[h+w-1] = modpow(fac[h+w-1], mod-2, mod) for n in range(h+w-2, -1, -1): invfac[n] = invfac[n+1] * (n+1) % mod def com(a,b): return fac[a] * invfac[b] * invfac[a-b] % mod r=0 for i in range(w-b): r += com( h-a-1+b+i, b+i ) * com( a-1+w-b-i-1, a-1 ) % mod r %= mod print(r)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s602175149
p04046
Accepted
def mf(num,mod): ret=1 for i in range(1,num+1): ret=(ret*i)%mod return ret def rev(num,pow,mod): ret=1 while pow>0: if pow&1!=0: ret=(ret*num)%mod pow=pow>>1 num=(num*num)%mod return ret h,w,a,b=map(int,input().split()) mod=10**9+7 ans=[] s=0 tb=rev(mf(b,mod),mod-2,mod) tf=mf(b-1,mod) trev=[rev(mf(h-a-1,mod),mod-2,mod)] for i in range(h-a-1,0,-1): trev.append((trev[h-a-1-i]*i)%mod) trev=trev[::-1] for i in range(h-a): tf=(tf*(b+i))%mod ans.append((tf*tb*trev[i])%mod) ans[i]=(ans[i]-s)%mod s+=ans[i] tb=rev(mf(w-b-1,mod),mod-2,mod) tf=mf(w+a-b-2,mod) trev=[rev(mf(h-1,mod),mod-2,mod)] for i in range(h-1,0,-1): trev.append((trev[h-1-i]*i)%mod) for i in range(h-a-1,-1,-1): tf=(tf*(w-b-1+h-i-1))%mod ans[i]=(ans[i]*(tf*tb*trev[i])%mod) ret=0 for i in range(h-a): ret+=ans[i] ret=ret%mod print(ret)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s943735466
p04046
Accepted
MOD = 10**9 + 7 MAX = int(2e5+1) def div(a, b): return a * pow(b, MOD-2, MOD) % MOD FACT = [1] * (MAX+1) for i in range(1, MAX+1): FACT[i] = (i * FACT[i-1]) % MOD INV = [1] * (MAX+1) INV[MAX] = div(1, FACT[MAX]) for i in range(MAX, 0, -1): INV[i-1] = (INV[i] * i) % MOD def main(): H, W, A, B = map(int, input().split()) ans = 0 i = H-A-1 j = B while i >= 0 and j < W: a = (FACT[i+j] * INV[i] * INV[j]) % MOD b = (FACT[H-i-1 + W-j-1] * INV[H-i-1] * INV[W-j-1]) % MOD ans = (ans + a * b) % MOD i -= 1 j += 1 print(ans) if __name__ == "__main__": main()
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s815855023
p04046
Accepted
H, W, A, B = map(int, input().split()) MOD = 10 ** 9 + 7 N = 100 # 階乗 & 逆元計算 factorial = [1] inverse = [1] for i in range(1, H + W + 1): factorial.append(factorial[-1] * i % MOD) inverse.append(pow(factorial[-1], MOD - 2, MOD)) # 組み合わせ計算 def nCr(n, r): if n < r or r < 0: return 0 elif r == 0: return 1 return factorial[n] * inverse[r] * inverse[n - r] % MOD ans = 0 for w in range(B + 1, W + 1): ans += nCr(H - A - 1 + w - 1, w - 1) * nCr(A - 1 + W - w, A - 1) % MOD print(ans % MOD)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s833612910
p04046
Accepted
def main(): H, W, A, B = map(int, input().split()) H, W= H-1, W-1 DIV = 10**9+7 size = H+W+1 fact = [0]*size inverse = [0]*size inv_cal = [0]*size fact[:2] = [1, 1] inverse[:2] = [1, 1] inv_cal[:2] = [0, 1] for i in range(2, size): fact[i] = (fact[i-1]*i%DIV) inv_cal[i] = (-inv_cal[DIV%i]*(DIV//i))%DIV inverse[i] = inverse[i-1]*inv_cal[i]%DIV def C(n, r): ans = fact[n]*inverse[r]%DIV return ans*inverse[n-r]%DIV ans = 0 for x in range(B, W+1): y = H-A tmp = (C(x+y, x)*C(H+W-x-y-1, W-x))%DIV ans += tmp ans %= DIV print(ans) if __name__ == '__main__': main()
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s064393174
p04046
Accepted
def main(): H, W, A, B = map(int, input().split()) H, W= H-1, W-1 DIV = 10**9+7 size = H+W+1 fact = [0]*size inverse = [0]*size inv_cal = [0]*size fact[:2] = [1, 1] inverse[:2] = [1, 1] inv_cal[:2] = [0, 1] for i in range(2, size): fact[i] = (fact[i-1]*i%DIV) inv_cal[i] = (-inv_cal[DIV%i]*(DIV//i))%DIV inverse[i] = inverse[i-1]*inv_cal[i]%DIV def C(n, r): ans = fact[n]*inverse[r]%DIV return ans*inverse[n-r]%DIV ans = 0 for x in range(B, W+1): y = H-A tmp = (C(x+y, x)%DIV)*(C(H+W-x-y-1, W-x)%DIV)%DIV ans += tmp ans %= DIV print(ans) if __name__ == '__main__': main()
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s651220120
p04046
Accepted
def cmb(a, b, fact, mod): return fact[a] * pow(fact[b], mod - 2, mod) * pow(fact[a - b], mod - 2, mod) % mod def main(): h, w, a, b = map(int, input().split()) mod = 10 ** 9 + 7 fact = [1] * (h + w + 1) for i in range(1, h + w + 1): fact[i] = (fact[i - 1] * i) % mod ans = 0 for i in range(b, w): ans += cmb(h - a + i - 1, i, fact, mod) * cmb(w - 1 - i + a - 1, w - i - 1, fact, mod) % mod print(ans % mod) if __name__ == '__main__': main()
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s576785754
p04046
Accepted
def inpl(): return list(map(int, input().split())) H, W, A, B = inpl() MOD = 10**9 + 7 def cmb(n, r, mod): if ( r<0 or r>n ): return 0 r = min(r, n-r) return g1[n] * g2[r] * g2[n-r] % mod size = H+ W + 1 g1, g2, inverse = [0]*size, [0]*size, [0]*size g1[:2] = [1, 1] # 元テーブル g2[:2] = [1, 1] #逆元テーブル inverse[:2] = [0, 1] #逆元テーブル計算用テーブル for i in range(2, size): g1[i] = ( g1[i-1] * i ) % MOD inverse[i] = (-inverse[MOD % i] * (MOD//i) ) % MOD g2[i] = (g2[i-1] * inverse[i]) % MOD ans = 0 for a in range(H-A): h = H - a - 1 w = W - B - 1 ans = (ans + cmb(a+B-1, a, MOD)*cmb(h+w, h, MOD))%MOD print(ans)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s957738754
p04046
Accepted
class combnk_mod(object): # pが素数かつaとpが互いに素であるとき # フェルマーの小定理より以下が成り立つ. # 1 / a = a ** (p - 2) (mod p) # これを使って (1 / k!) mod p を計算する. def __init__(self, maxn, p): self.maxn = maxn self.p = p self.x = [1] # 分母にあたる数 1 / n! mod p self.y = [1] # 分子にあたる数. n! mod p for i in range(1, self.maxn + 1): self.x.append(self.x[-1] * pow(i, self.p - 2, self.p) % self.p) self.y.append(self.y[-1] * i % self.p) def combnk(self, n, k): return (self.x[n - k] * self.x[k] * self.y[n]) % self.p h, w, a, b = map(int, input().split()) MOD = 10 ** 9 + 7 c = combnk_mod(h + w, MOD) ans = 0 for i in range(b + 1, w + 1): ans += c.combnk(h - a + i - 2, i - 1) * c.combnk(a + w - i - 1, a - 1) % MOD ans %= MOD print(ans)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s754756754
p04046
Accepted
H,W,A,B=map(int,input().split()) INF=10**9+7 x=[0]*(H+W-1) x[0]=1 for i in range(1,H+W-1): x[i]=(i*x[i-1])%INF y=[0]*(H+W-1) y[-1]=pow(x[-1],10**9+5,INF) for i in range(H+W-2,0,-1): y[i-1]=(y[i]*i)%INF ans=0 for i in range(B,W): j=H-A-1 ans+=(x[i+j]*y[i]*y[j])*(x[W-1-i+H-2-j]*y[H-2-j]*y[W-1-i]) ans%=INF print(ans)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s162359607
p04046
Accepted
mod=10**9+7 h,w,a,b=map(int,input().split()) F,I=[0]*(h+w+2),[0]*(h+w+2) def inv(n): return pow(n,mod-2,mod) F[0],F[1],I[0],I[1]=1,1,1,1 for i in range(2,h+w+2): F[i]=i*F[i-1]%mod I[i]=inv(F[i]) def c(a,b): return F[a+b]*I[a]*I[b]%mod ans=0 for i in range(b+1,w+1): ans+=(c(h-a-1,i-1)*c(a-1,w-i))%mod print(ans%mod)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s303710844
p04046
Accepted
import operator as op from functools import reduce MODULUS = 10**9 + 7 if __name__ == '__main__': [h, w, a, b] = map(int, input().split()) # Taken from https://atcoder.jp/contests/abc042/submissions/3178794 for learning purposes. factorial = [0] * (h+w) factorial[0] = 1 factorial[1] = 1 inverse = [0] * (h+w) inverse[0] = 1 inverse[1] = 1 for i in range(2, h+w): factorial[i] = factorial[i-1] * i % MODULUS # フェルマーの小定理から(x! ** mod-2 % mod == x! ** -1 % mod) # powに第三引数入れると冪乗のmod付計算を高速にやってくれる inverse[i] = pow(factorial[i], MODULUS-2, MODULUS) # 組み合わせの数だけ返してくれる関数(自作) def ncr(n, r): # 10C7 = 10C3 r = min(r, n-r) # 分子の計算 numerator = factorial[n] # 分母の計算 denominator = inverse[r] * inverse[n-r] % MODULUS return numerator * denominator % MODULUS res = 0 for i in range(h-a): rights_and_downs_to_right_border_of_forbidden_area = i + (b-1) rights_and_downs_from_right_border_of_forbidden_area = (w - b - 1) + (h - i - 1) res = (res + ncr( rights_and_downs_to_right_border_of_forbidden_area, b-1 ) \ * ncr( rights_and_downs_from_right_border_of_forbidden_area, (h - i - 1) )) % MODULUS print(res)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s735263126
p04046
Accepted
H,W,A,B = map(int,input().split()) P=10**9+7 fac=[1]*(H+W) inv=[1]*(H+W) finv=[1]*(H+W) for n in range(2,H+W): fac[n]=fac[n-1]*n%P inv[n]=(-inv[P%n]*(P//n))%P finv[n]=finv[n-1]*inv[n]%P def comb(n,k):return ((fac[n]*finv[k]%P)*finv[n-k])%P ans=0 for i in range(1,min(W-B+1,H-A+1)): w=B+i-1 h=H-A-i ans=(ans+comb(w+h,w)*comb(W+H-w-h-2,W-w-1))%P print(ans)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s239425847
p04046
Accepted
H,W,A,B=map(int,input().split()) N=H+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 ans=0 for i in range(H-A): t=table[B-1+i]*rtable[i]*rtable[B-1] s=table[W+H-B-i-2]*rtable[H-i-1]*rtable[W-B-1] t=(t*s)%mod #print(t) ans+=t ans%=mod print(ans)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s569141799
p04046
Accepted
#Combinationクラス class Combination: 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 h, w, a, b = map(int,input().split()) MOD = 10**9 + 7 comb = Combination(h+w, MOD) ans = 0 for i in range(0, h-a): ans += comb.combination((b-1)+i,i) * comb.combination((w-b-1)+(h-i-1), w-b-1) ans = ans % MOD print(ans)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s478605185
p04046
Accepted
def read_input(): h, w, a, b = map(int, input().split()) return h, w, a, b # xの階乗 mod 10**9 + 7 について、1~nまでの結果を辞書にして返す def factorial_table(n): result = {0:1} curr = 1 acc = 1 modmax = (10**9) + 7 while curr <= n: acc *= curr acc %= modmax result[curr] = acc curr += 1 return result # xの逆元 mod 10**9 + 7を求める def reverse_mod(x): return power_n(x, 10**9 + 5) # xのn乗 mod 10**9 + 7を求める def power_n(x, n): r = 1 curr_a = x modmax = (10 ** 9) + 7 while n: if 1 & n: r *= curr_a r %= modmax n = n >> 1 curr_a *= curr_a curr_a %= modmax return r def comb(x, y, factorial_dic): return factorial_dic[x] * reverse_mod(factorial_dic[y]) * reverse_mod(factorial_dic[x - y]) def path(s, e, factorial_dic, modmax): x = e[0] - s[0] y = e[1] - s[1] return comb(x + y, x, factorial_dic) % modmax def submit(): h, w, a, b = read_input() f_dic = factorial_table(h + w - 2) count = 0 modmax = 10**9 + 7 for i in range(h - a): count += path((0, 0), (i, b - 1), f_dic, modmax) * path((i, b), (h - 1, w - 1), f_dic, modmax) count %= modmax print(count) if __name__ == '__main__': submit()
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s491412173
p04046
Accepted
h,w,a,b=map(int,input().split()) mod=10**9+7 fact=[1]*(h+w+1) for i in range(1,h+w+1): fact[i]=(fact[i-1]*i)%mod def cmb(a,b): return fact[a]*pow(fact[b],mod-2,mod)*pow(fact[a-b],mod-2,mod)%mod ans=0 for i in range(b,w): ans+=cmb(h-a+i-1,i)*cmb(w-1-i+a-1,w-i-1)%mod print(ans%mod)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s775764445
p04046
Accepted
from sys import exit H,W,A,B = [int(n) for n in input().split()] MOD = 10**9+7 NUM = 2*10**5 fact=[1]*(NUM+1) factinv=[1]*(NUM+1) for i in range(1,NUM+1): fact[i]=(fact[i-1]*i)%MOD factinv[NUM]=pow(fact[NUM],MOD-2,MOD) for i in range(NUM,0,-1): factinv[i-1]=(factinv[i]*i) % MOD ans=0 for i in range(B,W): tmp0 = fact[i+H-A-1] tmp0 *= factinv[H-A-1] tmp0 %= MOD tmp0 *= factinv[i] tmp0 %= MOD tmp1 = fact[W-i-1+A-1] tmp1 *= factinv[W-i-1] tmp1 %= MOD tmp1 *= factinv[A-1] tmp1 %= MOD ans += (tmp0 * tmp1) % MOD ans %= MOD print(ans)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s623347379
p04046
Accepted
U = 2*10**5 MOD = 10**9+7 fact = [1]*(U+1) fact_inv = [1]*(U+1) for i in range(1,U+1): fact[i] = (fact[i-1]*i)%MOD fact_inv[U] = pow(fact[U],MOD-2,MOD) for i in range(U,0,-1): fact_inv[i-1] = (fact_inv[i]*i)%MOD def comb(n,k): if k < 0 or k > n: return 0 x = fact[n] x *= fact_inv[k] x %= MOD x *= fact_inv[n-k] x %= MOD return x H,W,A,B = map(int,input().split()) # はじめてB+1列目に入る瞬間で場合分けして足す ans = 0 for i in range(H-A): # (0,0) to (i,B-1) x = comb(B-1+i,i) # (i,B) to (H-1,W-1) a = H-1-i b = W-1-B x *= comb(a+b,a) x %= MOD ans += x ans %= MOD print(ans)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s006880260
p04046
Accepted
h, w, a, b = [int(item) for item in input().split()] MOD = 10**9 + 7 n = h + w def mod_pow(x, n): ans = 1 while(n != 0): if n & 1: ans = ans * x % MOD x = x * x % MOD n = n >> 1 return ans fac = [1] + [0] * n fac_inv = [1] + [0] * n for i in range(1, n+1): fac[i] = fac[i-1] * (i) % MOD # Fermat's little theorem says # a**(p-1) mod p == 1 # then, a * a**(p-2) mod p == 1 # it means a**(p-2) is inverse element fac_inv[i] = fac_inv[i-1] * mod_pow(i, MOD-2) % MOD def mod_nCr(n, r): if n == 0 and r == 0: return 1 if n < r or n < 0: return 0 tmp = fac_inv[n-r] * fac_inv[r] % MOD return tmp * fac[n] % MOD if a+b > (h+w)//2: ans = 0 for i in range(h-a): ans += mod_nCr(b-1+i, i) * mod_nCr(w-b-1 + h-1-i, h-1-i) ans %= MOD else: ans = mod_nCr(h-1+w-1, h-1) for i in range(b): ans -= mod_nCr(h-a-1+i, i) * mod_nCr(a-1 + w-1-i, a-1) ans %= MOD print(ans)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s034278573
p04046
Accepted
import sys def comb(n, r, MOD, factrial, fact_inv): if n < 0 or r < 0 or n < r: return 0 else: return (factrial[n] * fact_inv[r] * fact_inv[n-r]) % MOD def main(): input = sys.stdin.readline H, W, A, B = map(int, input().split()) MOD = 10**9 + 7 n = H + W - 2 factrial = [1] * (n+1) for k in range(1, n+1): factrial[k] = (factrial[k-1] * k) % MOD fact_inv = [1] * (n+1) fact_inv[n] = pow(factrial[n], MOD - 2, MOD) for k in range(n-1, -1, -1): fact_inv[k] = (fact_inv[k+1] * (k+1)) % MOD ans = 0 for w in range(B+1, W+1): n_a = H - A + w -2 r_a = w - 1 a = comb(n_a, r_a, MOD, factrial, fact_inv) n_b = A - 1 + W - w r_b = A - 1 b = comb(n_b, r_b, MOD, factrial, fact_inv) ans += (a * b) % MOD return ans % MOD if __name__ == '__main__': print(main())
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s860364454
p04046
Accepted
from functools import reduce import math memo = [] memo_inv = [] MOD = 10**9+7 def get_factorial(i): return memo[i] def get_factorial_inv(i): return memo_inv[i] def nCr(n, r): return get_factorial(n) * get_factorial_inv(n-r) * get_factorial_inv(r) def main(): # 一文字のみを読み込み # s = input().rstrip().split(' ') # スペース区切りで標準入力を配列として読み込み # s = input().rstrip().split(' ') # 位置文字ずつ標準入力を配列として読み込み # s = list(input().rstrip()) slist = input().rstrip().split(' ') H = int(slist[0]) W = int(slist[1]) A = int(slist[2]) B = int(slist[3]) global memo memo = [1]*(H+W+1) f= 1 for i in range(1,H+W+1): f *=i f %=MOD memo[i] = f global memo_inv memo_inv = [1]*(H+W+1) memo_inv[H+W] = pow(memo[H+W],MOD-2,MOD) f_inv = memo_inv[H+W] for i in range(H+W-1,0,-1): f_inv *= i +1 f_inv %= MOD memo_inv[i] = f_inv ans = 0 for i in range(B,W): ans += nCr(i+H-A-1,i) * nCr(W-i-1+A-1,A-1) print(ans % MOD) if __name__ == '__main__': main()
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s526215955
p04046
Accepted
m=10**9+7 h,w,a,b=map(int,input().split()) d=c=1 for i in range(h-1): d=c=c*(w+h-b-2-i)*pow(i+1,m-2,m)%m for i in range(1,h-a): c=c*(b-1+i)*(h-i)*pow(i*(w+h-b-1-i),m-2,m)%m d+=c print(d%m)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s971022354
p04046
Accepted
def mod_inv(n: int, mod: int)->int: b, u, v = mod, 1, 0 while b > 0: t = n // b n -= t * b u -= t * v n, b = b, n u, v = v, u return (u+mod) % mod def comb(r: int, c: int, mod: int) -> int: if c < 0 or r < c: raise Exception('invalid r={}, c={}'.format(r, c)) if c == 0 or c == r: return 1 return (comb(r-1, c-1, mod) + comb(r-1, c, mod)) % mod # n+1Cr = (n+1)*n!/((n+1-r)!r!) = ((n+1)/(n+1-r)) * (n!/((n-r)!r!)) def iroha_and_grid(H: int, W: int, A: int, B: int) -> int: MOD = 10 ** 9 + 7 c1, c2 = 1, 1 uH, uW = H - A, B - 1 r = uH - 1 for n in range(r, (uH - 1) + (uW - 1) + 1): c1 = (c1 * (n + 1) * mod_inv(n + 1 - r, MOD)) % MOD lH, lW = A, W - B r = lH - 1 for n in range(r, (lH - 1) + (lW - 1) + 1): c2 = (c2 * (n + 1) * mod_inv(n + 1 - r, MOD)) % MOD ret = 0 for w in range(B, W): # 上の領域の高さと幅 uH, uW = H - A, w - 1 n = (uH - 1) + (uW - 1) + 1 r = uH - 1 c1 = (c1 * (n + 1) * mod_inv(n + 1 - r, MOD)) % MOD # 下の領域の高さと幅 lH, lW = A, W - w n = (lH - 1) + (lW - 1) r = lH - 1 c2 = (c2 * (n + 1 - r) * mod_inv(n + 1, MOD)) % MOD ret = (ret + c1 * c2) % MOD return ret if __name__ == "__main__": H, W, A, B = map(int, input().split()) ans = iroha_and_grid(H, W, A, B) print(ans)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s680029994
p04046
Accepted
from sys import stdin import sys sys.setrecursionlimit(1000000) mod = 10 ** 9 + 7 input = stdin.readline H, W, A, B = [int(x) for x in input().rstrip().split()] factorial = [1] for i in range(1, H+W): factorial.append(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, int(y/2)) ** 2 % mod else: return power(x, int((y-1)/2)) ** 2 * x % mod def C(n, r): return (((factorial[n] * x_inv[r]) % mod) * x_inv[n-r]) % mod # x_inv = [] # for i in range(H+W): # x_inv.append(power(factorial[i], mod-2)) x_inv = [0] * (H+W) x_inv[-1] = power(factorial[-1], mod-2) for i in range(H+W-2, -1, -1): x_inv[i] = x_inv[i+1] * (i+1) % mod sum = 0 for i in range(B, W): sum += C(i+H-A-1, i) * C(A+W-i-2, A-1) % mod print(sum%mod)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s032132084
p04046
Accepted
#!/usr/bin/env python3.4 # -*- coding: utf-8 -*- # arc058_b # input H, W, A, B = map(int, input().split()) mod = 1000000007 # fermat N = 0 fac = [1] inv = [1] def mypow(a, b): return _mypow(a, a, b) def _mypow(p, a, b): if b == 1: return p n = (p*p) % mod c, r = divmod(b, 2) if r == 0: return _mypow(n, a, c) else: ret = _mypow(n, a, c) * p return ret % mod def init_cmb(_N): global N, fac, inv N=_N fac = [1] for i in range(1, N+1): n = (fac[i-1] * i) % mod fac.append(n) inv = (N+1) * [1] inv[N] = mypow(fac[N], mod-2) for i in range(1, N)[::-1]: inv[i] = (inv[i+1] * (i+1)) % mod def cmb(n, r): if (r<0 or n<r): raise Exception(n, r) if n>N: raise Exception(n) ret = fac[n] * inv[r] * inv[n-r] return ret % mod init_cmb(100000*2) # getans def calc(h, w): h += -1 w += -1 return cmb(h+w, h) ans = 0 for y in range(1, H-A+1): left = calc(y, B) right = calc((H-y+1), (W-B)) ans += left*right ans %= mod # output print(ans)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s548073452
p04046
Accepted
H, W, A, B = list(map(int, input().split())) # 事前計算 + 演算ごとに mod する def solve(): # 組合せの計算用に階乗を事前計算しておく MOD = 10 ** 9 + 7 FACT = [0] * (H + W + 1) INV_FACT = [0] * (H + W + 1) # 逆元. 組合せの計算ごとにMODを取るため FACT[0], INV_FACT[0] = 1, 1 for i in range(1, (H + W + 1)): FACT[i] = (FACT[i - 1] * i) % MOD INV_FACT[i] = pow(FACT[i], MOD - 2, MOD) # フェルマーの小定理を使う def mod_nCr(n, r): if n < r or n == 0 or r == 0: return 1 return FACT[n] * (INV_FACT[r] * INV_FACT[n - r]) % MOD # print(H, W, A, B) # 座標は(x, y). startは(0, 0) goal = W - 1, H - 1 # 境界点: 移動できないエリアの右上の点 nx = B - 1 ny = H - A # 境界点から右斜め上に1つ移動した点(choke_left)から, 右端へ伸びる線がchoke points choke_left = nx + 1, ny - 1 choke_points = [] for i in range(B, W): choke_points.append((i, choke_left[1])) # 組合せを計算する: startからcp1, cp2からgoalまで組合せを掛ける ans = 0 for cp1 in choke_points: x, y = cp1 from_start_to_cp1 = mod_nCr(x + y, x) # cp1から1つ下の点がcp2 y += 1 dx, dy = goal[0] - x, goal[1] - y from_cp2_to_goal = mod_nCr(dx + dy, dx) ans = ans % MOD + (from_start_to_cp1 * from_cp2_to_goal % MOD) % MOD return ans % MOD res = solve() print(res)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s671135255
p04046
Accepted
import math H, W, A, B = map(int, input().split()) MOD = 10 ** 9 + 7 factorial = [1] inv_factorial = [1] for i in range(1, H+W+1): factorial.append(factorial[-1] * i % MOD) inv_factorial.append(pow(factorial[-1], MOD-2, MOD)) def f(h, w): return (factorial[h+w-2] * inv_factorial[h-1] * inv_factorial[w-1]) % MOD answer = 0 for i in range(B+1, W+1): answer += f(H-A, i) * f(A, W-i+1) answer %= MOD print(int(answer))
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s960498477
p04046
Accepted
# coding: UTF-8 import numpy as np MOD = int(1e+9+7) H, W, A, B = map(int, input().split()) fact = [1] for i in range(1, W+H-1): fact.append((fact[-1]*i) % MOD) ifact = [pow(fact[-1], MOD-2, MOD)] for i in reversed(range(1, W+H-1)): ifact.append((ifact[-1]*i) % MOD) ifact.reverse() def comb(n, r): return fact[n]*ifact[n-r]*ifact[r] r = H-A-1 s = 0 for c in range(B, W): s += comb(c+r, r) * comb(W-c+A-2, A-1) s %= MOD print(int(s))
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s473049594
p04046
Accepted
import sys from collections import deque import copy import bisect import math def get_read_func(fileobject): if fileobject == None : return raw_input else: return fileobject.readline def power_func(a,b,p): if b==0: return 1 if b%2==0: d=power_func(a,b//2,p) return d*d %p if b%2==1: return (a*power_func(a,b-1,p ))%p class comb_calclator(): def __init__(self, N, p): self.N = N self.p = p self.frac_N = [0 for i in range(N + 1)] self.frac_N[0] = 1L self.comb_N = {} for i in range(1, N + 1): self.frac_N[i] = (self.frac_N[i - 1] * i ) % p def comb(self, n, r): if n<0 or r<0 or n<r: return 0 if n==0 or r==0: return 1 if (n, r) in self.comb_N: return self.comb_N[(n, r)] a = self.frac_N[n] b = self.frac_N[r] c = self.frac_N[n - r] self.comb_N[(n, r)] = (a*power_func(b,self.p-2,self.p)*power_func(c,self.p-2, self.p))% self.p self.comb_N[(n, n - r)] = self.comb_N[(n, r)] return self.comb_N[(n, r)] def main(): if len(sys.argv) > 1: f = open(sys.argv[1]) else: f = None read_func = get_read_func(f); input_raw = read_func().strip().split() [H, W, A, B] = [int(input_raw[0]), int(input_raw[1]), int(input_raw[2]), int(input_raw[3])] p = 1000000007 cmb = comb_calclator(H + W, p) count = 0L for i in range(H - A): count = (count + cmb.comb(B - 1 + i, i) * cmb.comb((W - B - 1) + (H - 1) - i, (H - 1) - i)) % 1000000007 print count if __name__ == '__main__': main()
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s428810004
p04046
Accepted
h, w, a, b = map(int, input().split()) MOD = 10 ** 9 + 7 class ModCmb: def __init__(self, size): self.inv = [1] * (size + 1) self.fact = [1] * (size + 1) temp_inv = [1] * (size + 1) for i in range(2, size + 1): temp_inv[i] = ( -(MOD // i) * temp_inv[MOD%i] ) % MOD for i in range(2, size + 1): self.fact[i] = self.fact[i-1] * i % MOD self.inv[i] = self.inv[i-1] * temp_inv[i] % MOD def cmb(self, n, r): #print(n, r) if n <= 1 or n == r: return 1 elif r == 1: return n res = (self.fact[n] * self.inv[r] % MOD ) * self.inv[n-r] % MOD #print(res) return res c = ModCmb(h+w) ans = 0 x = h-a-1 y = w-b-1 for i in range(h-a): j = h+w-2-i-b #print(i, j) #print(i+b-1, b-1, j, w-b-1) ans += c.cmb(i+b-1, b-1) * c.cmb(j, w-b-1) ans %= MOD print(ans % MOD)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s602098093
p04046
Accepted
#!usr/bin/env python3 from collections import defaultdict from heapq import heappush, heappop import sys import math import bisect import random def LI(): return list(map(int, sys.stdin.readline().split())) def I(): return int(sys.stdin.readline()) def LS(): return list(map(list, sys.stdin.readline().split())) def S(): return list(sys.stdin.readline())[:-1] def IR(n): return [I() for i in range(n)] def LIR(n): return [LI() for i in range(n)] def SR(n): return [S() for i in range(n)] def LSR(n): return [LS() for i in range(n)] mod = 1000000007 #A """ def check(n): n = list(map(int,list(str(n)))) s = 0 for i in n: s += f[i] return s == 0 n,k = LI() d = LI() f = [0 for i in range(10)] for i in d: f[i] = 1 while 1: if check(n): print(n) quit() n += 1 """ #B def f(x,y): return fact[x-1+h-y]*fact[y-1+w-x]*inv_fact[x-1]*inv_fact[y-1]*inv_fact[w-x]*inv_fact[h-y]%mod h,w,a,b = LI() fact = [1]*(h+w+1) for i in range(h+w): fact[i+1] = fact[i]*(i+1)%mod inv_fact = [0]*(h+w+1) inv_fact[h+w] = pow(fact[-1],mod-2,mod) for i in range(h+w)[::-1]: inv_fact[i] = inv_fact[i+1]*(i+1)%mod ans = 0 for i in range(1,min(w-b,h-a)+1): ans += f(b+i,a+i) ans %= mod print(ans) #C #D #E #F #G #H #I #J #K #L #M #N #O #P #Q #R #S #T
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s727928694
p04046
Accepted
#!usr/bin/env python3 from collections import defaultdict from heapq import heappush, heappop import sys import math import bisect import random def LI(): return list(map(int, sys.stdin.readline().split())) def I(): return int(sys.stdin.readline()) def LS(): return list(map(list, sys.stdin.readline().split())) def S(): return list(sys.stdin.readline())[:-1] def IR(n): return [I() for i in range(n)] def LIR(n): return [LI() for i in range(n)] def SR(n): return [S() for i in range(n)] def LSR(n): return [LS() for i in range(n)] mod = 1000000007 #A """ def check(n): n = list(map(int,list(str(n)))) s = 0 for i in n: s += f[i] return s == 0 n,k = LI() d = LI() f = [0 for i in range(10)] for i in d: f[i] = 1 while 1: if check(n): print(n) quit() n += 1 """ #B def f(x,y): return fact[x-1+h-y]*fact[y-1+w-x]*inv_fact[x-1]*inv_fact[y-1]*inv_fact[w-x]*inv_fact[h-y]%mod h,w,a,b = LI() fact = [1]*(h+w+1) for i in range(h+w): fact[i+1] = fact[i]*(i+1)%mod inv_fact = [0]*(h+w+1) inv_fact[h+w] = pow(fact[-1],mod-2,mod) for i in range(h+w)[::-1]: inv_fact[i] = inv_fact[i+1]*(i+1)%mod ans = 0 for i in range(1,min(w-b,h-a)+1): ans += f(b+i,a+i) ans %= mod print(ans) #C #D #E #F #G #H #I #J #K #L #M #N #O #P #Q #R #S #T
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s921971259
p04046
Accepted
#!usr/bin/env python3 from collections import defaultdict from heapq import heappush, heappop import sys import math import bisect import random def LI(): return list(map(int, sys.stdin.readline().split())) def I(): return int(sys.stdin.readline()) def LS(): return list(map(list, sys.stdin.readline().split())) def S(): return list(sys.stdin.readline())[:-1] def IR(n): return [I() for i in range(n)] def LIR(n): return [LI() for i in range(n)] def SR(n): return [S() for i in range(n)] def LSR(n): return [LS() for i in range(n)] mod = 1000000007 #A """ def check(n): n = list(map(int,list(str(n)))) s = 0 for i in n: s += f[i] return s == 0 n,k = LI() d = LI() f = [0 for i in range(10)] for i in d: f[i] = 1 while 1: if check(n): print(n) quit() n += 1 """ #B h,w,a,b = LI() ans = 0 k = 1 inv = [pow(i,mod-2,mod) for i in range(h+w+1)] x = b+1 y = a+1 for i in range(min(x-1,h-y)): k *= (x-1+h-y-i)*inv[i+1]%mod k %= mod for i in range(min(y-1,w-x)): k *= (y-1+w-x-i)*inv[i+1]%mod k %= mod for i in range(1,min(w-b,h-a)+1): ans += k ans %= mod k *= (h-y)*(w-x)*inv[x]*inv[y]%mod k %= mod x += 1 y += 1 print(ans) #C #D #E #F #G #H #I #J #K #L #M #N #O #P #Q #R #S #T
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s762939325
p04046
Accepted
H,W,A,B = map(int, input().split()) mod = 10**9+7 fact = [1] for i in range(1, H+W+1): fact.append ( fact[-1] * i % mod) inv_fact = [0] * (H+W+1) inv_fact[H+W] = pow(fact[H+W], mod-2, mod) for i in range(H+W, 1, -1): inv_fact[i-1] = inv_fact[i] * i % mod inv_fact[0] = 1 def C(a,b): if a <= 0 or b < 0: return 0 if a==0 or b == 0: return 1 return fact[a] * inv_fact[b] * inv_fact[a-b] % mod ans = 0 for w in range(B+1, W+1): h = H-A - (w-B-1) if w == 0 or h == 0: break ans = (ans + C(h+w-2, w-1) * C(H+W-h-w, W-w) % mod) % mod print(ans)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s433747861
p04046
Accepted
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 modfunctional = [1]*(2*10**5+1) modinv = getInv(10**5+1) modinvfunctional = [1]*(10**5+1) for i in range(10**5): modinvfunctional[i+1] = (modinvfunctional[i]*modinv[i+1])%Q for i in range(2*10**5): modfunctional[i+1] = (modfunctional[i]*(i+1))%Q H, W, A, B = map( int, input().split()) ans = 0 for i in range(B+1, W+1): ans += (modfunctional[i+H-A-2]*modinvfunctional[i-1]*modinvfunctional[H-A-1]%Q)*(modfunctional[A+W-i-1]*modinvfunctional[A-1]*modinvfunctional[W-i]%Q)%Q ans %= Q print(ans)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s415150880
p04046
Accepted
class Factorial: def __init__(self,n,mod): self.f=[1] for i in range(1,n+1): self.f.append(self.f[-1]*i%mod) self.i=[pow(self.f[-1],mod-2,mod)] for i in range(1,n+1)[::-1]: self.i.append(self.i[-1]*i%mod) self.i.reverse() def factorial(self,i): return self.f[i] def ifactorial(self,i): return self.i[i] def comb(self,n,k): return self.f[n]*self.i[n-k]%mod*self.i[k]%mod mod=10**9+7 h,w,a,b=map(int,input().split()) f=Factorial(h+w,mod) ans=0 for i in range(b,w): ans=(ans+f.comb(h-a-1+i,i)*f.comb(a-1+w-i-1,w-i-1))%mod print(ans)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s214656036
p04046
Accepted
h,w,a,b=map(int,input().split()) mod=10**9+7 n=h+w+1 fc,inv=[1]*n,[1]*n for i in range(1,n): fc[i]=i*fc[i-1]%mod inv[n-1]=pow(fc[n-1],mod-2,mod) for i in range(n-1,0,-1): inv[i-1]=inv[i]*i%mod f=lambda a,b:fc[a+b]*inv[a]*inv[b]%mod v=0 for i in range(b,w): v+=f(h-a-1,i)*f(a-1,w-i-1)%mod print(v%mod)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s491323396
p04046
Accepted
H, W, A, B = map(int, input().split()) mod = 10 ** 9 + 7 # 階乗 & 逆元計算 factorial = [1] inverse = [1] for i in range(1, H + W + 1): factorial.append(factorial[-1] * i % mod) inverse.append(pow(factorial[-1], mod-2, mod)) # 組み合わせ計算 def nCr(n, r): if n < r or r < 0: return 0 elif r == 0: return 1 return factorial[n] * inverse[r] * inverse[n - r] % mod ans = 0 for w in range(B, W): ans += nCr(H - A - 1 + w, w) * nCr(A - 1 + W - w - 1, A - 1) % mod print(ans % mod)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s817940056
p04046
Accepted
H, W, A, B = map(int, input().split()) mod = 10 ** 9 + 7 ans = 0 X = [i for i in range(H + W + 1)] X[0] = 1 for i in range(2, H + W + 1): X[i] = X[i - 1] * i % mod Y = X.copy() Y[-1] = pow(Y[-1], mod - 2, mod) for i in range(H + W, 1, -1): Y[i - 1] = i * Y[i] % mod def combination(x, y): return X[x] * Y[y] * Y[x - y] % mod for i in range(B, W): ans += (combination(H - A - 1 + i, i) * combination(A - 1 + W - i - 1, W - i -1)) ans %= mod print(ans)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s160453898
p04046
Accepted
H, W, A, B = [int(_) for _ in input().split()] mod = 10**9 + 7 X = [i for i in range(H + W + 1)] X[0] = 1 for i in range(2, H + W + 1): X[i] = X[i - 1] * i % mod Y = X.copy() Y[-1] = pow(Y[-1], mod - 2, mod) for i in range(H + W, 1, -1): Y[i - 1] = i * Y[i] % mod def comb(x, y): return X[x] * Y[y] * Y[x - y] % mod ans = 0 for i in range(B, min(B + H - A, W)): ans += comb(H - A + B - 1, i) * comb(W + A - B - 1, W - i - 1) ans %= mod print(ans) # http://arc058.contest.atcoder.jp/data/arc/058/editorial.pdf
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s524441006
p04046
Accepted
h, w, a, b = map(int, input().split()) mod = 10 ** 9 + 7 MAX_N = 2 * 10 ** 5 factorial = [1] * MAX_N #事前に階上テーブルを用意 def calc_factorial(): for i in range(1, MAX_N): factorial[i] = i * factorial[i - 1] % mod def comb(n, k): a = factorial[n] % mod b = (factorial[k] * factorial[n - k]) % mod b_ = pow(b, mod - 2, mod) return (a * b_) % mod # 階乗を用意 calc_factorial() all = comb(h + w - 2, h - 1) sub = 0 for j in range(1, b + 1): sub += comb(h - a - 1 + j - 1, j - 1) * comb(a - 1 + w - j, w - j) sub = sub % mod if all - sub <= 0: print(all - sub + mod) else: print(all - sub)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s971526659
p04046
Accepted
H, W, A, B = map(int, input().split()) h = {} p = 10**9+7 q = p - 2 for x in range(H+W-1): if h.get(x-1): a = (x * h[x-1][0]) % p else: a = 1 b = pow(a,q,p) h[x] = (a,b) def C(n, r): return h[n][0] * h[r][1] * h[n-r][1] ans = 0 for i in range(B, W): ans += C(i+H - A - 1, i) * C(W - 1 - i + A - 1, W - 1 - i) ans %= p print(ans % p)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s750423043
p04046
Accepted
H, W, A, B = map(int, input().split()) h = {} p = 10**9+7 q = p - 2 for x in range(H+W-1): if h.get(x-1): a = (x * h[x-1][0]) % p else: a = 1 b = pow(a,q,p) h[x] = (a,b) def C(n, r): return h[n][0] * h[r][1] * h[n-r][1] ans = 0 for i in range(B, W): ans += C(i+H - A - 1, i) * C(W - 1 - i + A - 1, W - 1 - i) print(ans % p)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s922921200
p04046
Accepted
mod = 10**9+7 h, w, a, b = map(int, input().split()) n = h+w-2 ans = 0 fac = [1]*(n+1) inv = [1]*(n+1) for i in range(1, n+1): fac[i] = i*fac[i-1] % mod for i in range(1, n+1): inv[i] = pow(fac[i], mod-2, mod) def func(x, y): m = fac[x+y]*inv[x]*inv[y]%mod return m for x in range(b, w): ans += func(x, h-a-1)*func(w-1-x, a-1) ans %= mod print(ans)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s756476304
p04046
Accepted
H,W,A,B = map(int,input().split()) def init_fact(n,mod): fact,finv,inv = [1]*n,[1]*n,[1]*n for i in range(2,n): fact[i] = (fact[i-1]*i) % mod inv[i] = mod - inv[mod%i] * (mod//i)%mod finv[i] = finv[i-1] * inv[i] % mod return (fact,finv,inv) def nCr(n,r,mod,fact,finv): if n<r: return 0 else: return fact[n] * (finv[r] * finv[n-r] % mod) % mod modulo = 10**9+7 fact,finv,inv = init_fact(200000,modulo) ans = 0 for i in range(H-A): ans += nCr(i+B-1,i,modulo,fact,finv) * nCr(H-i-1+W-B-1,W-B-1,modulo,fact,finv) ans %= modulo print(ans)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s843656330
p04046
Accepted
H, W, A, B = map(int, input().split()) answer = 0 mod = 1000000007 factorial = [1] for n in range(1, H+W): factorial.append(factorial[n-1]*n%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: a = power(x, y//2) ** 2 return a * x % mod inverseFactorial = [0] * (H+W) inverseFactorial[H+W-1] = power(factorial[H+W-1], mod-2) for n in range(H+W-2, -1, -1): inverseFactorial[n] = inverseFactorial[n+1] * (n+1) % mod def combi(n, m): return factorial[n] * inverseFactorial[m] *inverseFactorial[n-m]%mod for i in range(B+1, W+1): answer = (answer + combi(H-A-2+i, i-1)*combi(A+W-i-1,W-i)) % mod print(answer)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s695264505
p04046
Accepted
H, W, A, B = (int(i) for i in input().split()) def power(a, b): if b == 0: return 0 elif b == 1: return a % 1000000007 elif b % 2 == 0: return (power(a, b//2) ** 2) % 1000000007 else: return (power(a, b//2) ** 2 * a) % 1000000007 def divide(a, b): return (a * power(b, 1000000005)) % 1000000007 #階乗の逆元を求める fac_lim = 300000 fac = [None]*(fac_lim+1) fac[0] = 1 for i in range(fac_lim): fac[i+1] = fac[i] * (i+1) fac[i+1] = fac[i + 1] % 1000000007 fac_inv = [None]*(fac_lim+1) fac_inv[fac_lim] = power(fac[fac_lim], 1000000005) for i in range(fac_lim, 0, -1): fac_inv[i-1] = (fac_inv[i] * i) % 1000000007 def conv(a, b): return (fac[a] * fac_inv[a-b] * fac_inv[b]) % 1000000007 res = conv(H+W-2, H-1) for i in range(A): res -= conv(H-A+i + B-1, B-1) * conv(A-1-i + W-B-1, W-B-1) res = res % 1000000007 print(res)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s442498448
p04046
Accepted
H, W, A, B = map(int, input().split()) ans = 0 MOD = 10**9 + 7 N = H + W - 2 fac = [1] * (N+1) inv = [1] * (N+1) # 階乗 for i in range(1, N+1): fac[i] = i * fac[i - 1] % MOD # 普通の逆元テーブル for i in range(1, N+1): inv[i] = pow(fac[i], MOD-2, MOD) def f(x, y): ans = fac[x + y] * inv[x] * inv[y] % MOD return ans for x in range(B, W): ans += f(x, H - A - 1) * f(W - 1 - x, A - 1) ans %= MOD print(ans % MOD)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s090395446
p04046
Accepted
H, W, A, B = map(int, input().split()) ans = 0 MOD = 10**9 + 7 N = H + W - 2 fac = [1] * N inv = [1] * N # 階乗 for i in range(1, N): fac[i] = (fac[i - 1] * i) % MOD # 高速な逆元テーブル 最初にN-1を求めておくことであとはiをかけるだけで残りの逆元が算出できる inv[N - 1] = pow(fac[N - 1], MOD - 2, MOD) for i in range(N - 1, 0, -1): inv[i - 1] = (inv[i] * i) % MOD def f(x, y): ans = fac[x + y] * inv[x] * inv[y] % MOD return ans for x in range(B, W): ans += f(x, H - A - 1) * f(W - 1 - x, A - 1) ans %= MOD print(ans % MOD)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s717382803
p04046
Accepted
H, W, A, B = map(int, input().split()) ans = 0 MOD = 10**9 + 7 N = H + W - 2 fac = [1] * N inv = [1] * N # 階乗 for i in range(1, N): fac[i] = (fac[i - 1] * i) % MOD # 逆元 inv[N - 1] = pow(fac[N - 1], MOD - 2, MOD) for i in range(N - 1, 0, -1): inv[i - 1] = (inv[i] * i) % MOD def f(x, y): ans = fac[x + y] * inv[x] * inv[y] % MOD return ans for x in range(B, W): ans += f(x, H - A - 1) * f(W - 1 - x, A - 1) ans %= MOD print(ans % MOD)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s237165575
p04046
Accepted
H,W,A,B=map(int,input().split()) mod=10**9+7 n=H+W INV=[None]*(n+1)#1/aのリストを予め作っておく. for i in range(1,n+1): INV[i]=pow(i,mod-2,mod) k=H+W-2 n=H-1 Combi1=[None]*(k+1)#Combi[i]=iCnを表す.kは必要な分だけ. Combi1[n]=1 for i in range(n+1,k+1): Combi1[i]=Combi1[i-1]*i*INV[i-n] %mod k=H+B-2 n=B-1 Combi2=[None]*(k+1)#Combi[i]=iCnを表す.kは必要な分だけ. Combi2[n]=1 for i in range(n+1,k+1): Combi2[i]=Combi2[i-1]*i*INV[i-n] %mod k=W-B-1+A n=W-B-1 Combi3=[None]*(k+1)#Combi[i]=iCnを表す.kは必要な分だけ. Combi3[n]=1 for i in range(n+1,k+1): Combi3[i]=Combi3[i-1]*i*INV[i-n] %mod ANS=Combi1[H+W-2] for x in range(H-A,H): ANS=(ANS-Combi2[x+B-1]*Combi3[(H-x-1)+W-B-1])%mod print(ANS)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s914502816
p04046
Accepted
mod=10**9+7 H,W,A,B=map(int,input().split()) Factorial=[1]*(H+W+1) for i in range(1,H+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]*(H+W+1) inverseFactorial[H+W]=power(Factorial[H+W],mod-2) for i in range(0,H+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 ans=0 for a in range(0,H-A): ans=(ans+comb(B+a-1,a)*comb(W-B+H-a-2,H-a-1))%mod print(ans)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s961135878
p04046
Accepted
K = 10 ** 9 + 7 def pow_K(x, n): if n == 0: return 1 else: return (pow_K(x, n // 2) ** 2 * x ** (n % 2)) % K H, W, A, B = map(int, input().split()) fact = [0 for i in range(H+W-1)] fact[0] = 1 for i in range(H+W-2): fact[i + 1] = fact[i] * (i+1) % K fact_inv = [0 for i in range(H+W-1)] fact_inv[H+W-2] = pow_K(fact[H+W-2], K-2) for i in range(H+W-2, 0, -1): fact_inv[i-1] = fact_inv[i] * i % K r = 0 for i in range(B, W): r += ((fact[H-A-1+i] * fact_inv[H-A-1] %K)*fact_inv[i] % K) * ((fact[W-1-i+A-1] * fact_inv[W-1-i] %K) * fact_inv[A-1] % K) %K r %= K print(r)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s613130251
p04046
Accepted
H, W, A, B = map(int, input().split()) M = 10 ** 5 + 5 M *= 2 fact = [0] * M rfact = [0] * M fact[0] = 1 MOD = 10 ** 9 + 7 for i in range(1, M): fact[i] = fact[i - 1] * i % MOD 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) % MOD def comb(n, k): return fact[n] * rfact[n - k] * rfact[k] % MOD num = 0 for i in range(B + 1, W + 1): num += comb(H - (A + 1) + i - 1, i - 1) * comb(W - i + A - 1, A - 1) num %= MOD print(num)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s790245245
p04046
Accepted
def mod_inv(inv,a,m): i=a if inv.get(a,0)!=0: return inv[a] e=m-2 tmp=1 while e!=1: if e%2==1: tmp*=a tmp%=m a*=a a%=m e//=2 a*=tmp a%=m inv[i]=a return a def factrial(fact,a,m): for i in range(1,a+1): fact[i]=fact[i-1]*i%mod def comb(fact,inv,a,b,m): return (fact[a+b]*mod_inv(inv,fact[a],m)*mod_inv(inv,fact[b],m))%m H,W,A,B=list(map(int,input().split())) mod=10**9+7 fact=[1 for _ in range(H+W+1)] inv={} factrial(fact,H+W,mod) ans=0 for i in range(W-B): ans+=comb(fact,inv,B+i,H-A-1,mod)*comb(fact,inv,W-B-1-i,A-1,mod) ans%=mod print(ans)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s902637464
p04046
Accepted
import math MOD = 10 ** 9 + 7 H, W, A, B = map(int, input().split()) maxf = H + W f, invf = [1] * maxf, [1] * maxf for i in range(1, maxf): f[i] = i * f[i - 1] % MOD invf[maxf - 1] = pow(f[maxf - 1], MOD - 2, MOD) for i in range(maxf - 1, 0, -1): invf[i - 1] = invf[i] * i % MOD comb = lambda a, b: f[a + b] * invf[a] * invf[b] % MOD ans = 0 for x in range(B, W): ans += comb(x, H - A - 1) * comb(W - 1 - x, A - 1) % MOD print(ans % MOD)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s233413978
p04046
Accepted
def gcd(a, b): while b: a, b = b, a % b return a def egcd(a, b): (x, lastx) = (0, 1) (y, lasty) = (1, 0) while b != 0: q = a // b (a, b) = (b, a % b) (x, lastx) = (lastx - q * x, x) (y, lasty) = (lasty - q * y, y) return (lastx, lasty, a) # ax ≡ 1 (mod m) def modinv(a, m): (inv, q, gcd_val) = egcd(a, m) return inv % m N = 10**9+7 R = 10**5*2 modfact = [0]*(R+1) def modfact_memo(n,N): r = 1 modfact[0] = 1 for i in range(1,n+1): r = r % N * i % N modfact[i] = r modfact_memo(R,N) # ax ≡ 1 (mod m) modinv=[0]*(R+1) def modinv_memo(a,m): for i in range(a+1): j = modfact[i] (inv, q, gcd_val) = egcd(j, m) modinv[i] = inv % m modinv_memo(R,N) def modconb(n,r,N): p = modfact[n] q1 = modinv[r] q2 = modinv[n-r] return p*q1%N*q2%N H,W,A,B = map(int,input().split()) ans = 0 for r in range(B,W): ans += modconb(H-A-1+r,r,N)*modconb(A-1+W-r-1,A-1,N) ans %= N print(ans)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s106203428
p04046
Accepted
from collections import defaultdict, Counter from itertools import product, groupby, count, permutations, combinations from math import pi, sqrt from collections import deque from bisect import bisect, bisect_left, bisect_right from string import ascii_lowercase from functools import lru_cache import sys sys.setrecursionlimit(10000) INF = float("inf") YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no" dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0] dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1] def inside(y, x, H, W): return 0 <= y < H and 0 <= x < W def ceil(a, b): return (a + b - 1) // b class Combination: def __init__(self, n, mod): assert 0 < n self.mod = mod self.fact = [1] * (n + 1) self.inv = [1] * (n + 1) for i in range(1, len(self.fact)): self.fact[i] = (i * self.fact[i - 1]) % self.mod self.inv[i] = pow(self.fact[i], self.mod - 2, self.mod) def nCr(self, n, k): if n == 0 and k == 0: return 1 assert 0 <= n < len(self.fact) and 0 <= k a = self.fact[n] b = (self.inv[k] * self.inv[n - k]) % self.mod return a * b % self.mod def main(): H, W, A, B = map(int, input().split()) MOD = 10 ** 9 + 7 comb = Combination(100000 * 2 + 10, MOD) ans = 0 for i in range(B + 1, W + 1): h, w = H - A - 1, i - 1 s = comb.nCr(h + w, w) % MOD h, w = A - 1, W - i t = comb.nCr(h + w, w) % MOD ans += (s * t) % MOD ans %= MOD print(ans) if __name__ == '__main__': main()
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s946115370
p04046
Accepted
MAX = 510000 MOD = 1000000007 fac = [0] * MAX finv = [0] * MAX inv = [0] * MAX def comb_init(): fac[0] = fac[1] = 1 finv[0] = finv[1] = 1 inv[1] = 1 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 comb(n, r): if n < r: return 0 if n < 0 or r < 0: return 0 return fac[n] * (finv[r] * finv[n - r] % MOD) % MOD def solve(): ans = 0 an = H + B - A - 1 ar = B bn = W + A - B - 2 br = W - B - 1 for i in range(W-B): a = comb(an, ar) b = comb(bn, br) ans += (a * b) % MOD an += 1; ar += 1 bn -= 1; br -= 1 return ans % MOD H, W, A, B = map(int, input().split()) comb_init() print(solve())
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s417702275
p04046
Accepted
iH,iW,iA,iB = [int(x) for x in input().split()] iD = 10**9+7 #法 iLBoxW=iB-1 iLBoxH=iH-iA-1 iRBoxW=iW-iB-1 iRBoxH=iH-1 #iMax = iH+iW-2 iMax = max(iLBoxW+iLBoxH,iRBoxW+iRBoxH) #nCr = n!/r!(n-r)! #二分累乗法 iDを法として def fBiPow(iX,iN,iD): iY = 1 while iN > 0: if iN % 2 == 0: iX = iX * iX % iD iN = iN // 2 else: iY = iX * iY % iD iN = iN - 1 return iY #階乗(iDを法とした)の配列 aM = [1]*(iMax+1) for i in range(1,iMax+1): aM[i]=aM[i-1]*i %iD #各階乗のiDを法とした逆元の配列 aInvM = [1]*(iMax+1) aInvM[iMax] = fBiPow(aM[iMax],iD-2,iD) for i in range(iMax,0,-1): aInvM[i-1]=aInvM[i]*i % iD iRet = 0 for iL in range(0,iLBoxH+1): iRet += (aM[iL+iLBoxW]*aInvM[iL]*aInvM[iLBoxW]) % iD * (aM[iRBoxW+iRBoxH-iL]*aInvM[iRBoxW]*aInvM[iRBoxH-iL])%iD iRet %= iD iRet %= iD print(iRet)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s850465721
p04046
Accepted
iH,iW,iA,iB = [int(x) for x in input().split()] iD = 10**9+7 #法 #初等整数論勉強して解説通りキッチリ実装したった #二分累乗法 iDを法として def fBiPow(iX,iN,iD): iY = 1 while iN > 0: if iN % 2 == 0: iX = iX * iX % iD iN = iN // 2 else: iY = iX * iY % iD iN = iN - 1 return iY #nCr = n!/r!(n-r)! #ループ回すだけ版や def fFr(iX,iR=1): if iR == 0 or iX == 0: return 1 else : iRet = 1 for i in range(iR,iX+1): iRet *= i return iRet def fnCr(iN,iR): if iR == 0: return 1 else: return fFr(iN,iR+1) // fFr(iN-iR) #階乗(iDを法とした)の配列 aM = [1]*(iH+iW-1) for i in range(1,iH+iW-1): aM[i]=aM[i-1]*i %iD #各階乗のiDを法とした逆元の配列 aInvM = [1]*(iH+iW-1) aInvM[iH+iW-2] = fBiPow(aM[iH+iW-2],iD-2,iD) for i in range(iH+iW-2,0,-1): aInvM[i-1]=aInvM[i]*i % iD iRet = 0 for iL in range(0,iH-iA): #iRet += (fnCr(iL+iB-1,iL) * fnCr(iW-iB+iH-iL-2,iW-iB-1))%iD iRet += (aM[iL+iB-1]*aInvM[iL]*aInvM[iB-1]) % iD * (aM[iW-iB+iH-iL-2]*aInvM[iW-iB-1]*aInvM[iH-iL-1])%iD iRet %= iD iRet %= iD print(iRet)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s976535576
p04046
Accepted
# -*- coding: utf-8 -*- H,W,A,B = map(int, input().split()) mod = (10 ** 9 + 7) # 予め組み合わせ計算に必要な階乗と逆元のテーブルを作っておく factorial = [1] * (H+W) factorial[0] = 1 factorial[1] = 1 for i in range(2,H+W): factorial[i] = factorial[i-1] * i % mod inverse = [1] * (H+W) # フェルマーの小定理から(x! ** mod-2 % mod == x! ** -1 % mod) # powに第三引数入れると冪乗のmod付計算を高速にやってくれる inverse[H+W-1] = pow(factorial[H+W-1], mod-2, mod) for i in range(H+W-2, 0, -1): # 最後から戻っていくこのループならH+W回powするより処理が速い inverse[i] = inverse[i+1] * (i+1) % mod # 組み合わせの数だけ返してくれる関数(自作) def nCr(n, r): # 10C7 = 10C3 r = min(r, n-r) # 分子の計算 numerator = factorial[n] # 分母の計算 denominator = inverse[r] * inverse[n-r] % mod return numerator * denominator % mod h = H - A w = B + 1 ans = 0 # マスを右上に1つずつずらして、必ず通る場所でパターンを足し合わせていく while h > 0 and w <= W: ans = (ans + nCr(h+w-2, h-1) * nCr(H-h+W-w, H-h)) % mod h -= 1 w += 1 print(ans)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s668782399
p04046
Accepted
# -*- coding: utf-8 -*- H,W,A,B = map(int, input().split()) mod = (10 ** 9 + 7) # 予め組み合わせ計算に必要な階乗と逆元のテーブルを作っておく factorial = [0] * (H+W) factorial[0] = 1 factorial[1] = 1 inverse = [0] * (H+W) inverse[0] = 1 inverse[1] = 1 for i in range(2,H+W): factorial[i] = factorial[i-1] * i % mod # フェルマーの小定理から(x! ** mod-2 % mod == x! ** -1 % mod) # powに第三引数入れると冪乗のmod付計算を高速にやってくれる inverse[i] = pow(factorial[i], mod-2, mod) # 組み合わせの数だけ返してくれる関数(自作) def nCr(n, r): # 10C7 = 10C3 r = min(r, n-r) # 分子の計算 numerator = factorial[n] # 分母の計算 denominator = inverse[r] * inverse[n-r] % mod return numerator * denominator % mod h = H - A w = B + 1 ans = 0 # マスを右上に1つずつずらして、必ず通る場所でパターンを足し合わせていく while h > 0 and w <= W: ans = (ans + nCr(h+w-2, h-1) * nCr(H-h+W-w, H-h)) % mod h -= 1 w += 1 print(ans)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s565161318
p04046
Accepted
h,w,a,b=map(int, input().split()) n=h+w-2 mod=10**9+7 fc,inv=[1]*n,[1]*n for i in range(1,n): fc[i]=i*fc[i-1]%mod inv[n-1]=pow(fc[n-1],mod-2,mod) for i in range(n-1,0,-1): inv[i-1]=inv[i]*i%mod f=lambda a,b:fc[a+b]*inv[a]*inv[b]%mod v=0 for x in range(b,w): v+=f(x,h-a-1)*f(w-1-x,a-1)%mod print(v%mod)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s829026072
p04046
Accepted
H, W, A, B = map(int, input().split()) mod = 10**9 + 7 N = H + W - 2 # 階乗 fac = [1] * N for i in range(1, N): fac[i] = (fac[i - 1] * i) % mod # 逆元 inv = [1] * N inv[N - 1] = pow(fac[N - 1], mod - 2, mod) for i in range(N - 1, 0, -1): inv[i - 1] = (inv[i] * i) % mod def f(x, y): res = fac[x + y] * inv[x] * inv[y] return res % mod ans = 0 for x in range(B, W): ans += f(x, H - A - 1) * f(W - 1 - x, A - 1) print(ans % mod)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s718080819
p04046
Accepted
H, W, A, B = map(int, input().split()) mod = 10 ** 9 + 7 # 階乗 fac = [1] * (H + W - 2) # 逆元 inv = [1] * (H + W - 2) for i in range(1, H + W - 2): fac[i] = (fac[i - 1] * i) % mod inv[i] = pow(fac[i], mod - 2, mod) def f(x, y): res = fac[x + y] * inv[x] * inv[y] return res % mod ans = 0 for x in range(B, W): ans += f(x, H - A - 1) * f(W - 1 - x, A - 1) print(ans % mod)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s770652618
p04046
Accepted
from collections import defaultdict,deque import sys,heapq,bisect,math,itertools,string,queue,datetime sys.setrecursionlimit(10**8) INF = float('inf') mod = 10**9+7 eps = 10**-7 def inpl(): return list(map(int, input().split())) def inpl_s(): return list(input().split()) H,W,A,B=inpl() ans = 0 MAX = H+W fac = [1]*(MAX+1) for i in range(1,MAX+1): fac[i] = (fac[i-1]*i)%mod gyakugen = [1]*(MAX+1) gyakugen[MAX] = pow(fac[MAX],mod-2,mod) for i in range(MAX,0,-1): gyakugen[i-1] = (gyakugen[i]*i)%mod def Comb(n,k):#nCk return (fac[n]*gyakugen[k]*gyakugen[n-k])%mod for x1 in range(B+1,W+1): y1 = H-A x2 = A y2 = W-x1+1 ans += (Comb(x1+y1-2,y1-1)*Comb(x2+y2-2,y2-1))%mod ans %= mod print(ans)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s952616122
p04046
Accepted
h, w, a, b = map(int, input().split()) m = 10**9 + 7 fac = [1, 1] inv = [1, 1] finv = [1, 1] for i in range(2, w+h+5): fac.append(fac[i-1] * i % m) inv.append(m - inv[m%i] * (m//i) % m) finv.append(finv[i-1] * inv[i] % m) def nck(n, k): if n < k: return 0 if n < 0 or k < 0: return 0 return fac[n] * (finv[k] * finv[n-k] % m) % m row = [] for i in range(h-a): row.append(nck(b+i, i)) ans = 0 for i in range(len(row)-1): ans += row[i] * nck(w-b-2 + h-1-i, h-1-i) ans %= m ans += row[-1] * nck(w-b-1 + a, a) ans %= m print(ans)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s491184318
p04046
Accepted
# nの逆元のリスト def inv_mod(n:int, mod:int) -> list: inv = [0,1] for i in range(2,n+1): inv.append(mod - ((mod//i)*inv[mod%i]) % mod) return inv # nの階乗のリスト def fact(n:int, mod:int) -> list: fac = [1,1] res = 1 for i in range(2,n+1): res = res*i%mod fac.append(res) return fac # nの階乗の逆元のリスト def fact_inv(n:int, inv:list, mod:int) -> list: facInv = [1,1] for i in range(2,n+1): facInv.append(facInv[i-1]*inv[i] % mod) return facInv # 二項係数 def nCr(n:int, r:int, mod:int, fac:list, facInv:list) -> int: if not (0<=r and r<=n): return 0 return ((fac[n]*facInv[r]) % mod) * facInv[n-r] % mod MOD = 10**9+7 n = 2*10**5 + 1 H,W,A,B = map(int, input().split()) # 二項係数の準備 inv = inv_mod(n,MOD) fac = fact(n,MOD) facInv = fact_inv(n,inv,MOD) # 手前×奥を実行して足していく ans = 0 for h in range(H-A): temp = nCr(h+(B-1), h, MOD, fac, facInv) \ * nCr((H-h-1)+(W-B-1), W-B-1, MOD, fac, facInv) ans = (ans + temp) % MOD print(ans)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s875460692
p04046
Accepted
H,W,A,B = map(int,input().split()) N = H+W+10 MOD = 10**9+7 fac = [1,1] + [0]*N finv = [1,1] + [0]*N inv = [0,1] + [0]*N for i in range(2,N+2): fac[i] = fac[i-1] * i % MOD inv[i] = -inv[MOD%i] * (MOD // i) % MOD finv[i] = finv[i-1] * inv[i] % MOD def ncr(n,r): if n < r: return 0 if n < 0 or r < 0: return 0 return fac[n] * (finv[r] * finv[n-r] % MOD) % MOD ans = 0 for i in range(W-B): ans += ncr(H-1-A+B+i, H-1-A) * ncr(A-1+W-1-B-i, A-1) ans %= MOD print(ans)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s966300455
p04046
Accepted
# -*- coding: utf-8 -*- import bisect import heapq import math import random import sys from pprint import pprint from collections import Counter, defaultdict, deque from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal from functools import lru_cache, reduce from itertools import combinations, combinations_with_replacement, product, permutations from operator import add, mul, sub sys.setrecursionlimit(10000) def read_int(): return int(input()) def read_int_n(): return list(map(int, input().split())) def read_float(): return float(input()) def read_float_n(): return list(map(float, input().split())) def read_str(): return input().strip() def read_str_n(): return list(map(str, input().split())) def error_print(*args): print(*args, file=sys.stderr) def mt(f): import time def wrap(*args, **kwargs): s = time.time() ret = f(*args, **kwargs) e = time.time() error_print(e - s, 'sec') return ret return wrap class Combination: def __init__(self, n, mod): g1 = [1, 1] g2 = [1, 1] inverse = [0, 1] for i in range(2, n + 1): g1.append((g1[-1] * i) % mod) inverse.append((-inverse[mod % i] * (mod//i)) % mod) g2.append((g2[-1] * inverse[-1]) % mod) self.MOD = mod self.N = n self.g1 = g1 self.g2 = g2 self.inverse = inverse def __call__(self, n, r): if (r < 0 or r > n): return 0 r = min(r, n-r) return self.g1[n] * self.g2[r] * self.g2[n-r] % self.MOD @mt def slv(H, W, A, B): MOD = 10**9+7 cmb = Combination(H+W, MOD) ans = 0 for i in range(W-B): w1 = cmb(i+(A-1), i) w2 = cmb((W-i-1)+(H-A-1), (H-A-1)) ans += w1*w2 ans %= MOD return ans % MOD def main(): H, W, A, B = read_int_n() print(slv(H, W, A, B)) if __name__ == '__main__': main()
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s030789723
p04046
Accepted
# -*- coding: utf-8 -*- import bisect import heapq import math import random import sys from pprint import pprint from collections import Counter, defaultdict, deque from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal from functools import lru_cache, reduce from itertools import combinations, combinations_with_replacement, product, permutations from operator import add, mul, sub sys.setrecursionlimit(10000) def read_int(): return int(input()) def read_int_n(): return list(map(int, input().split())) def read_float(): return float(input()) def read_float_n(): return list(map(float, input().split())) def read_str(): return input().strip() def read_str_n(): return list(map(str, input().split())) def error_print(*args): print(*args, file=sys.stderr) def mt(f): import time def wrap(*args, **kwargs): s = time.time() ret = f(*args, **kwargs) e = time.time() error_print(e - s, 'sec') return ret return wrap nCr = {} def C(n, r): if r == 0 or r == n: return 1 if r == 1: return n if (n, r) in nCr: return nCr[(n, r)] nCr[(n, r)] = C(n-1, r) + C(n-1, r-1) return nCr[(n, r)] MOD = 10**9+7 # 出力の制限 N = 10**6 g1 = [1, 1] # 元テーブル g2 = [1, 1] # 逆元テーブル inverse = [0, 1] # 逆元テーブル計算用テーブル for i in range(2, N + 1): g1.append((g1[-1] * i) % MOD) inverse.append((-inverse[MOD % i] * (MOD//i)) % MOD) g2.append((g2[-1] * inverse[-1]) % MOD) def cmb(n, r): if (r < 0 or r > n): return 0 r = min(r, n-r) return g1[n] * g2[r] * g2[n-r] % MOD @mt def slv(H, W, A, B): MOD = 10**9+7 ans = 0 for i in range(W-B): # print(ans) w1 = cmb(i+(A-1), i) w2 = cmb((W-i-1)+(H-A-1), (H-A-1)) ans += w1*w2 ans %= MOD return ans % MOD def main(): H, W, A, B = read_int_n() print(slv(H, W, A, B)) if __name__ == '__main__': main()
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s885713461
p04046
Accepted
H, W, A, B = map(int, input().split()) m = 10**9 + 7 import functools @functools.lru_cache(maxsize=None) def cr(c, r): return fc[c + r] * ic[c] * ic[r] % m fc = [1] * (H + W) for i in range(2, H+W): fc[i] = fc[i - 1] * i % m ic = [pow(x, m-2, m) for x in fc] ans = 0 for c in range(B, W): ans += cr(c, H - 1 - A) * cr(W - 1 - c, A - 1) % m print(ans % m)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s524232114
p04046
Accepted
# nの逆元のリスト def inv_mod(n:int, mod:int) -> list: inv = [0,1] for i in range(2,n+1): inv.append(mod - ((mod//i)*inv[mod%i]) % mod) return inv # nの階乗のリスト def fact(n:int, mod:int) -> list: fac = [1,1] res = 1 for i in range(2,n+1): res = res*i%mod fac.append(res) return fac # nの階乗の逆元のリスト def fact_inv(n:int, inv:list, mod:int) -> list: facInv = [1,1] for i in range(2,n+1): facInv.append(facInv[i-1]*inv[i] % mod) return facInv # 二項係数 def nCr(n:int, r:int, mod:int, fac:list, facInv:list) -> int: if n==0 and r==0: return 1 elif not (0<=r and r<=n): return 0 return ((fac[n]*facInv[r]) % mod) * facInv[n-r] % mod H,W,A,B = map(int, input().split()) mod = 10**9+7 inv = inv_mod(H+W, mod) fac = fact(H+W, mod) facInv = fact_inv(H+W,inv,mod) checkPoint = [] for i in range(H-A): checkPoint.append((i,B-1)) ans = 0 for h,w in checkPoint: ans = (ans + nCr(h+w,h,mod,fac,facInv) \ * nCr(H+W-h-w-3, H-h-1, mod, fac, facInv)) % mod print(ans)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s423917404
p04046
Accepted
H, W, A, B = map(int, input().split()) mod = 10**9 + 7 fact = [1] * (H + W - 1) invfact = [1] * (H + W - 1) for i in range(1, H + W - 1): fact[i] = fact[i - 1] * i % mod for i in range(1, H + W - 1): invfact[i] = pow(fact[i], mod-2, mod) def nCr(n,r): return fact[n] * invfact[r] * invfact[n-r] result = 0 for i in range(1, H - A + 1): result += (nCr((B-1)+(i-1), i-1) * nCr((W-B-1)+(H-i), W-B-1)) % mod print(int(result % mod))
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>
s927199611
p04046
Accepted
H, W, A, B = map(int, input().split()) p = 10 ** 9 + 7 F = [1 for i in range(H + W + 1)] for i in range(1, H + W + 1): F[i] = F[i - 1] * i % p Fi = [pow(f, p - 2, p) for f in F] def fac(a, b): a = F[a + b] * Fi[a] * Fi[b] return a % p if H - A < B: ans = 0 for h in range(H - A): ans += fac(h, B - 1) * fac(H - h - 1, W - B - 1) % p else: ans = fac(H - 1, W- 1) for w in range(B): ans -= fac(H - A - 1, w) * fac(A - 1, W - w - 1) % p print(ans % p)
2 3 1 1
2
<span class="lang-en"> <p>Score : <var>400</var> points</p> <div class="part"> <section> <h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns. Iroha is now standing in the top-left cell. She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p> <p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p> <p>Find the number of ways she can travel to the bottom-right cell.</p> <p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p> </section> </div> <div class="part"> <section> <h3>Constraints</h3><ul> <li><var> 1 ≦ H, W ≦ 100,000</var></li> <li><var> 1 ≦ A &lt; H</var></li> <li><var> 1 ≦ B &lt; W</var></li> </ul> </section> </div> <hr/> <div class="io-style"> <div class="part"> <section> <h3>Input</h3><p>The input is given from Standard Input in the following format:</p> <pre><var>H</var> <var>W</var> <var>A</var> <var>B</var> </pre> </section> </div> <div class="part"> <section> <h3>Output</h3><p>Print the number of ways she can travel to the bottom-right cell, modulo <var>10^9+7</var>.</p> </section> </div> </div> <hr/> <div class="part"> <section> <h3>Sample Input 1</h3><pre>2 3 1 1 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 1</h3><pre>2 </pre> <p>We have a <var>2×3</var> grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 2</h3><pre>10 7 3 4 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 2</h3><pre>3570 </pre> <p>There are <var>12</var> forbidden cells.</p> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 3</h3><pre>100000 100000 99999 99999 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 3</h3><pre>1 </pre> </section> </div> <hr/> <div class="part"> <section> <h3>Sample Input 4</h3><pre>100000 100000 44444 55555 </pre> </section> </div> <div class="part"> <section> <h3>Sample Output 4</h3><pre>738162020 </pre></section> </div> </span>