submission_id
string
problem_id
string
status
string
code
string
input
string
output
string
problem_description
string
s639233427
p04046
Accepted
h,w,a,b=map(int,input().split()) factorials=[] inverses=[] MOD=10**9+7 factorials.append(1) curr=1 for i in range(1,h+w+1): curr=(curr*i)%MOD factorials.append(curr) for i in range(h+w+1): inverses.append(pow(factorials[i],MOD-2,MOD)) ans=0 for i in range(h-a): ans=(ans+factorials[b-1+i]*factorials[h-2-i+w-b]*inverses[i]*inverses[b-1]*inverses[h-1-i]*inverses[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>
s316045630
p04046
Accepted
mod = 10 ** 9 + 7 fact = [1] inv = [1] for i in range(200000): fact.append(fact[i] * (i + 1) % mod) inv.append(pow(fact[i + 1], mod - 2, mod)) def ncr(n, r): if n < 0 or r < 0 or n - r < 0: return 0 return fact[n] * inv[r] * inv[n - r] % mod h, w, a, b = map(int, input().split()) ans = ncr(h + w - 2, h - 1) for i in range(min(a, b)): ans -= ncr(h - a + b - 1, h - a + i) * ncr(w - b + a - 1, w - b + i) 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>
s979367224
p04046
Accepted
h,w,a,b=map(int,input().split()) mod=pow(10,9)+7 # コンビネーション、さらに高速。あらかじめO(N)の計算をすることでのちの計算が早くなる 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 g1 = [1, 1] g2 = [1, 1] inverse = [0, 1] for i in range( 2, 200000 + 1 ): g1.append( ( g1[-1] * i ) % mod ) inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod ) g2.append( (g2[-1] * inverse[-1]) % mod ) ans=0 for k in range(1,h-a+1): tmp=cmb(k-1+b-1,k-1,mod) tmp*=cmb(h-k+w-b-1,h-k,mod) ans+=tmp 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>
s958216650
p04046
Accepted
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 mod = 10**9+7 size = 2*10**5 g1 = [1, 1] g2 = [1, 1] inverse = [0, 1] for i in range( 2, size + 1 ): g1.append( ( g1[-1] * i ) % mod ) inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod ) g2.append( (g2[-1] * inverse[-1]) % mod ) H, W, A, B = map(int,input().split()) ans = 0 for i in range(W-B): ans += cmb(H-A-1+(B+i), B+i, mod) * cmb(A-1+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>
s183501806
p04046
Accepted
def ext_euclid(a, b): # return (x, y, gcd(a, b)) such that a * x + b * y = gcd(a, b) if b == 0: return 1, 0, a y, x, v = ext_euclid(b, a % b) y -= (a // b) * x return x, y, v def mod_inv(a, mod): x, _, _ = ext_euclid(a, mod) return x % mod def comb_list_1(H, W, A, B, mod, modinv_list): # (h+B-1)_C_(B-1) (h in {0, ..., H-A-1}) ret = [0 for _ in range(H - A)] c = 1 for h in range(H - A): ret[h] = c c *= h + B c *= modinv_list[h + 1] c %= mod return ret def comb_list_2(H, W, A, B, mod, modinv_list): # (-h+H+W-B-2)_C_(W-B-1) (h in {0, ..., H-A-1}) ret = [0 for _ in range(H - A)] # initial value -> (A+W-B-1)_C_(W-B-1) (h = H-A-1) c = 1 for a in range(1, A + 1): c *= (a + W - B - 1) c *= modinv_list[a] c %= mod # fill elements of result list from the back for h in range(H - A - 1, -1, -1): ret[h] = c c *= H - h + W - B - 1 c *= modinv_list[H - h] c %= mod return ret def main(): MOD = 10 ** 9 + 7 H, W, A, B = list(map(int, input().split(' '))) modinv_list = [None] + [mod_inv(h, MOD) for h in range(1, H + 1)] # modinv doesn't exist on h = 0 combs_1 = comb_list_1(H, W, A, B, MOD, modinv_list) combs_2 = comb_list_2(H, W, A, B, MOD, modinv_list) ans = 0 for c1, c2 in zip(combs_1, combs_2): ans += c1 * c2 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>
s759041184
p04046
Accepted
h, w, a, b = map(int, input().split()) mod = 10 ** 9 + 7 n = h + w f = [1 for _ in range(n)] f_inv = [1 for _ in range(n)] for i in range(1, n): f[i] = f[i-1] * i % mod f_inv[i] = pow(f[i], mod-2, mod) def comb(n, k): return (f[n] * f_inv[k] % mod) * f_inv[n-k] % mod ans = comb(h+w-2, h-1) for i in range(b): ans -= comb(h-a+i-1, i) * comb(a+w-i-2, 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>
s160315145
p04046
Accepted
import math #import numpy as np import queue from collections import deque,defaultdict import heapq from sys import stdin,setrecursionlimit #from scipy.sparse.csgraph import dijkstra #from scipy.sparse import csr_matrix ipt = stdin.readline setrecursionlimit(10**7) def main(): h,w,a,b = map(int,ipt().split()) mod = 10**9+7 #nCrをmodで割った余りを求める。Nに最大値を入れて使用。 N = 2*10**5 g1 = [1, 1] # 元テーブル g2 = [1, 1] #逆元テーブル inverse = [0, 1] #逆元テーブル計算用テーブル 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 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) ans = cmb(h+w-2,h-1,mod) for i in range(max(a,b)): ans = (ans-cmb(b+h-a-1,b-1-i,mod)*cmb(a+w-b-1,a-1-i,mod)%mod)%mod print(ans) return 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>
s660671319
p04046
Accepted
g1 = [1, 1] g2 = [1, 1] inverse = [0, 1] MOD = 10**9+7 for i in range(2, 2*10**5+1): g1.append((g1[-1] * i) % MOD) inverse.append((-inverse[MOD % i] * (MOD // i)) % MOD) g2.append((g2[-1] * inverse[-1]) % MOD) def comb(n, r, mod=MOD): if r < 0 or r > n: return 0 r = min(r, n - r) return g1[n] * g2[r] * g2[n-r] % mod H, W, A, B = map(int, input().split()) ans = 0 n = H + W - B - 1 for i in range(1, H-A+1): ans += comb(n-i, H-i) * comb(B+i-2, B-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>
s113472321
p04046
Accepted
# 練習 import math h,w,a,b = list(map(int, input().split())) p = 10**9+7 # 方針 # (0,0) ~ (0,w-1) # (h-1,0 )~ (h-1,w-1)で考える # 通れないのは(h-a) ~ (h-1)行かつ(0) ~ (b-1) 列のところ # b <= i <= w-1 のiについて1,2,3の組み合わせはそれぞれ以下の通り # 1, (0,0) ~ (h-a-1,i) (h-a-1+i)! /(h-a-1)! * i! # 2, (h-a-1,i )~(h-a,i)  1 # 3, (h-a,i) ~ (h-1,w-1) (a-1+w-1-i)! /(a-1)! * (w-1-i)! def cmb(n, k, mod, fac, ifac): """ nCkを計算する """ k = min(k, n-k) return fac[n] * ifac[k] * ifac[n-k] % mod def make_tables(mod, n): """ 階乗テーブル、逆元の階乗テーブルを作成する """ fac = [1, 1] # 階乗テーブル・・・(1) ifac = [1, 1] #逆元の階乗テーブル・・・(2) inverse = [0, 1] #逆元テーブル・・・(3) for i in range(2, n+1): fac.append((fac[-1] * i) % mod) inverse.append((-inverse[mod % i] * (mod//i)) % mod) ifac.append((ifac[-1] * inverse[-1]) % mod) return fac, ifac fac, ifac = make_tables(p, h+w-2) total = 0 for i in range(b, w): r1 = cmb(h-a-1+i,i,p,fac, ifac) r3 = cmb(a-1+w-1-i, a-1,p,fac, ifac) total = total +( r1*r3) print(total%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>
s616725498
p04046
Accepted
MOD=10**9+7 H,W,A,B=map(int,input().split()) def invmod(a): return pow(a,MOD-2,MOD) def comb_mod(n,r): if 0<=r<=n: return fact_dic[n]*fact_inv_dic[r]*fact_inv_dic[n-r] else: return 0 fact_dic={0:1} fact_inv_dic={0:1} fact_mod=1 for i in range(1,H+W-1): fact_mod=(fact_mod*i)%MOD fact_dic[i]=fact_mod fact_inv_dic[i]=invmod(fact_mod) answer=0 for i in range(B,W): answer+=comb_mod(H-A-1+i,i)*comb_mod(A-1+W-1-i,W-1-i) answer%=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>
s302106136
p04046
Accepted
MOD=10**9+7 H,W,A,B=map(int,input().split()) def invmod(a): return pow(a,MOD-2,MOD) def comb_mod(n,r): if 0<=r<=n: return fact_dic[n]*fact_inv_dic[r]*fact_inv_dic[n-r] else: return 0 fact_dic={0:1} fact_inv_dic={0:1} fact_mod=1 for i in range(1,H+W-1): fact_mod=(fact_mod*i)%MOD fact_dic[i]=fact_mod fact_inv_dic[i]=invmod(fact_mod) answer=0 for i in range(B,W): answer+=comb_mod(H-A+B-1,i)*comb_mod(W+A-B-1,W-1-i) answer%=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>
s189114842
p04046
Accepted
MOD=10**9+7 H,W,A,B=map(int,input().split()) def invmod(a): return pow(a,MOD-2,MOD) def comb_mod(n,r): return fact_dic[n]*fact_inv_dic[r]*fact_inv_dic[n-r] fact_dic={0:1} fact_inv_dic={0:1} fact_mod=1 for i in range(1,H+W-1): fact_mod=(fact_mod*i)%MOD fact_dic[i]=fact_mod fact_inv_dic[i]=invmod(fact_mod) answer=0 if H-A>=W-B: for i in range(B,W): answer+=comb_mod(H-A+B-1,i)*comb_mod(W+A-B-1,W-1-i) answer%=MOD else: for j in range(H-A): answer+=comb_mod(H-A+B-1,B+j)*comb_mod(W+A-B-1,W-B-1-j) answer%=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>
s351728424
p04046
Accepted
import math p=1000000007 g1=[1,1] g2=[1,1] inverse=[0,1] for i in range(2,2*(10**5)+1): g1.append((g1[-1]*i)%p) inverse.append((-inverse[p%i]*(p//i))%p) g2.append((g2[-1]*inverse[-1])%p) def cmb2(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 def chwp(h,w,p): if h==1 or w==1:return 1 else:return cmb2(h+w-2,h-1,p) h,w,a,b= map(int,input().split()) ans=0 for i in range(1,h-a+1):ans=(ans+chwp(i,b,p)*chwp(h-i+1,w-b,p))%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>
s629168942
p04046
Accepted
h,w,a,b = map(int,input().split()) mod = 10**9+7 fact = [1,1] finv = [1,1] inv = [0,1] for i in range(2,h+w+5): fact.append((fact[-1]*i)%mod) inv.append((inv[mod%i]*(mod-mod//i))%mod) finv.append((finv[-1]*inv[-1])%mod) def nCr(n,r,mod): if r > n: return 0 else: return fact[n]*finv[r]*finv[n-r]%mod ans = 0 for i in range(100000): x = h-a-i y = b+1+i if 0 >= x or y > w: break ans += nCr(h+w-x-y,h-x,mod)*nCr(x+y-2,x-1,mod)%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>
s016401409
p04046
Accepted
import sys sys.setrecursionlimit(10000000) MOD = 10 ** 9 + 7 INF = 10 ** 15 def main(): H,W,A,B = map(int,input().split()) MAXN = H + W + 3 factorial = [1] for i in range(1,MAXN + 1): factorial.append(factorial[-1]*i%MOD) inv_factorial = [-1] * (MAXN + 1) inv_factorial[-1] = pow(factorial[-1],MOD - 2,MOD) for i in range(MAXN - 1,-1,-1): inv_factorial[i] = inv_factorial[i + 1]*(i + 1)%MOD fact = lambda N:factorial[N] nck = lambda N,K: 0 if K > N or K < 0 else factorial[N]*inv_factorial[N - K]*inv_factorial[K]%MOD dist = lambda sy,sx,gy,gx: nck(gy - sy + gx - sx,gy - sy) ans = 0 if H - A < W - B: H,W = W,H A,B = B,A for i in range(W - B): x = B + i + 1 y = H - A - i ans += dist(1,1,y,x)*dist(y,x,H,W)%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>
s380416209
p04046
Accepted
mod = 10 ** 9 + 7 N = 10 ** 6 fact = [1, 1] factinv = [1, 1] inv = [0, 1] for i in range(2, N + 1): fact.append(fact[-1] * i % mod) inv.append((-inv[mod % i] * (mod // i)) % mod) factinv.append((factinv[-1] * inv[-1]) % mod) def combi_mod(n, r, p): if (r < 0) or (n < r): return 0 r = min(r, n - r) return fact[n] * factinv[r] * factinv[n - r] % p H, W, A, B = map(int, input().split()) if B < W / 2: not_ans = 0 total = combi_mod((H - 1) + (W - 1), H-1, mod) for i in range(B): sq1h = H - A sq1w = i + 1 sq2h = A sq2w = W - i n = combi_mod((sq1h -1) + (sq1w - 1), (sq1h -1), mod) * combi_mod((sq2h - 1) + (sq2w - 1), (sq2h - 1), mod) % mod not_ans = (not_ans + n) % mod ans = (total - not_ans) % mod print(ans) else: ans = 0 for i in range(B, W): sq1h = H - A sq1w = i + 1 sq2h = A sq2w = W - i n = combi_mod((sq1h - 1) + (sq1w - 1), (sq1h - 1), mod) * combi_mod((sq2h - 1) + (sq2w - 1), (sq2h - 1), mod) ans = (ans + n) % 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>
s716750790
p04046
Accepted
# -*- coding: utf-8 -*- import sys sys.setrecursionlimit(10**9) INF=10**18 MOD=10**9+7 input=lambda: sys.stdin.readline().rstrip() YesNo=lambda b: bool([print('Yes')] if b else print('No')) YESNO=lambda b: bool([print('YES')] if b else print('NO')) int1=lambda x:int(x)-1 def main(): H,W,A,B=map(int,input().split()) def COMinit(n,MOD): fac,finv,inv=[0]*max(2,n+1),[0]*max(2,n+1),[0]*max(2,n+1) fac[0]=fac[1]=1 finv[0]=finv[1]=1 inv[1]=1 for i in range(2,(n+1)): fac[i]=fac[i-1]*i%MOD inv[i]=MOD-inv[MOD%i]*(MOD//i)%MOD finv[i]=finv[i-1]*inv[i]%MOD return fac,finv,inv fac,finv,inv=COMinit(H+W,MOD) def COM(n, k, MOD=MOD): if n<k or n<0 or k<0: return 0 return fac[n]*(finv[k]*finv[n-k]%MOD)%MOD ans=0 for i in range(B,W): ans+=COM(H-A-1+i,i)*COM(A-2+W-i,A-1) 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>
s483029157
p04046
Accepted
H,W,A,B = map(int,input().split()) N=H+W fact = [0]*(N+1) ifact = [0]*(N+1) inv = [0]*(N+1) p=10**9+7 def combination(n): fact[0] = 1 fact[1] = 1 ifact[0] = 1 ifact[1] = 1 inv[1] = 1 for i in range(2,n+1): fact[i] = (fact[i-1]*i)%p inv[i] = p - inv[p%i]*(p//i)%p ifact[i] = (ifact[i-1]*inv[i])%p def op(n,k): if k<0 or k>n or n<0: return 0 return (fact[n]*ifact[k]*ifact[n-k])%p combination(N) b = op(H-A+B-1, B) res = 0 for i in range(A): res = (res + op(W-B-2+i, i))%p res = (res*b)%p for i in range(H-A): res = (res + op(B+i, i)*op(H-i-1+W-B-2,W-B-2))%p if H-A==1 and W-B==1: res = 1 elif H-A==1: res = op(W-B+H-2, H-1) elif W-B==1: res = op(H-A+W-2, W-1) 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>
s820510133
p04046
Accepted
h,w,a,b = map(int, input().split()) import math ans = 0 mod = 10**9+7 l =[i for i in range(1,h+w+2)] for i in range(1,len(l)-1): l[i+1] = l[i+1]*l[i]%mod l = [1]+ l gyakugen = [pow(l[i], mod-2, mod) for i in range(1, len(l))] gyakugen = [1] + gyakugen for p in range(1, w-b+1): ans += l[(h-a-1+b+p-1)]*l[w-(b+p)+a-1]*\ gyakugen[h-a-1]*gyakugen[b+p-1] * gyakugen[a-1]*gyakugen[w-(b+p)] print(int(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>
s773831563
p04046
Accepted
H, W, A, B = map(int, input().split()) mod = int(1e9) + 7 def inved(a): x, y, u, v, k, l = 1, 0, 0, 1, a, mod while l != 0: x, y, u, v = u, v, x - u * (k // l), y - v * (k // l) k, l = l, k % l return x % mod fact = [1 for _ in range(H+W+A+B+1)] invf = [1 for _ in range(H+W+A+B+1)] for i in range(H+W+A+B): fact[i+1] = (fact[i] * (i + 1)) % mod invf[-1] = inved(fact[-1]) for i in range(H+W+A+B, 0, -1): invf[i-1] = (invf[i] * i) % mod S = 0 for i in range(H-A): S += (fact[i+B-1] * invf[i] * fact[H-i+W-B-2] * invf[H-1-i]) % mod S %= mod S *= (invf[B-1] * invf[W-B-1]) % mod S %= mod print(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>
s196254093
p04046
Accepted
# Combination MOD = 10**9+7 MAX = 2*10**5 fac = [1,1] + [0]*MAX finv = [1,1] + [0]*MAX inv = [0,1] + [0]*MAX for i in range(2,MAX+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 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 H, W, A, B = map(int, input().split()) ans = 0 for i in range(1, H-A+1): cnt = comb(i-1+B-1, i-1) #print(i, cnt) cnt *= comb(H-i+W-B-1, H-i) ans += cnt 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>
s047331751
p04046
Accepted
#ライブラリの点検 class Data(): def __init__(self): self.power=1 self.rev=1 class Combi(): def __init__(self,N,mod): self.lists=[Data() for _ in range(N+1)] self.mod=mod for i in range(2,N+1): self.lists[i].power=((self.lists[i-1].power)*i)%self.mod self.lists[N].rev=pow(self.lists[N].power,self.mod-2,self.mod) for j in range(N,0,-1): self.lists[j-1].rev=((self.lists[j].rev)*j)%self.mod def combi(self,K,R): if K<R: return 0 else: return ((self.lists[K].power)*(self.lists[K-R].rev)*(self.lists[R].rev))%self.mod ###################### h,w,a,b=map(int,input().split()) ans=0 mod=10**9+7 C=Combi(w+h+2,mod) for i in range(h-a): ans+=C.combi(b+i-1,i)*C.combi(w+h-2-b-i,h-1-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>
s375158317
p04046
Accepted
#!/usr/bin/env python3 import sys # import math # from string import ascii_lowercase, ascii_upper_case, ascii_letters, digits, hexdigits # import re # re.compile(pattern) => ptn obj; p.search(s), p.match(s), p.finditer(s) => match obj; p.sub(after, s) # from operator import itemgetter # itemgetter(1), itemgetter('key') # from collections import deque # deque class. deque(L): dq.append(x), dq.appendleft(x), dq.pop(), dq.popleft(), dq.rotate() # from collections import defaultdict # subclass of dict. defaultdict(facroty) # from collections import Counter # subclass of dict. Counter(iter): c.elements(), c.most_common(n), c.subtract(iter) # from heapq import heapify, heappush, heappop # built-in list. heapify(L) changes list in-place to min-heap in O(n), heappush(heapL, x) and heappop(heapL) in O(lgn). # from heapq import nlargest, nsmallest # nlargest(n, iter[, key]) returns k-largest-list in O(n+klgn). # from itertools import count, cycle, repeat # count(start[,step]), cycle(iter), repeat(elm[,n]) # from itertools import groupby # [(k, list(g)) for k, g in groupby('000112')] returns [('0',['0','0','0']), ('1',['1','1']), ('2',['2'])] # from itertools import starmap # starmap(pow, [[2,5], [3,2]]) returns [32, 9] # from itertools import product, permutations # product(iter, repeat=n), permutations(iter[,r]) # from itertools import combinations, combinations_with_replacement # from itertools import accumulate # accumulate(iter[, f]) # from functools import reduce # reduce(f, iter[, init]) # from functools import lru_cache # @lrucache ...arguments of functions should be able to be keys of dict (e.g. list is not allowed) # from bisect import bisect_left, bisect_right # bisect_left(a, x, lo=0, hi=len(a)) returns i such that all(val<x for val in a[lo:i]) and all(val>-=x for val in a[i:hi]). # from copy import deepcopy # to copy multi-dimentional matrix without reference # from fractions import gcd # for Python 3.4 (previous contest @AtCoder) def main(): mod = 1000000007 # 10^9+7 inf = float('inf') # sys.float_info.max = 1.79...e+308 # inf = 2 ** 64 - 1 # (for fast JIT compile in PyPy) 1.84...e+19 sys.setrecursionlimit(10**6) # 1000 -> 1000000 def input(): return sys.stdin.readline().rstrip() def ii(): return int(input()) def mi(): return map(int, input().split()) def mi_0(): return map(lambda x: int(x)-1, input().split()) def lmi(): return list(map(int, input().split())) def lmi_0(): return list(map(lambda x: int(x)-1, input().split())) def li(): return list(input()) def make_factorial_table(size, mod): ''' fact_mod[i] は i! % mod を表す。fact_mod[0] ~ facto_mod[size] まで計算可能なテーブルを返す >>> make_factorial_table(20, 10**9+7) [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 39916800, 479001600, 227020758, 178290591, 674358851, 789741546, 425606191, 660911389, 557316307, 146326063] ''' fact_mod = [1] * (size + 1) for i in range(1, size + 1): fact_mod[i] = (fact_mod[i - 1] * i) % mod return fact_mod def combination(n, r, mod, fact_table): ''' フェルマーの小定理 a ^ p-1 ≡ 1 (mod p) a ^ p-2 ≡ 1/a (mod p) (逆元) nCr = (n!) / ((n-r)! * r!) だが、mod p の世界ではこの分母を逆元を用いて計算しておくことが可能 >>> m = 1000000007 >>> fact_table = make_factorial_table(100, m) >>> combination(10, 5, m, fact_table) 252 >>> combination(100, 50, m, fact_table) 538992043 ''' numerator = fact_table[n] denominator = (fact_table[n-r] * fact_table[r]) % mod # pow はすでに繰り返し二乗法で効率的に実装されている return (numerator * pow(denominator, mod-2, mod)) % mod h, w, a, b = mi() # sigma {k = b to w-1} h-1-a+kCh-1-a * a-1+w-1-kCa-1 # sample 2 だと {k=4to6} 6+kC6 * 2+6-kC2 = (10C6 * 4C2 + 11C6 * 3C2 + 12C6 * 2C2) fact_table = make_factorial_table(2 * h + 2 * w, mod) ans = 0 for k in range(b, w): # print(f"{h-1-a+k} {h-1-a} / {a-1+w-1-k} {a-1}") ans = (ans + combination(h-1-a+k, h-1-a, mod, fact_table) * combination(a-1+w-1-k, a-1, mod, fact_table)) % 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>
s317456342
p04046
Accepted
import sys, re from collections import deque, defaultdict, Counter from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby from operator import itemgetter, mul from copy import deepcopy from string import ascii_lowercase, ascii_uppercase, digits from bisect import bisect, bisect_left from heapq import heappush, heappop from functools import reduce def input(): return sys.stdin.readline().strip() def INT(): return int(input()) def MAP(): return map(int, input().split()) def LIST(): return list(map(int, input().split())) def ZIP(n): return zip(*(MAP() for _ in range(n))) sys.setrecursionlimit(10 ** 9) INF = float('inf') mod = 10 ** 9 + 7 lim = 2*10**5 #必要そうな階乗の限界を入力 #階乗# fact = [1] * (lim+1) for n in range(1, lim+1): fact[n] = n * fact[n-1] % mod #階乗の逆元# fact_inv = [1]*(lim+1) fact_inv[lim] = pow(fact[lim], mod-2, mod) for n in range(lim, 0, -1): fact_inv[n-1] = n*fact_inv[n]%mod def C(n, r): return (fact[n]*fact_inv[r]%mod)*fact_inv[n-r]%mod H, W, A, B = MAP() ans = 0 for n in range(B, W): way = C(H-A-1+n, n)*C(W-n-1+A-1, A-1)%mod ans = (ans+way)%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>
s080701961
p04046
Accepted
import sys, re from collections import deque, defaultdict, Counter from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2 from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby from operator import itemgetter, mul from copy import deepcopy from string import ascii_lowercase, ascii_uppercase, digits from bisect import bisect, bisect_left from fractions import gcd from heapq import heappush, heappop from functools import reduce def input(): return sys.stdin.readline().strip() def INT(): return int(input()) def MAP(): return map(int, input().split()) def LIST(): return list(map(int, input().split())) def ZIP(n): return zip(*(MAP() for _ in range(n))) sys.setrecursionlimit(10 ** 9) INF = float('inf') mod = 10 ** 9 + 7 lim = 2*10**5 #必要そうな階乗の限界を入力 #階乗# fact = [1] * (lim+1) for n in range(1, lim+1): fact[n] = n * fact[n-1] % mod #階乗の逆元# fact_inv = [1]*(lim+1) fact_inv[lim] = pow(fact[lim], mod-2, mod) for n in range(lim, 0, -1): fact_inv[n-1] = n*fact_inv[n]%mod def C(n, r): return (fact[n]*fact_inv[r]%mod)*fact_inv[n-r]%mod H, W, A, B = MAP() ans = 0 for n in range(B, W): way = C(H-A-1+n, n)*C(W-n-1+A-1, A-1)%mod ans = (ans+way)%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>
s973821399
p04046
Accepted
import sys, re from collections import deque, defaultdict, Counter from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2 from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby from operator import itemgetter, mul from copy import deepcopy from string import ascii_lowercase, ascii_uppercase, digits from bisect import bisect, bisect_left from heapq import heappush, heappop from functools import reduce def input(): return sys.stdin.readline().strip() def INT(): return int(input()) def MAP(): return map(int, input().split()) def LIST(): return list(map(int, input().split())) def ZIP(n): return zip(*(MAP() for _ in range(n))) sys.setrecursionlimit(10 ** 9) INF = float('inf') mod = 10 ** 9 + 7 lim = 2*10**5 #必要そうな階乗の限界を入力 #階乗# fact = [1] * (lim+1) for n in range(1, lim+1): fact[n] = n * fact[n-1] % mod #階乗の逆元# fact_inv = [1]*(lim+1) fact_inv[lim] = pow(fact[lim], mod-2, mod) for n in range(lim, 0, -1): fact_inv[n-1] = n*fact_inv[n]%mod def C(n, r): return (fact[n]*fact_inv[r]%mod)*fact_inv[n-r]%mod H, W, A, B = MAP() ans = 0 for n in range(B, W): way = C(H-A-1+n, n)*C(W-n-1+A-1, A-1)%mod ans = (ans+way)%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>
s239755774
p04046
Accepted
import sys, re from collections import deque, defaultdict, Counter from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2 from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby from operator import itemgetter, mul from copy import deepcopy from string import ascii_lowercase, ascii_uppercase, digits from bisect import bisect, bisect_left from heapq import heappush, heappop from functools import reduce def input(): return sys.stdin.readline().strip() def INT(): return int(input()) def MAP(): return map(int, input().split()) def LIST(): return list(map(int, input().split())) def ZIP(n): return zip(*(MAP() for _ in range(n))) sys.setrecursionlimit(10 ** 9) INF = float('inf') mod = 10 ** 9 + 7 lim = 2*10**6 fact = [1]*(lim+1) for n in range(1, lim+1): fact[n] = n*fact[n-1]%mod fact_inv = [1]*(lim+1) fact_inv[lim] = pow(fact[lim], mod-2, mod) for n in range(lim, 0, -1): fact_inv[n-1] = n*fact_inv[n]%mod def C(n, r): return (fact[n]*fact_inv[r]%mod)*fact_inv[n-r]%mod H, W, A, B = MAP() ans = 0 for n in range(B, W): way = C(H-A-1+n, n)*C(W-n-1+A-1, A-1)%mod ans = (ans+way)%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>
s529214189
p04046
Accepted
def combmod_pre(N, p): ''' sample usage: p = 10**9+7 N = 10**6 fact, finv = combmod_pre(N, p) combmod(n, r, p) ''' fact = [1, 1] finv = [1, 1] inv = [0, 1] for i in range(2, N+1): fact.append((fact[-1] * i) % p) inv.append((-inv[p % i] * (p // i)) % p) finv.append((finv[-1] * inv[-1]) % p) return (fact, finv) def combmod(n, r, fact, finv, p): ''' sample usage: combmod(3000, 1000, fact, finv, p) p is a same value of combmod_pre's argument fact, finv is return value of combmod_pre ''' if r < 0 or n < r: return 0 return fact[n] * finv[r] * finv[n-r] % p p = 10**9+7 N = 10**6 fact, finv = combmod_pre(N, p) h, w, a, b = (int(x) for x in input().split()) ans = 0 for j in range(b, w): x = combmod(h - a - 1 + j, j, fact, finv, p) y = combmod(a + w - 2 - j, w-1 - j, fact, finv, p) ans += x * y 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>
s194170678
p04046
Accepted
def combmod(n, r, p): if r < 0 or n < r: return 0 return fact[n] * finv[r] * finv[n-r] % p p = 10**9+7 N = 10**6 fact = [1, 1] finv = [1, 1] inv = [0, 1] for i in range(2, N+1): fact.append((fact[-1] * i) % p) inv.append((-inv[p % i] * (p // i)) % p) finv.append((finv[-1] * inv[-1]) % p) h, w, a, b = (int(x) for x in input().split()) ans = 0 for j in range(b, w): ans += combmod(h-a-1 + j, j, p) * combmod(a+w-2 - j, w-1 - j, 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>
s067452857
p04046
Accepted
MOD = 10**9+7 h,w,a,b = map(int,input().split()) n = h+w fac = [1]*(n+1) fac_inv = [0]*(n+1) for i in range(n): fac[i+1] = (fac[i] * (i+1)) %MOD fac_inv[-1] = pow(fac[-1],MOD-2,MOD) for i in range(n,0,-1): fac_inv[i-1] = (fac_inv[i] * i) % MOD def comb(n,k): if k<0 or k>n: return 0 x = (fac[n] * (fac_inv[k] * fac_inv[n-k])%MOD)%MOD return x ans = 0 for i in range(w-b): ans += comb((h-a-1) + b+i, b+i) * comb((a-1) + (w - b-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>
s292934544
p04046
Accepted
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 mod = 10**9+7 #出力の制限 N = 2*10**5 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 ) H,W,A,B=map(int,input().split()) ans=cmb(H+W-2,H-1,mod) for i in range(H-A,H): ans-=cmb(i+B-1,B-1,mod)*cmb(H+W-2-i-B,W-B-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>
s633679559
p04046
Accepted
import sys def comb(n, r, mod=None): if r == 0 or r == n: return 1 r = min([r, n-r]) x, y = 1, 1 ans = 1 for i in range(1, r+1): if mod: x *= n+1-i y *= i x %= mod y %= mod else: ans *= n+1-i ans //= i ans = x*pow(y, mod-2, mod)%mod if mod else ans return ans def main(): input = sys.stdin.readline h, w, a, b = map(int, input().split()) mod = pow(10, 9)+7 bef, aft = 1, comb(h+w-b-2, h-1, mod) ans = 0 for i in range(h-a): ans += bef*aft ans %= mod bef = bef*(i+b)*pow(i+1, mod-2, mod)%mod aft = aft*(h-i-1)*pow(h-i+w-b-2, mod-2, mod)%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>
s586146267
p04046
Accepted
MOD=10**9+7 UPPERLIMIT=2*(10**5)+1 MODMUL=[1, 1]+[0]*(UPPERLIMIT-1) for i in range(2, UPPERLIMIT+1): MODMUL[i]=MODMUL[i-1]*i%MOD MODDIV=[1]*UPPERLIMIT+[pow(MODMUL[-1], MOD-2, MOD)] for i in range(UPPERLIMIT, 0, -1): MODDIV[i-1]=MODDIV[i]*i%MOD H, W, A, B=map(int, input().split()) ans=(((MODMUL[H+W-2]*MODDIV[H-1])%MOD)*MODDIV[W-1])%MOD x=[MODMUL[H-A+B+i-1]*MODDIV[B-1]*MODDIV[H-A+i]%MOD for i in range(A)] y=[MODMUL[W+A-B-i-1]*MODDIV[W-B]*MODDIV[A-i-1]%MOD for i in range(A)] for i in range(A-1, 0, -1): x[i]-=x[i-1] for i in range(A): ans-=(x[i]*y[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>
s236037854
p04046
Accepted
H, W, A, B = map(int, input().split()) MAX = 2 * 10 ** 5 + 1 MOD = 10 ** 9 + 7 # Factorial fac = [0] * (MAX + 1) fac[0] = 1 fac[1] = 1 for i in range(2, MAX + 1): fac[i] = fac[i - 1] * i % MOD # Inverse factorial finv = [0] * (MAX + 1) finv[MAX] = pow(fac[MAX], MOD - 2, MOD) for i in reversed(range(1, MAX + 1)): finv[i - 1] = finv[i] * i % MOD def comb(a, b): return fac[a + b] * finv[a] * finv[b] ans = 0 for i in range(B, W): ans += comb(i, H - A - 1) * comb(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>
s469473134
p04046
Accepted
a,b,c,d = list(map(int, input().split())) def cmb(n, r, p): if (r < 0) or (n < r): return 0 r = min(r, n - r) return fact[n] * factinv[r] * factinv[n-r] % p p = 10**9+7 N = 10 ** 6 + 2 fact = [1, 1] # fact[n] = (n! mod p) factinv = [1, 1] # factinv[n] = ((n!)^(-1) mod p) inv = [0, 1] # factinv 計算用 for i in range(2, N + 1): fact.append((fact[-1] * i) % p) inv.append((-inv[p % i] * (p // i)) % p) factinv.append((factinv[-1] * inv[-1]) % p) def wh(w,h): return cmb(w+h,w,10**9+7) ans = wh(b-1,a-1) #print(ans) for i in range(d): tmp = wh(i,a-c-1) * wh(c-1,b-i-1) % p ans += p ans -= tmp ans %= p #print(ans) 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>
s840046869
p04046
Accepted
MOD = 10 ** 9 + 7 def prepare(n): global MOD modFacts = [0] * (n + 1) modFacts[0] = 1 for i in range(n): modFacts[i + 1] = (modFacts[i] * (i + 1)) % MOD invs = [1] * (n + 1) invs[n] = pow(modFacts[n], MOD - 2, MOD) for i in range(n, 1, -1): invs[i - 1] = (invs[i] * i) % MOD return modFacts, invs def pathVar(sx, sy, gx, gy): x = gx - sx y = gy - sy rst = modFacts[x + y] rst *= invs[x] * invs[y] rst %= MOD return rst H, W, A, B = map(int, input().split()) # H -= 1; W -= 1 modFacts, invs = prepare(H + W) cnt = 0 while A > 0 and B > 0: x = H - A y = B - 1 path1 = pathVar(0, 0, x, y) path2 = pathVar(x, y, H - 1, W - 1) cnt += path1 * path2 cnt %= MOD A -= 1; B -= 1 ans = pathVar(0, 0, H - 1, W - 1) print((ans - cnt) % 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>
s925317109
p04046
Accepted
#coding: utf-8 import math import heapq import bisect import numpy as np from collections import Counter, deque import itertools #from scipy.misc import comb MOD = 10**9+7 H,W,A,B = map(int,input().split()) def comb(a,b): p = fac[a-b]*fac[b]%MOD return fac[a]*pow(p,MOD-2,MOD)%MOD fac=[1] for i in range(H+W): fac.append(fac[-1]*(i+1)%MOD) ans=0 for i in range(W-B): p = comb(H-A-1+B+i,B+i)*comb(W-B-i-2+A,A-1) ans += p%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>
s791598446
p04046
Accepted
h,w,a,b=map(int,input().split()) inv=[0]*(2*max(h,w)+1) fact=[0]*(2*max(h,w)+1) inv[0]=1;fact[0]=1 mod=10**9+7 for i in range(1,2*max(h,w)+1): fact[i]=fact[i-1]*i%mod inv[-1]=pow(fact[-1],mod-2,mod) for i in range(2*max(h,w),0,-1): inv[i-1]=inv[i]*i%mod ans=0 for yoko in range(b+1,w+1): re=fact[yoko-1+h-1-a]*inv[h-1-a]*inv[yoko-1]%mod rr=fact[w-yoko+a-1]*inv[w-yoko]*inv[a-1]%mod ans=(ans+re*rr%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>
s810498265
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)
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>
s693966858
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 = [int(i) for i in input().split(" ")] ans = 0 # 経路の足し上げ C(縦+横, 縦(or横)) (重複のある組み合わせ) for i in range(B, W): ans += comb((H-A-1) + i, i) * comb((A - 1) + (W-1-i), W-1- 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>
s033787017
p04046
Accepted
def prepare(n, MOD): f = 1 factorials = [1] for m in range(1, n + 1): f *= m f %= MOD factorials.append(f) inv = pow(f, MOD - 2, MOD) invs = [1] * (n + 1) invs[n] = inv for m in range(n, 1, -1): inv *= m inv %= MOD invs[m - 1] = inv return factorials, invs h, w, a, b = map(int, input().split()) MOD = 10 ** 9 + 7 facts, invs = prepare(h + w, MOD) ans = 0 ib = invs[b - 1] iwb = invs[w - b - 1] for down in range(h - a): left_pattern = facts[b - 1 + down] * ib * invs[down] % MOD right_pattern = facts[h - 1 - down + w - b - 1] * invs[h - 1 - down] * iwb % MOD ans = (ans + left_pattern * right_pattern) % 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>
s932614654
p04046
Accepted
H, W, A, B = map(int, input().split()) mod = 10**9 + 7 fact = [1] * (H+W+1) fact_inv = [1] * (H+W+1) for i in range(1, H+W+1): fact[i] = i * fact[i-1] % mod fact_inv[H+W] = pow(fact[H+W], mod-2, mod) for i in range(1, H+W+1)[::-1]: fact_inv[i-1] = i * fact_inv[i] % mod comb = lambda n, k: fact[n] * fact_inv[k] * fact_inv[n-k] % mod ans = 0 for i in range(B+1, W+1): ans += comb((i-1) + (H-A-1), i-1) * comb((W-i) + (A-1), W-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>
s418523274
p04046
Accepted
mod = 10 ** 9 + 7 fac_table = [1 for i in range(200001)] inv_table = [1 for i in range(200001)] def make_table(h, w): for i in range(1, h + w - 1): fac_table[i] = fac_table[i - 1] * i % mod inv_table[i] = pow(fac_table[i], mod - 2, mod) def comb(n, r): return fac_table[n] * inv_table[n - r] % mod * inv_table[r] % mod def resolve(): H, W, A, B = map(int, input().split()) make_table(H, W) print( sum( [ comb(H - A - 1 + i, i) * comb(A - 1 + W - i - 1, A - 1) % mod for i in range(B, W) ] ) % mod ) if __name__ == "__main__": resolve()
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>
s257839064
p04046
Accepted
H, W, A, B = map(int, input().split()) mod = 10**9 + 7 U = 2 * 10**5 factorial = [1 for _ in range(U + 1)] for i in range(1, U + 1): factorial[i] = (factorial[i - 1] * i) % mod inverse = [1 for _ in range(U + 1)] inverse[U] = pow(factorial[U], mod - 2, mod) for i in range(U, 0, -1): inverse[i - 1] = (inverse[i] * i) % mod def comb(n, k): if k < 0 or k > n: return 0 x = factorial[n] x *= inverse[k] x %= mod x *= inverse[n - k] x %= mod return x ans = 0 for i in range(H - A): x = comb(B - 1 + i, i) 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>
s456454409
p04046
Accepted
import math import time DIV_VALUE = 10**9 + 7 def calcModOfPow(a, n, p): btm = a ans = 1 while n != 0: # print('btm: {}'.format(btm)) if n & 1: ans = (ans * btm) % p # print('ans: {}'.format(ans)) n = n>>1 btm = (btm**2) % p # print(n) return ans def calcModOfInv(a, p): b = p x = 1 y = 0 while b: div = a // b a -= div * b [a, b] = [b, a] x -= div * y [x, y] = [y, x] x %= p if x < 0: x += p return x class FirstHalf: def __init__(self, H, W, A, B): self.H = H self.W = W self.A = A self.B = B self.k = 0 self.div = 0 self.sup = 0 self.numOfCases = 1 def getNumOfCases(self): if self.k != 0: self.numOfCases = self.numOfCases * ( (self.B + self.k - 1) ) * ( calcModOfInv(self.k, DIV_VALUE) ) % DIV_VALUE self.k += 1 # print('----- k == {} -----'.format(self.k)) # print(self.numOfCases) # self.numOfCases %= DIV_VALUE return self.numOfCases # return self.numOfCases % DIV_VALUE class SecondHalf: def __init__(self, H, W, A, B): self.H = H self.W = W self.A = A self.B = B self.k = 0 self.div = 0 self.numOfCases = 1 totalval = self.H + self.W - self.B - 2 loopval = self.H - 1 if self.H < self.W - self.B else self.W - self.B - 1 for top in range(totalval, totalval - loopval, -1): self.numOfCases *= top self.numOfCases %= DIV_VALUE for bottom in range(loopval, 0, -1): self.numOfCases *= calcModOfInv(bottom, DIV_VALUE) self.numOfCases %= DIV_VALUE # self.numOfCases = ( # math.factorial(self.H + self.W - self.B - 2) # ) // ( # math.factorial(self.W - self.B - 1) # ) // ( # math.factorial(self.H - 1) # ) def getNumOfCases(self): if self.k != 0: self.numOfCases = self.numOfCases * ( self.H - self.k ) * ( calcModOfInv( self.H + self.W - self.B - self.k - 1, DIV_VALUE ) ) % DIV_VALUE self.k += 1 # print(self.numOfCases) # self.numOfCases %= DIV_VALUE return self.numOfCases # return self.numOfCases % DIV_VALUE if __name__ == '__main__': [H, W, A, B] = [int(ipt) for ipt in input().split()] start = time.time() fstHlf = FirstHalf(H, W, A, B) sndHlf = SecondHalf(H, W, A, B) totalCases = 0 mid = time.time() # print('mid: {}'.format(mid - start)) for _ in range(H - A): totalCases += ( fstHlf.getNumOfCases() * sndHlf.getNumOfCases() ) totalCases %= DIV_VALUE end = time.time() # print('end: {}'.format(end - mid)) print(totalCases) #print('{} {} {} {}'.format(H, W, A, B)) #print(DIV_VALUE) # for i in range(12): # print('inv: {}'.format(calcModOfInv(i+1, 13)))
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>
s475829799
p04046
Accepted
class cmbs(object): def __init__(self, mod): self.mod = mod self.g1 = [1, 1] self.g2 = [1, 1] inverse = [0, 1] for i in range(2, 10 ** 6 + 1): self.g1.append((self.g1[-1] * i) % mod) inverse.append((-inverse[mod % i] * (mod // i)) % mod) self.g2.append((self.g2[-1] * inverse[-1]) % mod) def cmb(self, n, r): if n > 10 ** 6: return self.cmbl(n, r) return self.cmbr(n, r) def cmbr(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 def cmbl(self, n, r): t = 1 r = min(r, n-r) for i in range(n - r + 1, n + 1): t = t * i % self.mod return t * self.g2[r] % self.mod def main(): H, W, A, B = map(int, input().split()) mod = 10**9 + 7 c = cmbs(mod) r = c.cmb(H+W-2, H-1) for i in range(1, B+1): r -= c.cmb(H-A+i-2, i-1) * c.cmb(A-1+W-i, A-1) return r % mod 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>
s805821572
p04046
Accepted
class nCrMod(): def __init__(self, mod): self.mod = mod self.fac = [1, 1] self.finv = [1, 1] self.inv = [0, 1] def prep(self, n): mod = self.mod f, fi = self.fac[-1], self.finv[-1] for i in range(len(self.fac), n + 1): fn = f * i % mod v = -self.inv[mod % i] * (mod // i) % mod fin = fi * v % mod f, fi = fn, fin self.fac.append(f) self.finv.append(fi) self.inv.append(v) def __call__(self, n, r): if len(self.fac) <= n: self.prep(n) return self.fac[n] * self.finv[r] * self.finv[n - r] % self.mod def main(): H, W, A, B = map(int, input().split()) mod = 10 ** 9 + 7 nCr = nCrMod(mod) r = 0 a = H - A - 1 for i in range(B, W): r = (r + nCr(a + i, i) * nCr(A - 1 + W - i - 1, W - i - 1)) % mod return r 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>
s298845173
p04046
Accepted
class CombinationFermat: def __init__(self): """O(MAX)で前計算しておく→以降comb(a,b)はO(1)で取得可能 """ MOD = 10**9 + 7 MAX = 2*10**5 self.fac = [0]*MAX # self.fac[n]: (n!) mod p self.finv = [0]*MAX # self.finv[n]: (n!)^-1 mod p self.inv = [0]*MAX # inv[n]: (n)^-1 mod -p self.fac[0] = self.fac[1] = 1 self.finv[0] = self.finv[1] = 1 self.inv[1] = 1 for i in range(2, MAX): self.fac[i] = self.fac[i-1] * i % MOD self.inv[i] = MOD - self.inv[MOD % i] * (MOD//i) % MOD self.finv[i] = self.finv[i-1] * self.inv[i] % MOD def comb(self, n: int, r: int) -> int: MOD = 10**9 + 7 if n < r: return 0 if n < 0 or r < 0: return 0 return self.fac[n] * (self.finv[r] * self.finv[n-r] % MOD) % MOD H, W, A, B = map(int, input().split()) c = CombinationFermat() ans = 0 MOD = 10**9 + 7 for i in range(H-A): x = c.comb(B-1+i, i) a = H-1-i b = W-1-B x *= c.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>
s876810494
p04046
Accepted
MOD = 10**9 + 7 MAX = 2*10**5 fac = [0]*MAX # fac[n]: (n!) mod p finv = [0]*MAX # finv[n]: (n!)^-1 mod p inv = [0]*MAX # inv[n]: (n)^-1 mod -p def comb_init(): global fac, finv, inv 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: int, r: int) -> int: global fac, finv if n < r: return 0 if n < 0 or r < 0: return 0 return fac[n] * (finv[r] * finv[n-r] % MOD) % MOD H, W, A, B = map(int, input().split()) comb_init() ans = 0 for i in range(H-A): x = comb(B-1+i, i) 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>
s587846681
p04046
Accepted
class Calc: def __init__(self, max_value, mod): """combination(max_value, all)""" fact = [-1] * (max_value + 1) fact[0] = 1 fact[1] = 1 for x in range(2, max_value + 1): fact[x] = x * fact[x - 1] % mod invs = [1] * (max_value + 1) invs[max_value] = pow(fact[max_value], mod - 2, mod) for x in range(max_value - 1, 0, -1): invs[x] = invs[x + 1] * (x + 1) % mod self.fact = fact self.invs = invs self.mod = mod def combination(self, n, r): if n - r < r: return self.combination(n, n - r) if r < 0: return 0 if r == 0: return 1 if r == 1: return n return self.fact[n] * self.invs[r] * self.invs[n - r] % self.mod def main(): MOD = 10 ** 9 + 7 H, W, A, B = map(int, input().split()) calc = Calc(max_value=H + W, mod=MOD) ans = 0 for x in range(B, W): v = calc.combination(n=(H - A - 1) + x, r=x) u = calc.combination(n=(A - 1) + (W - 1 - x), r=A - 1) # (H-1)-(H-A)+(W-1)-x ans = (ans + v * u) % MOD print(ans) if __name__ == '__main__': main() # import sys # # sys.setrecursionlimit(10 ** 7) # # input = sys.stdin.readline # rstrip() # int(input()) # map(int, input().split())
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>
s810428530
p04046
Accepted
# -*- coding: utf-8 -*- #コンビネーション def cmb(n, r): if (r < 0) or (n < r): return 0 r = min(r, n - r) return fact[n] * factinv[r] * factinv[n-r] % mod N = 2 * 10 ** 5 + 1000 # N は必要分だけ用意する mod = pow(10, 9) + 7 fact = [1, 1] # fact[n] = (n! mod p) factinv = [1, 1] # factinv[n] = ((n!)^(-1) mod p) inv = [0, 1] # factinv 計算用 for i in range(2, N + 1): fact.append((fact[-1] * i) % mod) inv.append((-inv[mod % i] * (mod // i)) % mod) factinv.append((factinv[-1] * inv[-1]) % mod) ans = 0 h, w, a, b = map(int,input().split()) for i in range(h - a): #print(b - 1 + i, i, w - b - 1 + h - i - 1, w - b - 1) ans += (cmb(b - 1 + i, i) * cmb(w - b - 1 + h - i - 1, w - b - 1)) % (10 ** 9 + 7) print(ans % (10 ** 9 + 7))
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>
s475392102
p04046
Accepted
def main(): 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 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) H, W, A, B = map(int, input().split()) ans = cmb((H+W-2), min(H, W)-1, mod) tmp = 0 for i in range(B): t = cmb(H-A-1+i, i, mod) t *= cmb(W-1-i + A - 1, A-1, mod) tmp += t tmp %= mod print((ans - tmp) % mod) 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>
s206436338
p04046
Accepted
from itertools import permutations import sys sys.setrecursionlimit(10 ** 6) from bisect import * from collections import * from heapq import * def II(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def SI(): return sys.stdin.readline()[:-1] def LLI(rows_number): return [LI() for _ in range(rows_number)] int1 = lambda x: int(x) - 1 def MI1(): return map(int1, sys.stdin.readline().split()) def LI1(): return list(map(int1, sys.stdin.readline().split())) p2D = lambda x: print(*x, sep="\n") dij = [(1, 0), (0, 1), (-1, 0), (0, -1)] # grobalにmdを設定すること class mint: def __init__(self, x): self.__x = x % md def __str__(self): return str(self.__x) def __neg__(self): return mint(-self.__x) def __add__(self, other): if isinstance(other, mint): other = other.__x return mint(self.__x + other) def __sub__(self, other): if isinstance(other, mint): other = other.__x return mint(self.__x - other) def __rsub__(self, other): return mint(other - self.__x) def __mul__(self, other): if isinstance(other, mint): other = other.__x return mint(self.__x * other) __radd__ = __add__ __rmul__ = __mul__ def __truediv__(self, other): if isinstance(other, mint): other = other.__x return mint(self.__x * pow(other, md - 2, md)) def __rtruediv__(self, other): return mint(other * pow(self.__x, md - 2, md)) def __pow__(self, power, modulo=None): return mint(pow(self.__x, power, md)) md = 10**9+7 def nCr(com_n, com_r): if com_n < com_r: return 0 return fac[com_n] * ifac[com_r] * ifac[com_n - com_r] n_max = 200005 fac = [mint(1)] for i in range(1, n_max + 1): fac.append(fac[-1] * i) ifac = [mint(1)] * (n_max + 1) ifac[n_max] /= fac[n_max] for i in range(n_max - 1, 1, -1): ifac[i] = ifac[i + 1] * (i + 1) def main(): h,w,a,b=MI() cc=[] for i in range(h-a): cc.append(nCr(b+i,i)) #print(*cc) ans=mint(0) pc=0 for i,c in enumerate(cc): ans+=(c-pc)*nCr(h-1-i+w-1-b,w-1-b) pc=c print(ans) 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>
s691061124
p04046
Accepted
def cmb1(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 h,w,a,b=map(int,input().split()) # 前処理 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) ans=0 for i in range(b,w): ans+=cmb1(h-a-1+i,i,mod)*cmb1(a-1+w-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>
s004713854
p04046
Accepted
class Combination: def __init__(self, size, 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, self.size): self.fact[i] = self.fact[i - 1] * i % self.mod self.inv[i] = -self.inv[self.mod % i] * (self.mod // i) % self.mod self.factInv[i] = self.factInv[i - 1] * self.inv[i] % self.mod def npr(self, n, r): if n < r or n < 0 or r < 0: return 0 return self.fact[n] * self.factInv[n - r] % self.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 nhr(self, n, r): # 重複組合せ return self.ncr(n + r - 1, n - 1) def factN(self, n): if n < 0: return 0 return self.fact[n] H, W, A, B = map(int, input().split()) comb = Combination(H + W + 100) MOD = 10**9 + 7 ans = 0 for w in range(B + 1, W + 1): ans += comb.ncr(H - A + w - 2, w - 1) * comb.ncr(A + W - w - 1, 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>
s336401635
p04046
Accepted
import sys, re, os from collections import deque, defaultdict, Counter from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin, log, log10 from itertools import permutations, combinations, product, accumulate from operator import itemgetter, mul from copy import deepcopy from string import ascii_lowercase, ascii_uppercase, digits from fractions import gcd def debug(*args): if debugmode: print(*args) def input(): return sys.stdin.readline().strip() def STR(): return input() def INT(): return int(input()) def FLOAT(): return float(input()) def MAP(): return map(int, input().split()) def S_MAP(): return map(str, input().split()) def LIST(): return list(map(int, input().split())) def S_LIST(): return list(map(str, input().split())) def lcm(a, b): return a * b // gcd(a, b) sys.setrecursionlimit(10 ** 9) inf = sys.maxsize mod = 10 ** 9 + 7 dx = [0, 0, 1, -1, 1, -1, -1, 1] dy = [1, -1, 0, 0, 1, -1, 1, -1] debugmode = False #コンビネーション def cmb(n, r): if (r < 0) or (n < r): return 0 r = min(r, n - r) return fact[n] * factinv[r] * factinv[n-r] % mod N = 2 * 10 ** 5 + 100 # N は必要分だけ用意する fact = [1, 1] # fact[n] = (n! mod p) factinv = [1, 1] # factinv[n] = ((n!)^(-1) mod p) inv = [0, 1] # factinv 計算用 for i in range(2, N + 1): fact.append((fact[-1] * i) % mod) inv.append((-inv[mod % i] * (mod // i)) % mod) factinv.append((factinv[-1] * inv[-1]) % mod) h, w, a, b = MAP() ans = 0 for i in range(1, h - a + 1): debug(i, b + i - 2, i - 1, w - b + h - i - 1, h - i) debug(cmb(b + i - 2, i - 1), cmb(w - b + h - i - 1, h - i)) ans += cmb(b + i - 2, i - 1) * cmb(w - b + h - i - 1, h - 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>
s194116611
p04046
Accepted
#!usr/bin/env python3 from collections import defaultdict, deque from heapq import heappush, heappop from itertools import permutations, accumulate import sys import math import bisect def LI(): return [int(x) for x in sys.stdin.buffer.readline().split()] def I(): return int(sys.stdin.buffer.readline()) def LS():return [list(x) for x in sys.stdin.readline().split()] def S(): res = list(sys.stdin.readline()) if res[-1] == "\n": return res[:-1] return res 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)] sys.setrecursionlimit(1000000) mod = 1000000007 def solve(): f = [1] N = 1000000 for i in range(1,N+1): f.append(f[-1]*i%mod) inv = [None]*(N+1) inv[N] = pow(f[N],mod-2,mod) for i in range(N)[::-1]: inv[i] = inv[i+1]*(i+1)%mod def comb(a,b): return f[a]*inv[b]*inv[a-b]%mod h,w,a,b = LI() ans = 0 y = h-a x = b+1 while x <= w and y: ans += comb(x+y-2,x-1)*comb(h-y+w-x,w-x)%mod y -= 1 x += 1 print(ans%mod) return #Solve if __name__ == "__main__": 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>
s824602153
p04046
Accepted
memo=[0]*(10**6+1) memo[0]=memo[1]=1 mod=10**9+7 for i in range(2,10**6+1): memo[i]=(memo[i-1]*i)%mod def comb(n,k,p): return (memo[n]*pow(memo[k],p-2,p)*pow(memo[n-k],p-2,p))%mod h,w,a,b=map(int,input().split()) ans=0 for i in range(h-a): ans+=(comb(b+i-1,i,mod)*comb(h-i-1+w-b-1,w-b-1,mod))%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>
s941355053
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)
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>
s800338032
p04046
Accepted
# Function to find modulo inverse of b. It returns # -1 when inverse doesn't # modInverse works for prime m def gcd(b, m): if b == 0: return m return gcd(m%b, b) def modInverse(b,m): g = gcd(b, m) if (g != 1): # print("Inverse doesn't exist") return -1 else: # If b and m are relatively prime, # then modulo inverse is b^(m-2) mode m return pow(b, m - 2, m) # Function to compute a/b under modulo m def modDivide(a,b,m): a = a % m inv = modInverse(b,m) if(inv == -1): print("Division not defined") else: return (inv*a) % m MOD = (10 ** 9) + 7 H, W, A, B = list(map(int, input().split(' '))) DP = [] j = 1 for i in range(1,200002): j *= i j %= MOD DP.append(j) # print(DP) def factorial(i): if (i == 0): return 1 global DP # print(i) # print(i, DP[i-1]) # print(DP[i-1]) return DP[i-1] def move2(H, W, A, B): numPaths = 0 h = H-A w = W-(W-B)+1 a = A+1 pttp = 0 for b in range(w, W+1): # print(h, b, a, W-b+1) ttp = (factorial(h+b-2)*modInverse((factorial(h-1)*factorial(b-1))% MOD, MOD)) % MOD tpttp = ttp ttp -= pttp pttp = tpttp btp = (factorial(a+(W-b+1)-2)*modInverse((factorial(a-1)*factorial(W-b))% MOD, MOD)) % MOD # btp = factorial(a+(W-b+1)-2)//(factorial(a-1)*factorial(W-b)) numPaths += ttp*btp return numPaths ways = move2(H, W, A, B) print(ways % (10**9 + 7))
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>
s902230742
p04046
Accepted
MOD = 10**9+7 def modpow(base, pow, mod): res = 1 while pow > 0: if pow & 1: res = res * base % mod base = base**2 % mod pow >>= 1 return res def modmul(a, b, mod): return a*b % mod def solve(h,w,a,b): fact = [0]*(h+w+1) fact[0] = 1 fact_inv = [0]*(h+w+1) fact_inv[0] = 1 for i in range(1, h+w): fact[i] = fact[i-1]*i % MOD fact_inv[i] = modpow(fact[i], MOD-2, MOD) ans = 0 for col_idx in range(b, w): before_checkpoint = modmul(modmul(fact[h-a-1+col_idx], fact_inv[h-a-1], MOD), fact_inv[col_idx], MOD) from_checkpoint = modmul(modmul(fact[(a-1) + (w-col_idx-1)], fact_inv[w-col_idx-1], MOD), fact_inv[a-1], MOD) ans = (ans + modmul(before_checkpoint, from_checkpoint, MOD)) % MOD return ans if __name__ == "__main__": h,w,a,b = map(int, input().split()) print(solve(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>
s718583588
p04046
Accepted
h, w, a, b = [int(i) for i in input().split()] mod = 10 ** 9 + 7 kkai = [1] for i in range(1, 210000): kkai.append(kkai[-1] * i % mod) def kai(x, p=mod): return kkai[x] def comb(a, b, p=mod): if a < 0 or b < 0: return 0 elif a < b: return 0 c = 1 c *= kai(a, p) c *= pow(kai(b, p), p - 2, p) c *= pow(kai(a - b, p), p - 2, p) return c % p ans = 0 for i in range(b, w): ans += comb(i+(h-a)-1, i)*comb((w-i)+a-2, 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>
s952392467
p04046
Accepted
H,W,A,B = map(int,input().split()) class Combination: """ O(n)の前計算を1回行うことで,O(1)でnCr mod mを求められる n_max = 10**6のとき前処理は約950ms (PyPyなら約340ms, 10**7で約1800ms) 使用例: comb = Combination(1000000) print(comb(5, 3)) # 10 """ def __init__(self, n_max, mod=10 ** 9 + 7): self.mod = mod self.modinv = self.make_modinv_list(n_max) self.fac, self.facinv = self.make_factorial_list(n_max) def __call__(self, n, r): return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n - r] % self.mod def make_factorial_list(self, n): # 階乗のリストと階乗のmod逆元のリストを返す O(n) # self.make_modinv_list()が先に実行されている必要がある fac = [1] facinv = [1] for i in range(1, n + 1): fac.append(fac[i - 1] * i % self.mod) facinv.append(facinv[i - 1] * self.modinv[i] % self.mod) return fac, facinv def make_modinv_list(self, n): # 0からnまでのmod逆元のリストを返す O(n) modinv = [0] * (n + 1) modinv[1] = 1 for i in range(2, n + 1): modinv[i] = self.mod - self.mod // i * modinv[self.mod % i] % self.mod return modinv comb = Combination(1000000) MOD = 10**9+7 ans = 0 for i in range(B,W): ans += comb(H-A-1+i,i)*comb(A-1+W-1-i,A-1) ans %= MOD #print(comb(H-A-1+i,i)*comb(A-1+W-1-i,A-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>
s711753237
p04046
Accepted
#!/usr/bin/env python3 import sys sys.setrecursionlimit(10**8) INF = float("inf") MOD = 1000000007 # type: int class Combination(object): def __init__(self, N, mod=MOD): fac, finv, inv = [0]*(N+1), [0]*(N+1), [0]*(N+1) fac[:2] = 1, 1 finv[:2] = 1, 1 inv[1] = 1 for i in range(2, N+1): fac[i] = fac[i-1]*i % mod inv[i] = -inv[mod % i]*(mod//i) % mod finv[i] = finv[i-1]*inv[i] % mod self.N = N self.MOD = mod self.fac = fac self.finv = finv self.inv = inv def __call__(self, n, k): if n < k: return 0 if n < 0 or k < 0: return 0 fac = self.fac finv = self.finv mod = self.MOD return fac[n] * (finv[k]*finv[n-k] % mod) % mod def solve(H: int, W: int, A: int, B: int): cmb = Combination(H+W+1, MOD) ans = 0 for i in range(B, W): buf = cmb(H-A-1+i, i) buf *= cmb(A+W-i-2, W-i-1) buf %= MOD ans += buf ans %= MOD print(ans) return def main(): def iterate_tokens(): for line in sys.stdin: for word in line.split(): yield word tokens = iterate_tokens() H = int(next(tokens)) # type: int W = int(next(tokens)) # type: int A = int(next(tokens)) # type: int B = int(next(tokens)) # type: int solve(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>
s937115372
p04046
Accepted
H, W, A, B = map(int, input().split()) MOD = 1000000007 fac = [1, 1] inverse = [0, 1] ifac = [1, 1] for i in range(2, H+W): fac.append((fac[-1] * i) % MOD) inverse.append((-inverse[MOD % i] * (MOD // i)) % MOD) ifac.append((ifac[-1] * inverse[i]) % MOD) def f(n): return fac[B+n+H-A-1] * fac[W-B-1-n+A-1] * ifac[B+n] * ifac[H-A-1] * ifac[W-B-1-n] * ifac[A-1] def sigma(func, frm, to): result = 0 for i in range(frm, to+1): result += func(i) return result print(sigma(f, 0, W-B-1)%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>
s669159115
p04046
Accepted
H,W,A,B=map(int,input().split()) MAX_NUM = 2*10**5 + 1 pr = 10**9+7 fac = [0 for _ in range(MAX_NUM)] finv = [0 for _ in range(MAX_NUM)] inv = [0 for _ in range(MAX_NUM)] fac[0] = fac[1] = 1 finv[0] = finv[1] = 1 inv[1] = 1 for i in range(2,MAX_NUM): fac[i] = fac[i-1] * i % pr inv[i] = pr - inv[pr%i] * (pr // i) % pr finv[i] = finv[i-1] * inv[i] % pr def c(n,k): if n < k: return 0 if n < 0 or k < 0: return 0 return fac[n] * (finv[k] * finv[n-k] % pr) % pr a=0 while 1: a=(a+c(H-A-1+B,B)*c(A+W-B-1,A))%pr A+=1 B+=1 if A==H or B==W: break print(a)
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>
s380631765
p04046
Accepted
from operator import mul from functools import reduce def cmb(n, r, p): if (r < 0) or (n < r): return 0 r = min(r, n - r) return fact[n] * factinv[r] * factinv[n-r] % p p = 10 ** 9 + 7 N = 10 ** 6 fact = [1, 1] # fact[n] = (n! mod p) factinv = [1, 1] # factinv[n] = ((n!)^(-1) mod p) inv = [0, 1] for i in range(2, N + 1): fact.append((fact[-1] * i) % p) inv.append((-inv[p % i] * (p // i)) % p) factinv.append((factinv[-1] * inv[-1]) % p) #kari1 = '2 3 1 1' #2 #kari1 = '10 7 3 4' #3570 #kari1 = '100000 100000 99999 99999' #1 #kari1 = '100000 100000 44444 55555' #738162020 """ in1 = kari1.split() """ in1 = input().split() H = int(in1[0]) W = int(in1[1]) A = int(in1[2]) B = int(in1[3]) allCNT = 0 for idx1 in range(W - B): beforeCNT = cmb(H - A + B - 1 + idx1, H - A - 1, p) afterCNT = cmb(W + A - B - 2 - idx1, A - 1, p) allCNT = (allCNT + (beforeCNT * afterCNT) % p) % p print(allCNT)
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>
s651322526
p04046
Accepted
mod = 10 ** 9 + 7 def extGCD(a, b): if b == 0: return a, 1, 0 g, y, x = extGCD(b, a%b) y -= a//b * x return g, x, y def moddiv(a, b): _, inv, _ = extGCD(b, mod) return (a * inv) % mod N = 2 * 10 ** 5 + 10 fact = [0] * (N) fact[0] = 1 for i in range(1, N): fact[i] = (fact[i-1] * i) % mod def comb(a, b): return moddiv(moddiv(fact[a], fact[a-b]), fact[b]) h, w, a, b = map(int, input().split()) ans = 0 for i in range(min(h-a, w-b)): x = h - 1 - a - i y = b + i plus = comb(x + y, x) * comb((h-1 - x) + (w-1 - y), (h-1 - x)) ans += plus 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>
s866868141
p04046
Accepted
h, w, a, b = map(int, input().split()) MOD = 1000000007 def modPow(a, x, p): res = 1 while (x > 0): if (x % 2 != 0): res = (res * a) % p a = (a * a) % p x /= 2 return res fact = [None] * 220000 for i in range(1, 220000): fact[0] = 1 fact[i] = i * fact[i - 1] % MOD # inv[i] = modPow(fact[i], MOD-2, MOD) def ncr(n, r): den = fact[n - r] * fact[r] % MOD return fact[n] * pow(den, MOD - 2, MOD) % MOD def number_of_paths(h, w): n = h + w - 2 r = h - 1 return ncr(n, r) ans = 0 for i in range(b + 1, w + 1): # print('first', h-a, i) ans += number_of_paths(h - a, i) * number_of_paths(a, w - i + 1) print(int(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>
s075777515
p04046
Accepted
h,w,a,b = map(int, input().split()) def find_power(n,mod=10**9+7): powlist=[0]*(n+1) powlist[0]=1 powlist[1]=1 for i in range(2,n+1): powlist[i]=powlist[i-1]*i%(mod) return powlist def find_inv_power(n): powlist=find_power(n) check=powlist[-1] first=1 uselist=[0]*(n+1) secondlist=[0]*30 secondlist[0]=check secondlist[1]=check**2 for i in range(28): secondlist[i+2]=(secondlist[i+1]**2)%(10**9+7) a=format(10**9+5,"b") for j in range(30): if a[29-j]=="1": first=(first*secondlist[j])%(10**9+7) uselist[n]=first for i in range(n,0,-1): uselist[i-1]=(uselist[i]*i)%(10**9+7) return uselist mod = 10**9+7 p_lis=find_power(4*10**5+100,mod) ip_lis=find_inv_power(4*10**5+100) def comb(n,r,mod=10**9+7): if n<r: return 0 elif n>=r: return (p_lis[n]*ip_lis[r]*ip_lis[n-r])%(mod) ans=0 for i in range(h-a): pre=comb(i+b-1,i) post=comb(h+w-b-2-i,h-i-1) ans+=pre*post ans%=10**9+7 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>
s907387093
p04046
Accepted
import sys input = sys.stdin.readline def I(): return int(input()) def MI(): return map(int, input().split()) def LI(): return list(map(int, input().split())) def main(): mod=10**9+7 H,W,A,B=MI() N=H+W def cmb(n, r, mod): if (r < 0) or (n < r): return 0 r = min(r, n - r) return (fact[n] * factinv[r] * factinv[n-r])%mod fact=[1,1] factinv=[1,1] inv=[0,1] for i in range(2, N + 1): fact.append((fact[-1] * i) % mod) inv.append((-inv[mod % i] * (mod // i)) % mod) factinv.append((factinv[-1] * inv[-1]) % mod) ans=cmb(N-2,H-1,mod) for i in range(min(A,B)): h=H-A+i+1 w=B-i #print(h,w,cmb(h+w-2,h-1,mod),cmb(H+W-(h+w),H-h,mod)) ans-=cmb(h+w-2,h-1,mod)*cmb(H+W-(h+w),H-h,mod) ans%=mod print(ans%mod) 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>
s337405092
p04046
Accepted
N=2*10**5+3 mod=10**9+7 fac=[1]*(N+1) for i in range(1,N+1): fac[i]=fac[i-1]*i%mod inv_fac=[1]*(N+1) inv_fac[N]=pow(fac[N],mod-2,mod) for i in range(N-1,0,-1): inv_fac[i]=inv_fac[i+1]*(i+1)%mod def nCr(n,r): if n<0 or r<0 or r>n: return 0 return fac[n]*inv_fac[r]%mod*inv_fac[n-r]%mod h,w,a,b=map(int,input().split()) ans=nCr(h+w-2,h-1) for i in range(b): ans=(ans-nCr(h-a+i-1,i)*nCr(a+w-i-2,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>
s715567132
p04046
Accepted
class Cmber: def __init__(self,gensize,mod=10**9+7): self.mod = mod self.g1=[1,1] self.g2 = [1, 1] #逆元テーブル self.inverse = [0, 1] #逆元テーブル計算用テーブル self.getgen(gensize) def getgen(self,N): for i in range( 2, N + 1 ): self.g1.append( ( self.g1[-1] * i ) % self.mod ) self.inverse.append( ( -self.inverse[self.mod % i] * (self.mod//i) ) % self.mod ) self.g2.append( (self.g2[-1] * self.inverse[-1]) % self.mod ) def cmb(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 def cmb_very_high_n(self,n, r): if ( r<0 or r>n ): return 0 g1=1 r = min(r, n-r) for i in range(n-r+1,n+1): g1*=i g1%=self.mod return g1 * self.g2[r] % self.mod def resolve(): H,W,A,B= map(int,input().split()) cmb= Cmber(H+W+1) sm =0 for i in range(H-A): bef = cmb.cmb(B+i-1,i) r=W-B-1 u=H-i-1 aft = cmb.cmb(r+u,u) sm += (aft*bef) % 1000000007 sm %= 1000000007 # print(i,r,u,bef,aft) print(sm) if __name__ == "__main__": resolve()
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>
s989067102
p04046
Accepted
h,w,a,b=map(int,input().split()) N=h+w mod=10**9+7 fac=[1]*(N+2) inv=[1]*(N+2) t=1 for i in range(1,N+2): t*=i t%=mod fac[i]=t t=pow(fac[N+1],mod-2,mod) for i in range(N+1,0,-1): inv[i]=t t*=i t%=mod def comb(n,r): return fac[n]*inv[n-r]*inv[r]%mod c=[0] for i in range(h-a): c.append(comb(b+i,i)) ans=0 for i in range(h-a): ans+=(c[i+1]-c[i])*comb(w-b+h-i-2,w-b-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>
s985696220
p04046
Accepted
h,w,a,b=map(int,input().split()) mod=10**9+7 ans=0 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 mod = 10**9+7 #出力の制限 N = h+w+1 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 ) #a = cmb(n,r,mod) for i in range(h-a): p=(i)+(b-1) q=(h-i-1)+((w-b)-1) ans+=cmb(p,i,mod)*cmb(q,h-i-1,mod) ans%=mod #print(p,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>
s435632479
p04046
Accepted
import math h, w, a, b = map(int, input().split()) #divmod mod = pow(10,9)+7 def divmod(num, mod=10**9+7): return pow(num, mod-2, mod) #combination def comb(a,b): p=fact[a-b]*fact[b]%mod return fact[a]*divmod(p)%mod fact = [1] for i in range(1,h+w): fact.append(i*fact[i-1]%mod) #print(fact) ans = 0 for j in range(h-a): tmp = comb(b-1+j,j)*comb(w+h-b-2-j,w-b-1)%mod ans += tmp ans = ans%mod # ans = ans + fact[b-1+j]*fact[w+h-b-2-j]/fact[b-1]/fact[j]/fact[w-b-1]/fact[h-1-j] print(int(ans)) ''' 単純なh*wのマス目だった場合 #h-row / w-col con = [[0]*w]*h for i in range(h): con[i][0] = 1 for j in range(w): con[0][j] = 1 for k in range(1,h): for l in range(1,w): con[k][l] = con[k-1][l] + con[k][l-1] print(con[h-1][w-1]) ''' ''' 全量を計算するやり方 H*Wがデカすぎると時間というよりもメモリエラー print('initialize') con = [[0 for i in range(w)] for j in range(h)] #リスト内包表記で作らないとバグる #ダメな例 -> con = [[0]*w]*h #https://qiita.com/utgwkk/items/5ad2527f19150ae33322 print('initialize finished') for i in range(h-a): con[i][0] = 1 #print(con[0]) for j in range(w): #wは先に初期化やっちゃう con[0][j] = 1 #print(con) for k in range(1,h-a): # print(k) for l in range(1,b+1): con[k][l] = con[k-1][l] + con[k][l-1] #print(con[h-a-1][b-1]) #print(con[h-a][b-1]) #print(con[h-a-1][b]) for m in range(1,h): # print('---{} row---'.format(m)) # print(con[m]) for n in range(b,w): # print('m,n:('+str(m)+','+str(n)+')') con[m][n] = con[m-1][n] + con[m][n-1] # print(con[m]) print(con[h-1][w-1]) '''
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>
s530108711
p04046
Accepted
class ModInt: def __init__(self, num, mod): self.num = num self.mod = mod def __str__(self): return str(self.num) def __repr__(self): return "ModInt(num: {}, mod: {}".format(self.num, self.mod) def __add__(self, other): ret = self.num + other.num ret %= self.mod return ModInt(ret, self.mod) def __sub__(self, other): ret = self.num - other.num ret %= self.mod return ModInt(ret, self.mod) def __mul__(self, other): ret = self.num * other.num ret %= self.mod return ModInt(ret, self.mod) def pow(self, times): pw = pow(self.num, times, self.mod) return ModInt(pw, self.mod) def inverse(self): return self.pow(self.mod - 2) def __truediv__(self, other): num = self * other.inverse() return ModInt(num, self.mod) h, w, a, b = map(int, input().split()) mod = 10 ** 9 + 7 fact = [ModInt(1, mod)] inv = [ModInt(1, mod).inverse()] * (h + w - 1) def comb(n, r): return fact[n] * inv[r] * inv[n-r] for i in range(1, h + w - 1): fact.append(fact[-1] * ModInt(i, mod)) inv[h+w-2] = fact[-1].inverse() for i in range(h + w - 2, 0, -1): inv[i-1] = inv[i] * ModInt(i, mod) ans = ModInt(0, mod) for hi in range(h - a): ans += comb(hi + b - 1, hi) * comb(h - hi - 1 + w - b - 1, w - 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>
s500868525
p04046
Accepted
mod=10**9+7 h,w,a,b=map(int,input().split()) def comb(a,b): p=fac[a-b]*fac[b]%mod return fac[a]*pow(p,mod-2,mod)%mod fac=[1] for i in range(h+w): fac.append(fac[-1]*(i+1)%mod) ans=0 for i in range(w-b): p=comb(h-a-1+b+i,b+i)*comb(w-b-i-2+a,a-1) ans+=p%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>
s091927857
p04046
Accepted
import math from functools import reduce from collections import deque import sys sys.setrecursionlimit(10**7) # スペース区切りの入力を読み込んで数値リストにして返します。 def get_nums_l(): return [ int(s) for s in input().split(" ")] # 改行区切りの入力をn行読み込んで数値リストにして返します。 def get_nums_n(n): return [ int(input()) for _ in range(n)] # 改行またはスペース区切りの入力をすべて読み込んでイテレータを返します。 def get_all_int(): return map(int, open(0).read().split()) def log(*args): print("DEBUG:", *args, file=sys.stderr) def init_kaijo(MAX, MOD): kaijo = [0] * MAX kaijo[0] = 1 for i in range(1, len(kaijo)): kaijo[i] = kaijo[i-1] * i % MOD return kaijo MOD = 10**9+7 kaijo = init_kaijo(500000, MOD) def inv(x, MOD): return pow(x, MOD-2, MOD) def nPk(n, k, MOD): return kaijo[n] * inv(kaijo[n-k], MOD) % MOD def nCk(n, k, MOD): # log("{}C{} = {}".format(n,k,nPk(n,k, MOD) * inv(kaijo[k], MOD))) return nPk(n,k, MOD) * inv(kaijo[k], MOD) h, w, a, b = get_nums_l() ans = 0 for i in range(h-a): # (0,0)から(b-1, i)に到着するパターン数 * (b,i)から(W-1, H-1)に到着するパターン数 # log(nCk(b+i-1, i, MOD), nCk(w-b+h-i-2, h-i-1, MOD)) ans += nCk(b+i-1, i, MOD) * nCk(w-b+h-i-2, h-i-1, MOD) % 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>
s743875436
p04046
Accepted
# D - いろはちゃんとマス目 def cmb1(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 h,w,a,b=map(int,input().split()) # 前処理 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) ans = 0 for i in range(b, w): ans+=cmb1(i+h-1-a,i,mod)*cmb1(w-1-i+a-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>
s038983669
p04046
Accepted
h, w, a, b = map(int, input().split()) def cmb1(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 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 ) ans = 0 for i in range(b, w): ans += cmb1(i+h-1-a, i, mod)*cmb1(w-1-i+a-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>
s816650220
p04046
Accepted
def main(): H, W, A, B = (int(i) for i in input().split()) m = H + W + 3 MOD = 10**9 + 7 fac = [0] * m finv = [0] * m inv = [0] * m def COMBinitialize(m): fac[0] = 1 finv[0] = 1 if m > 1: fac[1] = 1 finv[1] = 1 inv[1] = 1 for i in range(2, m): 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, k): if n < k: return 0 if n < 0 or k < 0: return 0 return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD COMBinitialize(m) ans = 0 for x in range(W-B): if x == 0: p = COMB(H-A-1 + B+x, B+x) else: p = COMB(H-A-1 + B+x, B+x) - COMB(H-A-1 + B+x-1, B+x-1) q = COMB(A+W-1-B-x, A) ans += p*q ans %= 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>
s313640545
p04046
Accepted
MOD = 10**9+7 fac = [1 for k in range(200010)] inv = [1 for k in range(200010)] finv = [1 for k in range(200010)] for k in range(2,200010): fac[k] = (fac[k-1]*k)%MOD inv[k] = (MOD - inv[MOD%k] * (MOD // k))%MOD finv[k] = (finv[k - 1] * inv[k]) % MOD; def nCr(n,r): return (fac[n]*finv[r]*finv[n-r])%MOD H, W, A, B = map(int,input().split()) ans = 0 for k in range(W-B): ans += nCr(A+W-B-k-2,A-1)*nCr(B+k+1+H-A-2,H-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>
s348368747
p04046
Accepted
class Combination(): # コンストラクタ def __init__(self, N:int, P:int): self.N = N self.P = P # fact[i] = (i! mod P) self.fact = [1, 1] # factinv[i] = ((i!)^(-1) mod P) self.factinv = [1, 1] # factinv 計算用 self.inv = [0, 1] for i in range(2, N+1): self.fact.append((self.fact[-1] * i) % P) self.inv.append((-self.inv[P % i] * (P // i)) % P) self.factinv.append((self.factinv[-1] * self.inv[-1]) % P) # nCk (mod P) (ただし、n<=N) def getComb(self, n:int, k:int): if (k < 0) or (n < k): return 0 k = min(k, n - k) return self.fact[n] * self.factinv[k] * self.factinv[n-k] % self.P def main(): H,W,A,B = map(int,input().split()) MOD = 10**9 + 7 COMB = Combination(H+W, MOD) # (1,1) -> (i,B-1) -> (i,B+1) -> (H,W) ans = 0 for i in range(1,H-A+1): tmp = COMB.getComb((i-1)+(B-1), i-1) tmp *= COMB.getComb((H-i)+(W-B-1), H-i) ans = (ans + tmp) % 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>
s669878261
p04046
Accepted
H,W,A,B = map(int, input().split()) 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 mod = 10**9+7 #出力の制限 g1 = [1, 1] # 元テーブル g2 = [1, 1] #逆元テーブル inverse = [0, 1] #逆元テーブル計算用テーブル for i in range( 2, H+W + 1 ): g1.append( ( g1[-1] * i ) % mod ) inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod ) g2.append( (g2[-1] * inverse[-1]) % mod ) ans = 0 for h in range(1, H-A+1): ans += cmb(h-1+B-1, h-1, mod)*cmb(H-h+W-B-1, H-h, 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>
s077226498
p04046
Accepted
import sys def comb(n, r, fact, revfact, mod): return (fact[n] * revfact[n-r] * revfact[r]) % mod def solve(): H, W, A, B = map(int, input().split()) mod = 7 + 10 ** 9 fact = [1] * (H + W + 4) revfact = [1] * (H + W + 4) for i in range(1, H + W + 4): fact[i] = (i * fact[i-1]) % mod revfact[H + W + 3] = pow(fact[H + W + 3], mod - 2, mod) for i in reversed(range(1, H + W + 3)): revfact[i] = ((i + 1) * revfact[i + 1]) % mod ans = 0 for j in range(B + 1, W + 1): up = comb(H - A + j - 2, j - 1, fact, revfact, mod) down = comb(A + W - j - 1, A - 1, fact, revfact, mod) ans += (up * down) % mod ans %= mod print(ans) return 0 if __name__ == "__main__": 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>
s161678019
p04046
Accepted
import sys mod = pow(10, 9) + 7 sys.setrecursionlimit(pow(10, 8)) 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-1)//2)**2 * x % mod def mul(a, b): return ((a % mod) * (b % mod)) % mod def div(a, b): return mul(a, power(b, mod-2)) def div2(a, b): return mul(a, modinv(b)) def modinv(a): b, u, v = mod, 1, 0 while b: t = a//b a, u = a-t*b, u-t*v a, b, u, v = b, a, v, u u %= mod return u 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 NNN = (10**6) g1 = [1, 1] g2 = [1, 1] inverse = [0, 1] for i in range( 2, NNN + 1 ): g1.append( ( g1[-1] * i ) % mod ) inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod ) g2.append( (g2[-1] * inverse[-1]) % mod ) H, W, A, B = map(int, input().split()) r = 0 for i in range(H-A): # (0, 0) -> (i, B-1) # (i, B) -> (H-1, W-1) r = (r + mul(cmb(i+B-1, i), cmb(H-1-i+W-1-B, H-1-i)))%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>
s058736282
p04046
Accepted
class Combination: """ SIZEが10^6程度以下の二項係数を何回も呼び出したいときに使う 使い方: comb = Combination(SIZE, MOD) comb(10, 3) => 120 """ def __init__(self, N, MOD=10 ** 9 + 7): self.MOD = MOD self.fact, self.inv = self._make_factorial_list(N) def __call__(self, n, k): if k < 0 or k > n: return 0 res = self.fact[n] * self.inv[k] % self.MOD res = res * self.inv[n - k] % self.MOD return res def _make_factorial_list(self, N): fact = [1] * (N + 1) inv = [1] * (N + 1) MOD = self.MOD for i in range(1, N + 1): fact[i] = (fact[i - 1] * i) % MOD inv[N] = pow(fact[N], MOD - 2, MOD) for i in range(N, 0, -1): inv[i - 1] = (inv[i] * i) % MOD return fact, inv if __name__ == "__main__": H, W, A, B = map(int, input().split()) mod = 10 ** 9 + 7 comb = Combination(2 * 10 ** 5 + 10, mod) ans = 0 x = H - A y = B + 1 while x > 0 and y <= W: path1 = comb(x + y - 2, x - 1) path2 = comb(H - x + W - y, H - x) ans = (ans + path1*path2) % mod x -= 1 y += 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>
s638339919
p04046
Accepted
MAX = 2*10**5+100 fact = [0]*MAX inv = [0]*MAX finv = [0]*MAX def C_init(): fact[0] = 1 fact[1] = 1 finv[0] = 1 finv[1] = 1 inv[1] = 1 for i in range(2, MAX): fact[i] = fact[i-1]*i%MOD inv[i] = MOD-inv[MOD%i]*(MOD//i)%MOD finv[i] = finv[i-1]*inv[i]%MOD def C(n, r): if n<r: return 0 if n<0 or r<0: return 0 return fact[n]*(finv[r]*finv[n-r]%MOD)%MOD H, W, A, B = map(int, input().split()) MOD = 10**9+7 C_init() ans = 0 for i in range(H-A): ans += C(i+B-1, i)*C(H-1-i+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>
s740150393
p04046
Accepted
h, w, a, b = map(int, input().split()) mod = 10**9 + 7 fac = [1, 1] inv = [1, 1] finv = [1, 1] for i in range(2, h+w+5): fac.append(fac[i-1] * i % mod) inv.append(mod - inv[mod%i] * (mod//i) % mod) finv.append(finv[i-1] * inv[i] % mod) 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] % mod) % mod ans = 0 for i in range(h-a): ans += nck(i+b-1, b-1) * nck(h-1-i + w-b-1, w-b-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>
s887423083
p04046
Accepted
class Combination: """ SIZEが10^6程度以下の二項係数を何回も呼び出したいときに使う 使い方: comb = Combination(SIZE, MOD) comb(10, 3) => 120 """ def __init__(self, N, MOD=10 ** 9 + 7): self.MOD = MOD self.fact, self.inv = self._make_factorial_list(N) def __call__(self, n, k): if k < 0 or k > n: return 0 res = self.fact[n] * self.inv[k] % self.MOD res = res * self.inv[n - k] % self.MOD return res def _make_factorial_list(self, N): fact = [1] * (N + 1) inv = [1] * (N + 1) MOD = self.MOD for i in range(1, N + 1): fact[i] = (fact[i - 1] * i) % MOD inv[N] = pow(fact[N], MOD - 2, MOD) for i in range(N, 0, -1): inv[i - 1] = (inv[i] * i) % MOD return fact, inv if __name__ == "__main__": H, W, A, B = map(int, input().split()) mod = 10**9+7 comb = Combination(2*10**5+10, mod) ans = 0 h1 = H - A w1 = B + 1 while True: X = comb(h1 + w1 - 2, h1 - 1) h2 = H - h1 w2 = W - w1 Y = comb(h2 + w2, h2) ans += X * Y % mod ans %= mod h1 -= 1 w1 += 1 if h1 <= 0 or w1 > W: break 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>
s778888518
p04046
Accepted
h,w,a,b = map(int, input().split()) mx = max(h,w) mod = 10**9+7 fac = [1]*(h+w+1) for i in range(1,h+w+1): fac[i]=fac[i-1]*i%mod rev = [1]*(mx+1) rev[-1] = pow(fac[mx], mod-2, mod) for i in range(mx-1, -1, -1): rev[i] = rev[i+1]*(i+1)%mod const = rev[h-a-1]*rev[a-1]%mod ans = 0 for i in range(b,w): ans += fac[h-a+i-1]*rev[i]*fac[a+w-2-i]*rev[w-i-1]%mod print(ans*const%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>
s479470866
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>
s147744319
p04046
Accepted
class Factorial: def __init__(self, n, mod=10**9+7): self.fac = [0] * (n+1) self.ifac = [0] * (n+1) self.fac[0] = 1 self.ifac[0] = 1 self.mod = mod modmod = self.mod - 2 for i in range(n): self.fac[i+1] = self.fac[i] * (i+1) % self.mod self.ifac[i+1] = self.ifac[i] * pow(i+1, modmod, self.mod) % self.mod def comb(self, n, r): if n == 0 and r == 0: return 1 if n < r or n < 0: return 0 tmp = self.ifac[n-r] * self.ifac[r] % self.mod return tmp * self.fac[n] % self.mod def perm(self, n, r): if n == 0 and r == 0: return 1 if n < r or n < 0: return 0 return (self.fac[n] * self.ifac[n-r]) % self.mod h,w,a,b = map(int, input().split()) mod = 10**9+7 fact = Factorial(h+w+1) ans = 0 c,d = h-a, w-b for i in range(b+1, w+1): d = w-i+1 # print(i,i+c-2, c-1, fact.comb(i+c-2, c-1), fact.comb(a+d-2, a-1)) ans = (ans + fact.comb(i+c-2, c-1) * fact.comb(a+d-2, 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>
s633470199
p04046
Accepted
H,W,A,B = map(int,input().split()) mod = 10**9+7 x1 = [1 for i in range(H+W)] xi = [1 for i in range(H+W)] num = 1 def power2(a,b,p): if b == 0: return 1 if b%2 == 0: d = power2(a,b//2,p) return (d*d)%p return (a*power2(a,b-1,p))%p for i in range(1,H+W): num = (num*i)%mod x1[i] = num for i in range(1,H+W): x2 = power2(x1[i],mod-2,mod) xi[i] = x2 def comb(a,b,p): return (x1[a]*xi[b]*xi[a-b])%p ans = 0 for w in range(B,W): ans = (ans+ (comb(H-A+w-1,w,mod)*comb(A+W-w-2,A-1,mod))%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>
s585366330
p04046
Accepted
import sys input = sys.stdin.buffer.readline import copy def main(): H,W,A,B = map(int,input().split()) MOD = 10**9+7 fac = [0 for _ in range(H+W+1)] fac[0],fac[1] = 1,1 inv = copy.deepcopy(fac) invfac = copy.deepcopy(fac) for i in range(2,H+W+1): fac[i] = (fac[i-1]*i)%MOD inv[i] = MOD-(MOD//i)*inv[MOD%i]%MOD invfac[i] = (invfac[i-1]*inv[i])%MOD def coef(x,y): num = (((fac[x+y]*invfac[x])%MOD)*invfac[y]%MOD) return num ans = 0 for i in range(H-A): able = coef(i,B-1)*coef(H-i-1,W-B-1) ans += able 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>
s431447671
p04046
Accepted
H, W, A, B = map(int, input().split()) mod = 10**9+7 F = [1]*200010 p = 1 for i in range(1, len(F)): F[i] = p = p*i%mod def comb(n, k): return F[n]*pow(F[n-k], mod-2, mod)*pow(F[k], mod-2, mod)%mod ans = 0 for h in range(H-A): x = comb(B-1+h, h) # 右にB-1回、下にh回移動する方法の数 y = comb(W+H-B-h-2, H-h-1) # 右にW-B-1回、下にH-h-1回移動する方法の数 ans += x*y 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>
s531459836
p04046
Accepted
mod = 10**9 + 7 h, w, a, b = map(int, input().split()) fact = [1] * (h + w + 1) invf = [1] * (h + w + 1) invn = [1] * (h + w + 1) for i in range(2, h + w + 1): fact[i] = fact[i-1] * i % mod invn[i] = (-invn[mod % i]) * (mod // i) % mod invf[i] = invf[i-1] * invn[i] % mod count = 0 for i in range(min(h-a, w-b)): count += fact[h+b-a-1] * invf[h-a-i-1] * invf[b+i] * fact[w+a-b-1] * invf[a+i] * invf[w-b-i-1] count %= mod print(count)
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>
s697581947
p04046
Accepted
H,W,A,B = map(int,input().split()) 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 N = 10**6 #出力の制限 mod = 10**9+7 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 path(a,b): h=b[0]-a[0] w=b[1]-a[1] return cmb(h+w,h,mod) st,gl = [0,0],[H-1,W-1] ans = path(st,gl) for i in range(A): ans-=(path(st,[H-i-1,B-1])*path([H-i-1,B],gl))%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>
s849254979
p04046
Accepted
import sys read = sys.stdin.read readline = sys.stdin.readline readlines = sys.stdin.readlines MOD = 10**9+7 fac = [1, 1] f_inv = [1, 1] inv = [0, 1] def modcmb(n, r, mod): if n < 0 or r < 0 or r > n: return 0 return fac[n] * f_inv[r] * f_inv[n-r] % mod def main(): H,W,A,B = map(int, readline().split()) n = H+W+1 for i in range(2,n): fac.append((fac[-1] * i) % MOD) inv.append((-inv[MOD % i] * (MOD//i)) % MOD) f_inv.append((f_inv[-1] * inv[-1]) % MOD) ans = 0 for i in range(H-A): ans += modcmb(B-1+i, B-1, MOD) * modcmb(H+W-B-2-i, W-1-B, MOD) % 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>