submission_id string | problem_id string | status string | code string | input string | output string | problem_description string |
|---|---|---|---|---|---|---|
s679471288 | p04046 | Time Limit Exceeded | #!usr/bin/env python3
from collections import defaultdict
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
mod = 1000000007
#A
"""
def check(n):
n = list(map(int,list(str(n))))
s = 0
for i in n:
s += f[i]
return s == 0
n,k = LI()
d = LI()
f = [0 for i in range(10)]
for i in d:
f[i] = 1
while 1:
if check(n):
print(n)
quit()
n += 1
"""
#B
def f(x,y):
return fact[x-1+h-y]*fact[y-1+w-x]*inv_fact[x-1]*inv_fact[y-1]*inv_fact[w-x]*inv_fact[h-y]%mod
h,w,a,b = LI()
fact = [1]
for i in range(h+w):
fact.append(fact[-1]*(i+1)%mod)
inv_fact = [pow(fact[-1],mod-2,mod)]
for i in range(h+w):
inv_fact.insert(0,inv_fact[0]*(h+w-i)%mod)
ans = 0
for i in range(1,min(w-b,h-a)+1):
ans += f(b+i,a+i)
ans %= mod
print(ans)
#C
#D
#E
#F
#G
#H
#I
#J
#K
#L
#M
#N
#O
#P
#Q
#R
#S
#T
| 2 3 1 1
| 2
| <span class="lang-en">
<p>Score : <var>400</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns.
Iroha is now standing in the top-left cell.
She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p>
<p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p>
<p>Find the number of ways she can travel to the bottom-right cell.</p>
<p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var> 1 ≦ H, W ≦ 100,000</var></li>
<li><var> 1 ≦ A < H</var></li>
<li><var> 1 ≦ B < 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> |
s127325022 | p04046 | Time Limit Exceeded | import math
def solve(H, W, A, B):
ans = 0
R = 10 ** 9 + 7
p1_y = math.factorial(H - 1 - A)
d2_y = math.factorial(H - 1 - (H - A))
way1 = -1
way3 = -1
for i in range(0, W - B):
p1 = (B + i, H - 1 - A)
p2 = (B + i, H - A)
e = (W - 1, H - 1)
d2 = (e[0] - p2[0], e[1] - p2[1])
if way1 == -1:
way1 = (math.factorial(p1[0] + p1[1]) // math.factorial(p1[0]) // p1_y)
else:
way1 *= (p1[0] + p1[1])
way1 //= (p1[0])
if way3 == -1:
way3 = (math.factorial(d2[0] + d2[1]) // math.factorial(d2[0]) // d2_y)
else:
way3 *= (d2[0] + 1)
way3 //= (d2[0] + d2[1] + 1)
ans += (way1 % R) * (way3 % R)
return ans % R
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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s422788859 | p04046 | Time Limit Exceeded | import math
def solve(H, W, A, B):
ans = 0
R = 10 ** 9 + 7
p1_y = math.factorial(H - 1 - A)
d2_y = math.factorial(H - 1 - (H - A))
way1 = -1
way3 = -1
for i in range(0, W - B):
p1 = (B + i, H - 1 - A)
p2 = (B + i, H - A)
e = (W - 1, H - 1)
d2 = (e[0] - p2[0], e[1] - p2[1])
if way1 == -1:
way1 = (math.factorial(p1[0] + p1[1]) // math.factorial(p1[0]) // p1_y)
else:
way1 *= (p1[0] + p1[1])
way1 //= (p1[0])
if way3 == -1:
way3 = (math.factorial(d2[0] + d2[1]) // math.factorial(d2[0]) // d2_y)
else:
way3 *= (d2[0] + 1)
way3 //= (d2[0] + d2[1] + 1)
ans += (way1 % R) * (way3 % R)
return ans % R
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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s930978160 | p04046 | Time Limit Exceeded | H, W, A, B = map(int, input().split())
mod = 10 ** 9 + 7
ans = 0
fac = [1 for _ in range(10**6+1)]
for i in range(1, 10 **6):
fac[i+1] = fac[i] * (i + 1)
def combination(n, r):
result = fac[n] // (fac[r] * fac[n-r])
return result
for i in range(B, W):
ans += (combination(H - A - 1 + i, i) * combination(A - 1 + W - i - 1, W - i -1))
ans %= mod
print(ans)
| 2 3 1 1
| 2
| <span class="lang-en">
<p>Score : <var>400</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns.
Iroha is now standing in the top-left cell.
She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p>
<p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p>
<p>Find the number of ways she can travel to the bottom-right cell.</p>
<p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var> 1 ≦ H, W ≦ 100,000</var></li>
<li><var> 1 ≦ A < H</var></li>
<li><var> 1 ≦ B < 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> |
s357248653 | p04046 | Time Limit Exceeded | H,W,A,B=map(int,input().split())
power=[]
for tate in range(H):
x= []
for yoko in range(W):
x.append(0)
power.append(x)
power[0][0]+=1
def calc(power,x,y):
if y==0 and x==0:return power
if y==0:ue=0
if y!=0:ue=power[y-1][x]
if x==0:hidari=0
if x!=0:hidari=power[y][x-1]
if y>=H-A and x < B:
power[y][x]=0
if y<H-A or x >=B:
power[y][x]=ue+hidari
return power
for y in range(H):
for x in range(W):
power=calc(power,x,y)
result= power[H-1][W-1]%(7+10**9)
print(result) | 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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s674042913 | p04046 | Time Limit Exceeded | H, W, A, B = map(int, input().split())
dp = [[0] * W for _ in range(H)]
dp[0][0] = 1
for k in range(1, H + W):
for y in range(max(H, W)):
x = k - y
if 0 <= y < H and 0 <= x < W:
if y < H - A or B <= x:
if x - 1 >= 0:
dp[y][x] += dp[y][x - 1]
if y - 1 >= 0:
dp[y][x] += dp[y - 1][x]
MOD = 1000000007
print(dp[H - 1][W - 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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s076405041 | p04046 | Time Limit Exceeded | import math
def combinations_count(n, r):
return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
H,W,A,B=map(int,raw_input().split())
Corner=[[B+i,A+i] for i in range(1,min(W-B,H-A)+1)]
Result=0
for c in Corner:
Result+=combinations_count(c[0]-1+H-c[1],c[0]-1)*combinations_count(W-c[0]+c[1]-1,c[1]-1)
print Result%(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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s969022599 | p04046 | Time Limit Exceeded | # calculate N!(modP)
def fac(n):
ans = 1
mod = 10**9+7
for i in range(1, n+1):
ans *= i
ans %= mod
return ans
# calculate a**b(modP)
def cal(a, b, mod):
if b == 0:
return 1
elif b % 2 == 0:
return cal(a, b//2, mod)**2 % mod
else:
return (a * cal(a, b-1, mod)) % mod
H, W, A, B = map(int, input().split())
ans = 0
mod = 10**9+7
for w in range(B, W):
now = fac((H-A-1)+w) * cal(fac(H-A-1), mod-2, mod) * cal(fac(w), mod-2, mod) % mod
now *= fac((A-1)+(W-w-1)) * cal(fac(A-1), mod-2, mod) * cal(fac(W-w-1), mod-2, mod) % mod
ans = (ans+now) % 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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s972090134 | p04046 | Time Limit Exceeded | K = 10 ** 9 + 7
def pow_K(x, n):
if n == 0:
return 1
else:
return (pow_K(x, n // 2) ** 2 * x ** (n % 2)) % K
H, W, A, B = map(int, input().split())
fact = [0 for i in range(H+W-1)]
fact[0] = 1
for i in range(H+W-2):
fact[i + 1] = fact[i] * (i+1) % K
fact_inv = [pow_K(fact[i], K-2) for i in range(H+W-1)]
r = 0
for i in range(B, W):
r += ((fact[H-A-1+i] * fact_inv[H-A-1] %K)*fact_inv[i] % K) * ((fact[W-1-i+A-1] * fact_inv[W-1-i] %K) * fact_inv[A-1] % K) %K
r %= K
print(r)
| 2 3 1 1
| 2
| <span class="lang-en">
<p>Score : <var>400</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns.
Iroha is now standing in the top-left cell.
She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p>
<p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p>
<p>Find the number of ways she can travel to the bottom-right cell.</p>
<p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var> 1 ≦ H, W ≦ 100,000</var></li>
<li><var> 1 ≦ A < H</var></li>
<li><var> 1 ≦ B < 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> |
s428777035 | p04046 | Time Limit Exceeded | from math import factorial
def P(n, r):
return factorial(n)//factorial(n-r)
def C(n, r):
return P(n, r)//factorial(r)
ans = 0
H,W,A,B = map(int, input().split())
MOD = int(1e9)+7
for i in range(min(H-A, W-B)):
ans += C(H-A+B-1, B+i)*C(A+W-B-1, A+i)
# print(ans)
ans %= MOD
print(ans) | 2 3 1 1
| 2
| <span class="lang-en">
<p>Score : <var>400</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns.
Iroha is now standing in the top-left cell.
She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p>
<p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p>
<p>Find the number of ways she can travel to the bottom-right cell.</p>
<p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var> 1 ≦ H, W ≦ 100,000</var></li>
<li><var> 1 ≦ A < H</var></li>
<li><var> 1 ≦ B < 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> |
s249800657 | p04046 | Time Limit Exceeded | # -*- coding: utf-8 -*-
import math
H, W, A, B = map(int, input().split())
union_sum = 0
i = 0
for n in range(B, W):
m = H - A - 1 + n
tmp_a = math.factorial(m) // (math.factorial(n) * math.factorial(m - n))
tmp_b = math.factorial(A - B + W - 2 - i) // (math.factorial(A - 1) * math.factorial(W - B - 1 - i))
union_sum += tmp_a * tmp_b
i += 1
print(int(union_sum % (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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s694235800 | p04046 | Time Limit Exceeded | import math
H, W, A, B = map(int, input().split())
def comb(n, r):
return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
def solve():
ans = 0
an = H + B - A - 1
ar = B
bn = W + A - B - 2
br = W - B - 1
for i in range(W-B):
a = comb(an, ar)
b = comb(bn, br)
ans += a * b
an += 1; ar += 1
bn -= 1; br -= 1
return ans%(10**9+7)
print(solve())
| 2 3 1 1
| 2
| <span class="lang-en">
<p>Score : <var>400</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns.
Iroha is now standing in the top-left cell.
She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p>
<p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p>
<p>Find the number of ways she can travel to the bottom-right cell.</p>
<p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var> 1 ≦ H, W ≦ 100,000</var></li>
<li><var> 1 ≦ A < H</var></li>
<li><var> 1 ≦ B < 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> |
s765871833 | p04046 | Time Limit Exceeded | iH,iW,iA,iB = [int(x) for x in input().split()]
iD = 1000000007
def fFr(iX,iR=1):
if iR == 0 or iX == 0:
return 1
else :
iRet = 1
for i in range(iR,iX+1):
iRet *= i
return iRet
def fnCr(iN,iR):
if iR == 0:
return 1
else:
return fFr(iN,iR+1) // fFr(iN-iR)
iRet = 0
for iL in range(0,iH-iA):
iRet += (fnCr(iL+iB-1,iL) * fnCr(iW-iB+iH-iL-2,iW-iB-1))%iD
iRet %= iD
iRet %= iD
print(iRet)
| 2 3 1 1
| 2
| <span class="lang-en">
<p>Score : <var>400</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns.
Iroha is now standing in the top-left cell.
She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p>
<p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p>
<p>Find the number of ways she can travel to the bottom-right cell.</p>
<p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var> 1 ≦ H, W ≦ 100,000</var></li>
<li><var> 1 ≦ A < H</var></li>
<li><var> 1 ≦ B < 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> |
s087266082 | p04046 | Time Limit Exceeded | # -*- coding: utf-8 -*-
H,W,A,B = map(int, input().split())
# 予め組み合わせ計算に必要な階乗のテーブルを作っておく
factorials = [0] * (H+W)
factorials[0] = 1
factorials[1] = 1
for i in range(2,H+W):
factorials[i] = factorials[i-1] * i
# 組み合わせの数だけ返してくれる関数(自作)
def nCr(n, r):
# 10C7 = 10C3
r = min(r, n-r)
# 分子の計算
numerator = factorials[n]
# 分母の計算
denominator = factorials[r] * factorials[n-r]
return numerator // denominator
h = H - A
w = B + 1
ans = 0
# マスを右上に1つずつずらして、必ず通る場所でパターンを足し合わせていく
while h > 0 and w <= W:
ans = (ans + nCr(h+w-2, h-1) * nCr(H-h+W-w, H-h)) % (10 ** 9 + 7)
h -= 1
w += 1
print(ans) | 2 3 1 1
| 2
| <span class="lang-en">
<p>Score : <var>400</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns.
Iroha is now standing in the top-left cell.
She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p>
<p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p>
<p>Find the number of ways she can travel to the bottom-right cell.</p>
<p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var> 1 ≦ H, W ≦ 100,000</var></li>
<li><var> 1 ≦ A < H</var></li>
<li><var> 1 ≦ B < 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> |
s927010735 | p04046 | Time Limit Exceeded | # -*- coding: utf-8 -*-
# 組み合わせの数だけ返してくれる関数
def nCr(n, r):
"""
Calculate the number of combination (nCr = nPr/r!).
The parameters need to meet the condition of n >= r >= 0.
It returns 1 if r == 0, which means there is one pattern
to choice 0 items out of the number of n.
"""
# 10C7 = 10C3
r = min(r, n-r)
# Calculate the numerator.
numerator = 1
for i in range(n, n-r, -1):
numerator *= i
# Calculate the denominator. Should use math.factorial?
denominator = 1
for i in range(r, 1, -1):
denominator *= i
return numerator // denominator
H,W,A,B = map(int, input().split())
h = H - A
w = B + 1
ans = 0
# マスを右上に1つずつずらして、必ず通る場所でパターンを足し合わせていく
while h > 0 and w <= W:
ans += nCr(h+w-2, h-1) * nCr(H-h+W-w, H-h) % (10 ** 9 + 7)
h -= 1
w += 1
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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s888458636 | p04046 | Time Limit Exceeded | # -*- coding: utf-8 -*-
from itertools import combinations as comb
H,W,A,B = map(int, input().split())
h = H - A
w = B + 1
ans = 0
# マスを右上に1つずつずらして、必ず通る場所でパターンを足し合わせていく
while h > 0 and w <= W:
ans += len(list(comb([0] * (h+w-2), h-1))) * len(list(comb([0] * (H-h+W-w), H-h))) % (10 ** 9 + 7)
h -= 1
w += 1
print(ans) | 2 3 1 1
| 2
| <span class="lang-en">
<p>Score : <var>400</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns.
Iroha is now standing in the top-left cell.
She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p>
<p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p>
<p>Find the number of ways she can travel to the bottom-right cell.</p>
<p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var> 1 ≦ H, W ≦ 100,000</var></li>
<li><var> 1 ≦ A < H</var></li>
<li><var> 1 ≦ B < 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> |
s964365035 | p04046 | Time Limit Exceeded | # -*- coding: utf-8 -*-
from itertools import combinations as comb
H,W,A,B = map(int, input().split())
HW = len(list(comb([0] * (H+W-2), H-1)))
h = H - A
w = B + 1
ans = 0
# マスを右上に1つずつずらして、必ず通る場所でパターンを足し合わせていく
while h > 0 and w <= W:
ans += len(list(comb([0] * (h+w-2), h-1))) * len(list(comb([0] * (H-h+W-w), H-h))) % (10 ** 9 + 7)
h -= 1
w += 1
print(ans) | 2 3 1 1
| 2
| <span class="lang-en">
<p>Score : <var>400</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns.
Iroha is now standing in the top-left cell.
She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p>
<p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p>
<p>Find the number of ways she can travel to the bottom-right cell.</p>
<p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var> 1 ≦ H, W ≦ 100,000</var></li>
<li><var> 1 ≦ A < H</var></li>
<li><var> 1 ≦ B < 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> |
s258874669 | p04046 | Time Limit Exceeded | from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,datetime
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inpl(): return list(map(int, input().split()))
def inpl_s(): return list(input().split())
H,W,A,B=inpl()
ans = 0
def Comb(n,k):#nCk
gyakugen = [1]*(n+1)
fac = [1]*(n+1)
for i in range(1,n+1):
fac[i] = (fac[i-1]*i)%mod
gyakugen[n] = pow(fac[n],mod-2,mod)
for i in range(n,0,-1):
gyakugen[i-1] = (gyakugen[i]*i)%mod
com = [1]*(n+1)
for i in range(1,n+1):
com[i] = (fac[n]*gyakugen[i]*gyakugen[n-i])%mod
return com[k]%mod
for x1 in range(B+1,W+1):
y1 = H-A
x2 = A
y2 = W-x1+1
ans += (Comb(x1+y1-2,y1-1)*Comb(x2+y2-2,y2-1))%mod
ans %= mod
print(ans)
| 2 3 1 1
| 2
| <span class="lang-en">
<p>Score : <var>400</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns.
Iroha is now standing in the top-left cell.
She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p>
<p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p>
<p>Find the number of ways she can travel to the bottom-right cell.</p>
<p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var> 1 ≦ H, W ≦ 100,000</var></li>
<li><var> 1 ≦ A < H</var></li>
<li><var> 1 ≦ B < 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> |
s733825296 | p04046 | Time Limit Exceeded | H, W, A, B = map(int, input().split())
m = 10**9 + 7
fc = [1] * (H + W)
for i in range(2, H+W):
fc[i] = fc[i - 1] * i % m
def mod_pow(x, n, mod):
if n == 0: return 1
d, m = n >> 1, n & 1
p = mod_pow(x, d, mod)
return (p * p * (x if m else 1)) % mod
def rev(c, r):
return mod_pow(fc[c] * fc[r] % m, m - 2, m)
import functools
@functools.lru_cache(maxsize=None)
def cr(c, r):
return fc[c + r] * rev(c, r) % m
ans = 0
for c in range(B, W):
ans += cr(c, H - 1 - A) * cr(W - 1 - c, A - 1) % m
print(ans % m) | 2 3 1 1
| 2
| <span class="lang-en">
<p>Score : <var>400</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns.
Iroha is now standing in the top-left cell.
She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p>
<p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p>
<p>Find the number of ways she can travel to the bottom-right cell.</p>
<p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var> 1 ≦ H, W ≦ 100,000</var></li>
<li><var> 1 ≦ A < H</var></li>
<li><var> 1 ≦ B < 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> |
s953924166 | p04046 | Time Limit Exceeded | H, W, A, B = map(int, input().split())
m = 10**9 + 7
fc = [1] * (H + W)
for i in range(2, H+W):
fc[i] = fc[i - 1] * i % m
def mod_pow(x, n, mod):
if n == 0: return 1
d, m = n >> 1, n & 1
p = mod_pow(x, d, mod)
return (p * p * (x if m else 1)) % mod
import functools
@functools.lru_cache(maxsize=None)
def rev(c, r):
return mod_pow(fc[c] * fc[r] % m, m - 2, m)
def cr(c, r):
return fc[c + r] * rev(c, r) % m
ans = 0
for c in range(B, W):
ans = (ans + cr(c, H - 1 - A) * cr(W - 1 - c, A - 1)) % 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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s914249857 | p04046 | Time Limit Exceeded | H, W, A, B = map(int, input().split())
m = 10**9 + 7
fc = [1] * (H + W)
for i in range(2, H+W):
fc[i] = fc[i - 1] * i % m
def mod_pow(x, n, mod):
if n == 0: return 1
d, m = n >> 1, n & 1
p = mod_pow(x, d, mod)
return (p * p * (x if m else 1)) % mod
def rev(n):
return mod_pow(n, m - 2, m)
def cr(c, r):
return fc[c + r] * rev(fc[c] * fc[r] % m) % m
ans = 0
for c in range(B, W):
ans = (ans + cr(c, H - 1 - A) * cr(W - 1 - c, A - 1)) % 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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s393173368 | p04046 | Time Limit Exceeded | H, W, A, B = map(int, input().split())
m = 10**9 + 7
fc = [1] * (H + W)
for i in range(2, H+W):
fc[i] = fc[i - 1] * i % m
import functools
@functools.lru_cache(maxsize=None)
def fact(n):
if n == 0: return 1
return n * fc[n - 1] % m
def mod_pow(x, n, mod):
if n == 0: return 1
d, m = n >> 1, n & 1
p = mod_pow(x, d, mod)
return (p * p * (x if m else 1)) % mod
def rev(n):
return mod_pow(n, m - 2, m)
def cr(c, r):
return fact(c + r) * rev(fact(c) * fact(r) % m) % m
ans = 0
for c in range(B, W):
ans += cr(c, H - 1 - A) * cr(W - 1 - c, A - 1)
print(ans % m) | 2 3 1 1
| 2
| <span class="lang-en">
<p>Score : <var>400</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns.
Iroha is now standing in the top-left cell.
She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p>
<p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p>
<p>Find the number of ways she can travel to the bottom-right cell.</p>
<p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var> 1 ≦ H, W ≦ 100,000</var></li>
<li><var> 1 ≦ A < H</var></li>
<li><var> 1 ≦ B < 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> |
s709974015 | p04046 | Time Limit Exceeded | H, W, A, B = map(int,input().split())
P = 10**9+7
#H, W, A, B = map(int,"100000 100000 44444 55555".split())
factlist = [1] * (H+W)
t = 1
for i in range(H+W-1):
t = (t * (i+1)) % P
factlist[i+1] = t
def fact(i):
return factlist[i]
def egcd(a, b):
if a == 0:
return (b, 0, 1)
else:
g, y, x = egcd(b % a, a)
return (g, x - (b // a) * y, y)
def modinv(a, m):
g, x, y = egcd(a, m)
if g != 1:
raise Exception('modular inverse does not exist')
else:
return x % m
def comb(i,j):
return (fact(i+j) * modinv(fact(i),P) * modinv(fact(j),P)) % P
s = 0
i = 0
while H-A-i-1 >= 0 and B+i <= W and A+i <= H and W-B-i-1 >= 0:
# print((H-A-i,B+i+1))
s = (s + comb(H-A-i-1,B+i) * comb(A+i,W-B-i-1)) % P
i += 1
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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s890874524 | p04046 | Time Limit Exceeded | import math
H, W, A, B = map(int, input().split())
p = 10 ** 9 + 7
ans = 0
def fac(a, b):
a = math.factorial(a + b) // math.factorial(a) // math.factorial(b)
return a % p
for h in range(H - A):
ans += fac(h, B - 1) * fac(H - h - 1, W - B - 1) % p
print(ans % p) | 2 3 1 1
| 2
| <span class="lang-en">
<p>Score : <var>400</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns.
Iroha is now standing in the top-left cell.
She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p>
<p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p>
<p>Find the number of ways she can travel to the bottom-right cell.</p>
<p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var> 1 ≦ H, W ≦ 100,000</var></li>
<li><var> 1 ≦ A < H</var></li>
<li><var> 1 ≦ B < 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> |
s550366751 | p04046 | Time Limit Exceeded | # E.py#
H, W, A, B = map(int,input().split())
P = 10**9+7
# H, W, A, B = map(int,"2 3 1 1".split())
def egcd(a, b):
if a == 0:
return (b, 0, 1)
else:
g, y, x = egcd(b % a, a)
return (g, x - (b // a) * y, y)
def modinv(a, m):
g, x, y = egcd(a, m)
if g != 1:
raise Exception('modular inverse does not exist')
else:
return x % m
def fact(i):
t = 1
while i > 0:
t = (t * i) % P
i -= 1
return t
def comb(i,j):
return (fact(i+j) * modinv(fact(i),P) * modinv(fact(j),P)) % P
s = 0
i = 0
while H-A-i > 0 and B+i+1 <= W and A+i <= H and W-B-i >= 0:
# print((H-A-i,B+i+1))
s = (s + comb(H-A-i-1,B+i+1-1) * comb(A+i,W-B-i-1)) % P
i += 1
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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s305468610 | p04046 | Time Limit Exceeded | MOD = 10**9+7
from math import factorial
h, w, a, b = list(map(int, input().split()))
num_path = 0
def get_path(n, r):
return factorial(n)//(factorial(n-r)*factorial(r))
y, x = h-a, b+1
while(True):
path_0 = get_path((y+x-2), min(x-1, y-1))
path_1 = get_path((h-y+w-x), min(h-y, w-x))
# print(y, x, path_0, path_1)
num_path = (num_path + path_0*path_1)%MOD
y -= 1
x += 1
if y == 0 or x > w:
break
print(num_path)
| 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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s249540129 | p04046 | Time Limit Exceeded | h, w, lower_bound, left_bound = map(int, input().split())
grid = [[0 for i in range(w)] for j in range(h)]
lower_i = h - lower_bound
left_i = left_bound
for i in range(lower_i, len(grid)):
for j in range(0, left_i):
grid[i][j] = 1
def count_path(row, col, grid, memo):
if memo[row][col] != -1:
return memo[row][col]
path_count = 0
if grid[row][col] == 1:
memo[row][col] = 0
return memo[row][col]
if row == h-1 and col == w-1:
return 1
if row+1 < len(grid):
path_count += count_path(row+1, col, grid, memo)
if col+1 < len(grid[0]):
path_count += count_path(row, col+1, grid, memo)
memo[row][col] = path_count
return memo[row][col]
path_count = 0
memo = [[-1 for i in range(w)] for j in range(h)]
path_count += count_path(0, 0, grid, memo)
final_path_count = path_count % (pow(10, 9) + 7)
print(final_path_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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s061149940 | p04046 | Time Limit Exceeded | import itertools
H,W,A,B = map(int, input().split())
ans = 0
for i in range(H-A):
L1 = [j for j in range(B+i-1)]
L2 = [k for k in range(H+W-B-i-2)]
C1 = list(itertools.combinations(L1,i))
C2 = list(itertools.combinations(L2,W-B-1))
ans += (len(C1) * len(C2)) %(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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s446506129 | p04046 | Time Limit Exceeded | LIM = 10**9 + 7
def main():
h, w, a, b = map(int, input().split())
print(solve(h, w, a, b))
def solve(h, w, a, b):
ans = 0
for i in range(h-a):
first = n_noblock_fast(i + 1, b) % LIM
second = n_noblock_fast(w - b, h - i) % LIM
ans = (ans + (first * second) % LIM) % LIM
return ans
def n_noblock_fast(h, w):
return choose(h + w - 2, w - 1)
def n_noblock(h, w):
n_paths = [[0 for _ in range(w)] for _ in range(h)]
# Accessed with n_paths[ih, iw],
# where ih is height, and iw is horizontal position.
for ih in range(h):
n_paths[ih][0] = 1
for iw in range(w):
n_paths[0][iw] = 1
for ih in range(1, h):
for iw in range(1, w):
n_paths[ih][iw] += n_paths[ih-1][iw]
n_paths[ih][iw] += n_paths[ih][iw-1]
#return n_paths[h-1][w-1]
return n_paths
def choose(n, k):
return factorial(n) // factorial(k) // factorial(n-k)
def factorial(n):
ans = 1
for i in range(n):
ans *= (i + 1)
return 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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s919843366 | p04046 | Time Limit Exceeded | h,w,a,b=list(map(int,input().split()))
road=[[0 for i in range(w)] for j in range(h)]
road[0][0]=1
for i in range(h):
for j in range(w):
if i>h-a-1 and j<b:
continue
if i-1>=0:
road[i][j]+=road[i-1][j]
if j-1>=0:
if i>h-a-1 and j==b:
continue
road[i][j]+=road[i][j-1]
print(road[h-1][w-1]%1000000007) | 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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s065994351 | p04046 | Time Limit Exceeded | MOD = 10**9+7
l = list(map(int,input().split()))
h,w,a,b = l[0],l[1],l[2],l[3]
def pow(x,n):
res = 1
while n > 0:
if n % 2 == 1:
res = res*x % MOD
x = x*x % MOD
n = n >> 1
return res
def comb(n,k):
return (fact[n]*inv[k] % MOD)*inv[n-k] % MOD
fact = [1]
inv = [1]
for i in range(h+w):
fact.append(fact[i]*(i+1) % MOD)
inv.append(pow(fact[i+1],MOD-2) % MOD)
ans = 0
for i in range(w-b):
ans = (ans + (comb(h-a+b+i-1,b+i)*comb(w-b-i+a-2,a-1) % 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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s429004966 | p04046 | Time Limit Exceeded | import math
M = 10**9+7
def nCr(n, r):
return math.factorial(n) // (math.factorial(n-r) * math.factorial(r))
h, w, a, b = map(int, input().split())
ans = 0
for i in range(b,w):
ans += nCr(h-a-1+i,h-a-1) * nCr(a-1+w-1-i, a-1)
print(ans % M)
| 2 3 1 1
| 2
| <span class="lang-en">
<p>Score : <var>400</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns.
Iroha is now standing in the top-left cell.
She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p>
<p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p>
<p>Find the number of ways she can travel to the bottom-right cell.</p>
<p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var> 1 ≦ H, W ≦ 100,000</var></li>
<li><var> 1 ≦ A < H</var></li>
<li><var> 1 ≦ B < 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> |
s858913931 | p04046 | Time Limit Exceeded | import scipy.misc as scm
h, w, a, b = [int(i) for i in input().split()]
p = 10 ** 9 + 7
s = 0
for i in range(w-b):
s += scm.comb(h+w-a-2-i, w-1-i, 1) * scm.comb(a-1+i, a-1, 1)
print(s%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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s905471452 | p04046 | Time Limit Exceeded | MOD = 10**9+7
def add(a,b):
return (a+b) % MOD
def sub(a,b):
return (a-b) % MOD
def mul(a,b):
return ((a%MOD) * (b%MOD)) % MOD
def pow(a,n):
ans = 1
mag = a
for b in reversed(str(bin(n))):
if b == 'b': break
if b == '1':
ans = mul(ans, mag)
mag = mul(mag, mag)
return ans
def inv(a):
return pow(a, MOD-2)
H,W,A,B = map(int,raw_input().split())
factorical = [1]
factorical_inv = [1]
for n in range (1,H+W+1):
f = mul(factorical[-1], n)
factorical.append(f)
factorical_inv.append(inv(f))
def ncr(n,r):
return mul(mul(factorical[n], factorical_inv[n-r]), factorical_inv[r])
def paths(w,h):
return ncr(w+h,w)
ans = 0
for i in range(W-B):
p = mul(paths(H-A-1, B+i), paths(A-1, W-B-1-i))
ans = add(ans, p)
print ans
| 2 3 1 1
| 2
| <span class="lang-en">
<p>Score : <var>400</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns.
Iroha is now standing in the top-left cell.
She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p>
<p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p>
<p>Find the number of ways she can travel to the bottom-right cell.</p>
<p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var> 1 ≦ H, W ≦ 100,000</var></li>
<li><var> 1 ≦ A < H</var></li>
<li><var> 1 ≦ B < 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> |
s228904891 | p04046 | Time Limit Exceeded | MOD = 10**9+7
def add(a,b):
return (a+b) % MOD
def sub(a,b):
return (a-b) % MOD
def mul(a,b):
return ((a%MOD) * (b%MOD)) % MOD
def pow(a,n):
ans = 1
mag = a
for b in reversed(str(bin(n))):
if b == 'b': break
if b == '1':
ans = mul(ans, mag)
mag = mul(mag, mag)
return ans
def inv(a):
return pow(a, MOD-2)
factorical = [1]
factorical_inv = [1]
for n in range (1,200002):
f = mul(factorical[-1], n)
factorical.append(f)
factorical_inv.append(inv(f))
def ncr(n,r):
return mul(mul(factorical[n], factorical_inv[n-r]), factorical_inv[r])
H,W,A,B = map(int,raw_input().split())
def paths(w,h):
return ncr(w+h,w)
ans = 0
for i in range(W-B):
p = mul(paths(H-A-1, B+i), paths(A-1, W-B-1-i))
ans = add(ans, p)
print ans | 2 3 1 1
| 2
| <span class="lang-en">
<p>Score : <var>400</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns.
Iroha is now standing in the top-left cell.
She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p>
<p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p>
<p>Find the number of ways she can travel to the bottom-right cell.</p>
<p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var> 1 ≦ H, W ≦ 100,000</var></li>
<li><var> 1 ≦ A < H</var></li>
<li><var> 1 ≦ B < 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> |
s580804528 | p04046 | Time Limit Exceeded | import math
H,W,A,B=map(int, input().split())
def comb(x,y):
return(math.factorial(x)//(math.factorial(x-y)*math.factorial(y)))
alls=comb(H+W-2,W-1)
for i in range(B):
alls-=comb(H-A-1+i,H-A-1)*comb(W+A-2-i,A-1)
print(alls%(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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s834658060 | p04046 | Time Limit Exceeded | H, W, A, B = [int(x) for x in input().split()]
matrix = [[0 for x in range(W)] for y in range(H)]
matrix[0][0] = 1
for y in range(0, H):
for x in range(0, W):
# if not (y >= H-A and x < B):
if y < H-A or x >= B:
if y != 0:
matrix[y][x] += matrix[y-1][x]
if x != 0:
matrix[y][x] += matrix[y][x-1]
ans = divmod(matrix[H-1][W-1], pow(10,9) + 7)
print(ans[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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s230205803 | p04046 | Time Limit Exceeded | import math
class Paths:
def returnPaths(self,h,w,a,b):
ans=0
dic={}
for i in range(h+w): dic[i]=math.factorial(i)
for i in range(b,w):
checkpoint=dic[h-a-1+i]/(dic[h-a-1]*dic[i])
ans+=checkpoint*(dic[a-2+w-i]/(dic[a-1]*dic[w-1-i]))
ans%=10**9+7
print ans
if __name__ == "__main__":
h,w,a,b=[int(i) for i in raw_input().split()]
p=Paths()
p.returnPaths(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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s245179070 | p04046 | Time Limit Exceeded | def POW(a,b,MOD):
p = 1
while b > 0:
if b&1:
p = p*a%MOD
b >>= 1
a = (a*a)%MOD
return p
MOD = int(1e9+7)
H,W,A,B = map(int,input().split())
fact = [1] * (H+W+1)
inv = [1] * (H+W+1)
for i in range(H+W):
fact[i+1] = (i+1)*fact[i]%MOD
inv[i+1] = POW(fact[i+1],MOD-2,MOD)
ans = 0
for i in range(B,W):
temp = fact[H-A-1+i]
temp = (temp*inv[H-A-1])%MOD
temp = (temp*inv[i])%MOD
temp = (temp*fact[A-1 + W-i-1])%MOD
temp = (temp*inv[A-1] )%MOD
temp = (temp*inv[W-i-1])%MOD
ans = (ans+temp)%MOD
print(ans)
| 2 3 1 1
| 2
| <span class="lang-en">
<p>Score : <var>400</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns.
Iroha is now standing in the top-left cell.
She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p>
<p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p>
<p>Find the number of ways she can travel to the bottom-right cell.</p>
<p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var> 1 ≦ H, W ≦ 100,000</var></li>
<li><var> 1 ≦ A < H</var></li>
<li><var> 1 ≦ B < 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> |
s286630773 | p04046 | Time Limit Exceeded | import re
class IO_for_Contest(object):
@staticmethod
def my_input():
# return raw_input()
return input()
@staticmethod
def read_from_input():
pass
@staticmethod
def read_line():
return IO_for_Contest.my_input().strip()
@staticmethod
def read_int():
return int(IO_for_Contest.my_input().strip())
@staticmethod
def read_n_int(n):
return list(map(
int,
re.split('\s+', IO_for_Contest.my_input().strip())))[: n]
def solve():
h, w, a, b = IO_for_Contest.read_n_int(4)
board = prepare_board(h, w)
ways = count_ways(board, a, b)
print(ways)
def prepare_board(h, w):
board = []
for y in range(h):
board.append([0] * w)
return board
def count_ways(board, a, b):
mode = 10 ** 9 + 7
board[0][0] = 1
h = len(board)
w = len(board[0])
for y in range(h):
for x in range(w):
if x == 0 and y == 0:
continue
if is_prohibited(w, h, b, a, x, y):
board[y][x] = 0
continue
ways_on_left_cell = get_ways_on_cell(board, x - 1, y)
ways_on_upper_cell = get_ways_on_cell(board, x, y - 1)
board[y][x] = (ways_on_left_cell + ways_on_upper_cell) % mode
# print('{0:d}, {1:d} = {2:d}'.format(x, y, board[y][x]))
# print(board)
return board[h - 1][w - 1]
def get_ways_on_cell(board, x, y):
if x < 0:
return 0
if y < 0:
return 0
return board[y][x]
def is_prohibited(w, h, b, a, x, y):
return (x < b) and (y >= h - a)
if __name__ == '__main__':
# import doctest
# doctest.testmod()
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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s611359489 | p04046 | Time Limit Exceeded | H, W, A, B = map(int, input().split())
#masu = [[0 for x in range(10)] for y in range(10)]
masu = [[0 for j in range(W)] for i in range(H)]
for j in range(W):
masu[0][j] = 1
for i in range(H-A):
masu[i][0] = 1
for i in range(1, H-A):
for j in range(1, W):
masu[i][j] = (masu[i-1][j] + masu[i][j-1]) % 1000000007
for i in range(H-A, H):
for j in range(B, W):
masu[i][j] = (masu[i-1][j] + masu[i][j-1]) % 1000000007
print(masu[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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s481814819 | p04046 | Time Limit Exceeded | M=10**9+7;F=[pow(X,M-2,M)for X in range(8**6)];H,W,A,B=map(int,input().split());Z=C=1
for I in range(H-1):Z=C=C*(W+H-B-2-I)*F[I+1]%M
for I in range(1,H-A):C=C*(B-1+I)*F[I]*(H-I)*F[W+H-B-1-I]%M;Z=(Z+C)%M
print(Z) | 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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s367472849 | p04046 | Time Limit Exceeded | import functools;M=10**9+7;F=functools.lru_cache(2**16)(lambda X:pow(X,M-2,M));H,W,A,B=map(int,input().split());Z=C=1
for I in range(H-1):Z=C=C*(W+H-B-2-I)*F(I+1)%M
for I in range(1,H-A):C=C*(B-1+I)*F(I)*(H-I)*F(W+H-B-1-I)%M;Z=(Z+C)%M
print(Z) | 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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s047534201 | p04046 | Time Limit Exceeded | F=lambda X:pow(X,M-2,M);M=10**9+7;H,W,A,B=map(int,input().split());Z=C=1
for I in range(min(W-1-B,H-1)):Z=C=C*(W+H-B-2-I)*F(I+1)%M
for I in range(1,H-A):C=C*(B-1+I)*F(I)%M*(H-I)*F(W+H-B-1-I)%M;Z=(Z+C)%M
print(Z) | 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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s633315228 | p04046 | Time Limit Exceeded | F=lambda X:pow(X,M-2,M);M=10**9+7;H,W,A,B=map(int,input().split());Z=C=1
for I in range(min(W-1-B,H-1)):Z=C=C*(W+H-B-2-I)*F(I+1)%M
for I in range(1,H-A):C=C*(B-1+I)*(H-I)*F(I)*F(W+H-B-1-I)%M;Z=(Z+C)%M
print(Z) | 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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s184908856 | p04046 | Time Limit Exceeded | F=lambda X:pow(X,M-2,M);M=10**9+7;H,W,A,B=map(int,input().split());Z=C=1
for I in range(W-1-B):Z=C=C*(W+H-B-2-I)*F(I+1)%M
for I in range(1,H-A):C=C*(B-1+I)*(H-I)*F(I)*F(W+H-B-1-I)%M;Z=(Z+C)%M
print(Z) | 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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s514160218 | p04046 | Time Limit Exceeded | def G(N,R):
X=1
for I in range(min(R,N-R)):X=X*(N-I)*F(I+1)%M
return X
F=lambda X:pow(X,M-2,M);M=10**9+7;H,W,A,B=map(int,input().split());Z=G(H+W-2,H-1)
for I in range(B):C=(C*(H-A+I-1)*F(I)*(W-I)*F(A+W-I-1)if I else G(H-A+I-1,H-A-1)*G(A+W-I-2,A-1))%M;Z=(Z-C)%M
print(Z%M) | 2 3 1 1
| 2
| <span class="lang-en">
<p>Score : <var>400</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns.
Iroha is now standing in the top-left cell.
She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p>
<p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p>
<p>Find the number of ways she can travel to the bottom-right cell.</p>
<p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var> 1 ≦ H, W ≦ 100,000</var></li>
<li><var> 1 ≦ A < H</var></li>
<li><var> 1 ≦ B < 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> |
s157085939 | p04046 | Time Limit Exceeded | def G(N,R):
X=1
for I in range(R):X=X*(N-I)*F(I+1)%M
return X
F=lambda X:pow(X,M-2,M);M=10**9+7;H,W,A,B=map(int,input().split());Z=G(H+W-2,H-1)
for I in range(B):C=(C*(H-A+I-1)*F(I)*(W-I)*F(A+W-I-1)if I else G(H-A+I-1,H-A-1)*G(A+W-I-2,A-1))%M;Z-=C
print(Z%M) | 2 3 1 1
| 2
| <span class="lang-en">
<p>Score : <var>400</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns.
Iroha is now standing in the top-left cell.
She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p>
<p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p>
<p>Find the number of ways she can travel to the bottom-right cell.</p>
<p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var> 1 ≦ H, W ≦ 100,000</var></li>
<li><var> 1 ≦ A < H</var></li>
<li><var> 1 ≦ B < 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> |
s760048716 | p04046 | Time Limit Exceeded | C = int(1e9) + 7
h, w, a, b = [int(x) for x in input().split()]
# 1000000005 = 2^29 + 2^28 + 2^27 + 2^25 + 2^24 + 2^23 + 2^20 + 2^19 + 2^17 + 2^15 + 2^14 + 2^11 + 2^9 + 2^2 + 1
def pow(n):
ans = n
for i in range(1, 30):
n = (n * n) % C
if i in {29, 28, 27, 25, 24, 23, 20, 19, 17, 15, 14, 11, 9, 2}:
ans = (ans * n) % C
return ans
fact = [1]
factinv = [1] # dummy
for x in range(1, h + w - 2):
fact.append((x * fact[x - 1]) % C)
# factinv.append(pow(x)) # <- this won't work, why?
factinv.append((pow(x) * factinv[x - 1]) % C)
def nCr(n, r):
return (fact[n] * factinv[r] * factinv[n - r]) % C
ans = 0
for i in range(b, w):
ans = (ans + nCr(i + h - a - 1, h - a - 1) * nCr(a - 1 + w - i - 1, a - 1)) % C
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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s213415181 | p04046 | Time Limit Exceeded | H,W,A,B = [int(n) for n in input().split(" ")]
answers = []
for total in range(B,W):
disp = 1
for n in range(1,total+(H-A)):
disp *= n
for m in range(1,H-A ):
disp //= m
for t in range(1,total + 1):
disp //= t
answers.append(disp)
for cnt,total in enumerate(range(B,W)):
disp = 1
for n in range(1,(W - total - 1) + (H - (H - A))):
disp *= n
for m in range(1,H- (H - A)):
disp //= m
for t in range(1,W-total):
disp //= t
answers[cnt] *= disp
print(sum(answers) %( 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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s208151375 | p04046 | Time Limit Exceeded | def read():
return map(int, input().split())
h, w, a, b = read()
m = 10 ** 9 + 7
line1 = [1 for _ in range(w)]
line2 = [0 for _ in range(w)]
for _ in range(1, h - a):
line2[0] = 1
for j in range(1, w):
line2[j] = (line2[j - 1] + line1[j]) % m
line1 = line2
line2 = [0 for _ in range(w)]
line1 = line1[b:]
line2 = [0 for _ in range(w - b)]
for _ in range(a):
line2[0] = line1[0]
for j in range(1, w - b):
line2[j] = (line2[j - 1] + line1[j]) % m
line1 = line2
line2 = [0 for _ in range(w - b)]
print(line1[-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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s636275448 | p04046 | Memory Limit Exceeded | import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque
sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7
DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
fac = [-1] * (10**7+1)
inv = [-1] * (10**7+1)
finv = [-1] * (10**7+1)
fac[0] = fac[1] = 1
inv[1] = 1
finv[0] = finv[1] = 1
def initNCMMod(limit):
for i in range(2, limit):
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 NCMMod(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
initNCMMod(10**6 + 1)
def main():
H, W, A, B = LI()
cnt = 0
for i in range(H-A):
way = NCMMod(B + i - 1, i) * NCMMod(W + H - B - i - 2, W - 1 - B)
way %= mod
cnt += way
cnt %= mod
print(cnt)
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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s100684067 | p04046 | Accepted | MODD = 10**9 + 7
def modinv(a,m)-> int:
b = m
u = 1
v = 0
while (b):
t = a // b
a -= t * b
a,b= b,a
u -= t * v
u,v= v,u
u %= m
if (u < 0):
u += m
return u
fac=[1]*200001
infac=[1]*200001
aaa=1
for i in range(1,200001):
aaa=(aaa*i)%MODD
fac[i]=aaa
infac[i]=modinv(aaa,MODD)
#print(fac[i])
import math as m
def perm(x,y,z) -> int:
#print(fac[x]//(fac[y]*fac[z]))
return ((fac[x]*infac[y]*infac[z])%MODD)
h,w,a,b= [int(x) for x in input().split()]
A = h-a
B = b-1
C = h-1
D = w-b-1
j=0
k=B
kl = C
jl = D
ans = 0
for i in range(A):
ans+=perm(B+i,B,i)*perm(C+D-i,C-i,D)
print(ans%MODD) | 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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s189716163 | 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 #出力の制限
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=cmb(h+w-2,h-1,mod)
for i in range(h-a+1,h+1):
ans-=cmb(i+b-2,i-1,mod)*cmb(h-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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s765532624 | p04046 | Accepted | # coding:UTF-8
import sys
from math import factorial
MOD = 10 ** 9 + 7
def combInit(n):
fact = [1]
finv = [1]
for i in range(1, n + 1):
fact.append(fact[i - 1] * i % MOD)
finv.append(pow(fact[i], MOD - 2, MOD))
return [fact, finv]
def comb(n, k, f):
if n < k:
return 0
elif n < 0 or k < 0:
return 0
else:
return f[0][n] * (f[1][k] * f[1][n - k] % MOD) % MOD
def perm(n, r):
return factorial(n) // factorial(r)
def gcb(a, b):
if b == 0:
return a
else:
return gcb(b, a % b)
def lcm(a, b):
d = gcb(a, b)
return int(a / d * b)
def surP(x):
return x % MOD
def main():
# ------ 入力 ------#
h, w, a, b = list(map(int, input().split())) # スペース区切り連続数字
# ------ 処理 ------#
f = combInit(h + w)
res = 0
for i in range(b, w):
res += comb((h-1-a)+i, i, f) * comb((a-1)+(w-1-i), w-1-i, f)
out = surP(res)
# ------ 出力 ------#
print("{}".format(out))
# if flg == 0:
# print("YES")
# else:
# print("NO")
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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s010651559 | p04046 | Accepted | # -*- coding: utf-8 -*-
#############
# Libraries #
#############
import sys
input = sys.stdin.readline
import math
#from math import gcd
import bisect
import heapq
from collections import defaultdict
from collections import deque
from collections import Counter
from functools import lru_cache
#############
# Constants #
#############
MOD = 10**9+7
INF = float('inf')
AZ = "abcdefghijklmnopqrstuvwxyz"
#############
# Functions #
#############
######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]
#####Shorten#####
def DD(arg): return defaultdict(arg)
#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)
######Combination######
kaijo_memo = []
def kaijo(n):
if(len(kaijo_memo) > n): return kaijo_memo[n]
if(len(kaijo_memo) == 0): kaijo_memo.append(1)
while(len(kaijo_memo) <= n): kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
return kaijo_memo[n]
gyaku_kaijo_memo = []
def gyaku_kaijo(n):
if(len(gyaku_kaijo_memo) > n): return gyaku_kaijo_memo[n]
if(len(gyaku_kaijo_memo) == 0): gyaku_kaijo_memo.append(1)
while(len(gyaku_kaijo_memo) <= n): gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
return gyaku_kaijo_memo[n]
def nCr(n,r):
if n == r: return 1
if n < r or r < 0: return 0
ret = 1
ret = ret * kaijo(n) % MOD
ret = ret * gyaku_kaijo(r) % MOD
ret = ret * gyaku_kaijo(n-r) % MOD
return ret
######Factorization######
def factorization(n):
arr = []
temp = n
for i in range(2, int(-(-n**0.5//1))+1):
if temp%i==0:
cnt=0
while temp%i==0:
cnt+=1
temp //= i
arr.append([i, cnt])
if temp!=1: arr.append([temp, 1])
if arr==[]: arr.append([n, 1])
return arr
#####MakeDivisors######
def make_divisors(n):
divisors = []
for i in range(1, int(n**0.5)+1):
if n % i == 0:
divisors.append(i)
if i != n // i:
divisors.append(n//i)
return divisors
#####MakePrimes######
def make_primes(N):
max = int(math.sqrt(N))
seachList = [i for i in range(2,N+1)]
primeNum = []
while seachList[0] <= max:
primeNum.append(seachList[0])
tmp = seachList[0]
seachList = [i for i in seachList if i % tmp != 0]
primeNum.extend(seachList)
return primeNum
#####GCD#####
def gcd(a, b):
while b: a, b = b, a % b
return a
#####LCM#####
def lcm(a, b):
return a * b // gcd (a, b)
#####BitCount#####
def count_bit(n):
count = 0
while n:
n &= n-1
count += 1
return count
#####ChangeBase#####
def base_10_to_n(X, n):
if X//n: return base_10_to_n(X//n, n)+[X%n]
return [X%n]
def base_n_to_10(X, n):
return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))
def base_10_to_n_without_0(X, n):
X -= 1
if X//n: return base_10_to_n_without_0(X//n, n)+[X%n]
return [X%n]
#####IntLog#####
def int_log(n, a):
count = 0
while n>=a:
n //= a
count += 1
return count
#############
# Main Code #
#############
H,W,A,B = IL()
ans = nCr(H+W-2,H-1)
for i in range(B):
ans -= nCr(H-A+i-1,i)*nCr(W+A-i-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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s965975403 | p04046 | Accepted | P = 10**9+7
fac = [1]
ifac = [1]
ff = 1
for i in range(1,200001):
ff *= i
ff %= P
fac.append(ff)
ifac.append(pow(ff, P-2, P))
def ncr(n, r):
return (fac[n] * ifac[r] % P * ifac[n-r] % P);
h,w,a,b = map(int,input().split())
s = 0
nC = b-1
kC = 0
nD = w-b-1+h-1
kD = h-1
for i in range(h-a):
C = ncr(nC, kC)
D = ncr(nD, kD)
s = (s + C * D) % P
nC += 1
kC += 1
kD -= 1
nD -= 1
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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s169528854 | p04046 | Accepted | from math import comb
h,w,a,b = map(int,input().split())
s = 0
nC = b-1
kC = 0
nD = w-b-1+h-1
kD = h-1
p = 1000000007
fac = [1]
ff = 1
for i in range(1,200001):
ff *= i
ff %= p
fac.append(ff)
def ncr(n, r, p):
return (fac[n] * pow(fac[r], p-2, p) % p * pow(fac[n-r], p-2, p) % p);
for i in range(h-a):
C = ncr(nC, kC, 1000000007)
D = ncr(nD, kD, 1000000007)
s = (s + C * D) % 1000000007
nC += 1
kC += 1
kD -= 1
nD -= 1
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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s625645633 | p04046 | Accepted | import sys
def input(): return sys.stdin.readline().strip()
mod = 10**9+7
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(10**6)
def main():
H, W, A, B = map(int, input().split())
ans = 0
for i in range(1, H - A + 1):
ans += comb(i + B - 2, i - 1) * comb(H - i + W - B - 1, H - i)
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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s837263719 | p04046 | Accepted | from sys import stdin
input = stdin.readline
MAX = 510000
MOD = 10**9 + 7
def MakeTable():
fac[0], fac[1] = 1, 1
finv[0], finv[1] = 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 COM(n, k):
if n < k:
return 0
elif n < 0 or k < 0:
return 0
else:
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD
H, W, A, B = map(int, input().split())
fac = [0] * MAX
finv = [0] * MAX
inv = [0] * MAX
N = min(H-A, W-B)
x, y = 0, 0
result = 0
MakeTable()
for _ in range(N):
n1 = H-A-1+B+x-y
n2 = A+W-1-B-x+y
result += (COM(n1, B+x) * COM(n2, A+y)) % MOD
x += 1
y += 1
print(result%MOD) | 2 3 1 1
| 2
| <span class="lang-en">
<p>Score : <var>400</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns.
Iroha is now standing in the top-left cell.
She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p>
<p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p>
<p>Find the number of ways she can travel to the bottom-right cell.</p>
<p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var> 1 ≦ H, W ≦ 100,000</var></li>
<li><var> 1 ≦ A < H</var></li>
<li><var> 1 ≦ B < 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> |
s232388862 | p04046 | Accepted | #縦H A
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**5)*2
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=0
for i in range(H-A):
ans+=cmb(B+i-1,B-1,mod) * cmb(H-i+W-B-2,W-B-1,mod)
print(ans%mod)
# 1 1 1
# $ 1 2 | 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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s231453289 | p04046 | Accepted | def permutation(n, k, mod):
s = 1
for _ in range(k):
s *= n
s %= mod
n -= 1
return s
def factorial(n, mod):
s = 1
for i in range(1, n + 1):
s *= i
s %= mod
return s
h, w, a, b = map(int, input().split())
mod = pow(10, 9) + 7
x = h - a + b - 1
y = w - b + a - 2
p = permutation(x, h - a - 1, mod) * permutation(y, a - 1, mod) % mod
ans = p
for _ in range(w - b - 1):
x += 1
p = p * x * (y - (a - 1)) * pow((x - (h - a - 1)) * y, mod - 2, mod) % mod
y -= 1
ans += p
ans %= mod
ans = ans * pow(factorial(a - 1, mod), mod - 2, mod) * pow(factorial(h - a - 1, mod), mod - 2, 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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s811976266 | p04046 | Accepted | H, W, A, B = map(int, open(0).read().split())
MOD = 10**9+7
factorials = [1] * (H + W + 1)
inv_factorials = [1] * (H + W + 1)
for i in range(H + W):
factorials[i+1] = factorials[i] * (i + 1) % MOD
for i in range(H + W):
inv_factorials[i+1] = inv_factorials[i] * pow(i + 1, MOD - 2, MOD) % MOD
def modcomb(m, n, mod):
return factorials[m] * inv_factorials[n] % MOD * inv_factorials[m - n] % MOD
# total = modcomb(H + W - 2, W - 1, MOD)
total = 0
# for i in range(B):
for i in range(B, W):
total += modcomb(H - A - 1 + i, i, MOD) * modcomb(A - 1 + W - 1 - i, W - 1 - i, MOD) % MOD
total %= MOD
print(total) | 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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s539781020 | p04046 | Accepted | H, W, A, B = map(int, open(0).read().split())
MOD = 10**9+7
factorials = [1] * (H + W + 1)
inv_factorials = [1] * (H + W + 1)
tmp = 1
for i in range(H + W):
tmp = tmp * (i + 1) % MOD
factorials[i+1] = tmp
inv_factorials = list(map(lambda n: pow(n, MOD - 2, MOD), factorials))
def modcomb(m, n, mod):
return factorials[m] * inv_factorials[n] % MOD * inv_factorials[m - n] % MOD
# total = modcomb(H + W - 2, W - 1, MOD)
total = 0
# for i in range(B):
for i in range(B, W):
total += modcomb(H - A - 1 + i, i, MOD) * modcomb(A - 1 + W - 1 - i, W - 1 - i, MOD) % MOD
total %= MOD
print(total) | 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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s527216173 | p04046 | Accepted | H, W, A, B = map(int, open(0).read().split())
MOD = 10**9+7
factorials = [1] * (H + W + 1)
inv_factorials = [1] * (H + W + 1)
for i in range(H + W):
factorials[i+1] = factorials[i] * (i + 1) % MOD
inv_factorials = list(map(lambda n: pow(n, MOD - 2, MOD), factorials))
def modcomb(m, n, mod):
return factorials[m] * inv_factorials[n] % MOD * inv_factorials[m - n] % MOD
# total = modcomb(H + W - 2, W - 1, MOD)
total = 0
# for i in range(B):
for i in range(B, W):
total += modcomb(H - A - 1 + i, i, MOD) * modcomb(A - 1 + W - 1 - i, W - 1 - i, MOD) % MOD
total %= MOD
print(total) | 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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s240389758 | p04046 | Accepted | H, W, A, B = map(int, open(0).read().split())
MOD = 10**9+7
factorials = [1] * (H + W + 1)
inv_factorials = [1] * (H + W + 1)
for i in range(H + W):
factorials[i+1] = factorials[i] * (i + 1) % MOD
inv_factorials = list(map(lambda n: pow(n, -1, MOD), factorials))
def modcomb(m, n, mod):
return factorials[m] * inv_factorials[n] % MOD * inv_factorials[m - n] % MOD
total = modcomb(H + W - 2, W - 1, MOD)
for i in range(B):
total -= modcomb(H - A - 1 + i, i, MOD) * modcomb(A - 1 + W - 1 - i, W - 1 - i, MOD) % MOD
total %= MOD
print(total) | 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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s313912718 | p04046 | Accepted | H, W, A, B = map(int, open(0).read().split())
MOD = 10**9+7
factorials = [1] * (H + W + 1)
inv_factorials = [1] * (H + W + 1)
for i in range(H + W):
factorials[i+1] = factorials[i] * (i + 1) % MOD
inv_factorials = list(map(lambda n: pow(n, MOD - 2, MOD), factorials))
def modcomb(m, n, mod):
return factorials[m] * inv_factorials[n] % MOD * inv_factorials[m - n] % MOD
total = modcomb(H + W - 2, W - 1, MOD)
for i in range(B):
total -= modcomb(H - A - 1 + i, i, MOD) * modcomb(A - 1 + W - 1 - i, W - 1 - i, MOD) % MOD
total %= MOD
print(total) | 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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s742225886 | p04046 | Accepted | H, W, A, B = map(int, open(0).read().split())
MOD = 10**9+7
factorials = [1] * (H + W + 1)
inv_factorials = [1] * (H + W + 1)
for i in range(H + W):
factorials[i+1] = factorials[i] * (i + 1) % MOD
inv_factorials = list(map(lambda n: pow(n, MOD - 2, MOD), factorials))
def modcomb(m, n, mod):
return factorials[m] * inv_factorials[n] % MOD * inv_factorials[m - n] % MOD
total = modcomb(H + W - 2, W - 1, MOD)
for i in range(B):
total -= modcomb(H - A - 1 + i, i, MOD) * modcomb(A - 1 + W - 1 - i, W - 1 - i, MOD)
total %= MOD
print(total) | 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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s152419936 | p04046 | Accepted | H, W, A, B = map(int, open(0).read().split())
MOD = 10**9+7
factorials = [1] * (H + W + 1)
inv_factorials = [1] * (H + W + 1)
for i in range(H + W):
factorials[i+1] = factorials[i] * (i + 1) % MOD
inv_factorials = list(map(lambda n: pow(n, MOD - 2, MOD), factorials))
def modcomb(m, n, mod):
return factorials[m] * inv_factorials[n] * inv_factorials[m - n] % MOD
total = modcomb(H + W - 2, W - 1, MOD)
for i in range(B):
total -= modcomb(H - A - 1 + i, i, MOD) * modcomb(A - 1 + W - 1 - i, W - 1 - i, MOD)
total %= MOD
print(total) | 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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s463944960 | p04046 | Accepted | H, W, A, B = map(int, open(0).read().split())
MOD = 10**9+7
def modperm(m, n, mod):
p = 1
for i in range(n):
p = p * (m - i) % mod
return p
def modcomb(m, n, mod):
if n > m - n:
n = m - n
p = modperm(m, n, mod)
q = pow(modperm(n, n, mod), mod - 2, mod)
return p * q % mod
total = modcomb(H + W - 2, W - 1, MOD)
tmp = modcomb(A + W - 2, W - 1, MOD)
total -= tmp
for i in range(B - 1):
a = H - A + i
b = i + 1
c = W - i - 1
d = W + A - 2 - i
# print(a,b,c,d)
tmp = tmp * a * c % MOD
tmp = tmp * pow(b, MOD - 2, MOD) % MOD
tmp = tmp * pow(d, MOD - 2, MOD) % MOD
# print(tmp)
total = (total - tmp) % MOD
print(total) | 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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s979987850 | p04046 | Accepted | H, W, A, B = map(int, open(0).read().split())
MOD = 10**9+7
def modcomb(m, n, mod):
if n > m - n:
n = m - n
p = 1
q = 1
for i in range(n):
p = p * (m - i) % mod
q = q * (i + 1) % mod
result = p * pow(q, mod - 2, mod) % mod
return result
total = modcomb(H + W - 2, W - 1, MOD)
tmp = modcomb(A + W - 2, W - 1, MOD)
total -= tmp
for i in range(B - 1):
a = H - A + i
b = i + 1
c = W - i - 1
d = W + A - 2 - i
# print(a,b,c,d)
tmp = tmp * a * c % MOD
tmp = tmp * pow(b, -1, MOD) % MOD
tmp = tmp * pow(d, -1, MOD) % MOD
# print(tmp)
total = (total - tmp) % MOD
print(total) | 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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s307340028 | p04046 | Accepted | M=10**9+7
H,W,A,B = map(int,input().split())
C = 1
ans = 1
for I in range(H-1):
ans = C = C*(W+H-B-2-I)*pow(I+1,M-2,M)%M
for I in range(1,H-A):
C = C*(B-1+I)*(H-I)*pow(I*(W+H-B-1-I),M-2,M)%M
ans+=C
print(ans%M) | 2 3 1 1
| 2
| <span class="lang-en">
<p>Score : <var>400</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns.
Iroha is now standing in the top-left cell.
She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p>
<p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p>
<p>Find the number of ways she can travel to the bottom-right cell.</p>
<p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var> 1 ≦ H, W ≦ 100,000</var></li>
<li><var> 1 ≦ A < H</var></li>
<li><var> 1 ≦ B < 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> |
s906003068 | p04046 | Accepted | class Factorials:
# nに十分大きい数を入力
def __init__(self, n, mod):
self.mod = mod
# self.fac[i] ≡ i! (factorial:階乗)
self.fac = [1]
num = 1
for i in range(1, n+1):
num *= i
num %= mod
self.fac.append(num)
# self.rec[i] ≡ 1 / i! (reciprocal:逆数)
num = pow(num, mod-2, mod)
self.rec = [1 for i in range(n+1)]
self.rec[n] = num
for i in range(n-1, 0, -1):
num *= i + 1
num %= mod
self.rec[i] = num
# comb(n, r) ≡ nCr
def comb(self, n, r):
return self.fac[n] * self.rec[r] * self.rec[n - r] % self.mod
# perm(n, r) ≡ nPr
def perm(self, n, r):
return self.fac[n] * self.rec[n-r] % self.mod
# fact(n) ≡ n!
def fact(self, n):
return self.fac[n]
h, w, a, b = map(int, input().split())
mod = 10**9 + 7
f = Factorials(2 * (10 ** 5), mod)
if h - a < w - b:
num = h - a
else:
num = w - b
ans = 0
for i in range(num):
x = h - a - i
y = b + i + 1
ans += f.comb(x + y - 2, x - 1) * f.comb(h + w - x - y, h - 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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s450458002 | p04046 | Accepted | class mod_comb_k():
def __init__(self, MAX_N = 10**6, mod = 10**9+7):
self.fact = [1]
self.fact_inv = [0] * (MAX_N + 4)
self.mod = mod
if MAX_N > mod:print('MAX_N > mod !')
for i in range(MAX_N + 3):
self.fact.append(self.fact[-1] * (i + 1) % self.mod)
self.fact_inv[-1] = pow(self.fact[-1], self.mod - 2, self.mod)
for i in range(MAX_N + 2, -1, -1):
self.fact_inv[i] = self.fact_inv[i + 1] * (i + 1) % self.mod
def comb(self, n, k):
if n < k:
print('n < k !')
return 0
else:return self.fact[n] * self.fact_inv[k] % self.mod * self.fact_inv[n - k] % self.mod
h,w,a,b=map(int,input().split())
c=mod_comb_k()
mod=10**9+7
ans=c.comb(h+w-2,w-1)
for i in range(b):
ans-=(c.comb(i+h-a-1,i)*c.comb(a+w-2-i,a-1))%mod
print(ans%mod) | 2 3 1 1
| 2
| <span class="lang-en">
<p>Score : <var>400</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns.
Iroha is now standing in the top-left cell.
She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p>
<p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p>
<p>Find the number of ways she can travel to the bottom-right cell.</p>
<p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var> 1 ≦ H, W ≦ 100,000</var></li>
<li><var> 1 ≦ A < H</var></li>
<li><var> 1 ≦ B < 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> |
s619637048 | p04046 | Accepted | h, w, a, b = map(int, input().split())
MOD = 10 ** 9 + 7
N = h + w + 10
fact = [0 for _ in range(N)]
invfact = [0 for _ in range(N)]
fact[0] = 1
for i in range(1, N):
fact[i] = fact[i - 1] * i % MOD
invfact[N - 1] = pow(fact[N - 1], MOD - 2, MOD)
for i in range(N - 2, -1, -1):
invfact[i] = invfact[i + 1] * (i + 1) % MOD
def nCk(n, k):
if k < 0 or n < k:
return 0
else:
return fact[n] * invfact[k] * invfact[n - k] % MOD
ans = 0
a += 1
b += 1
n = h - a + b - 1
n2 = w - b + a - 1
while a <= h and b <= w:
ans += nCk(n, b - 1) * nCk(n2, w - b)
ans %= MOD
a += 1
b += 1
print(ans) | 2 3 1 1
| 2
| <span class="lang-en">
<p>Score : <var>400</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns.
Iroha is now standing in the top-left cell.
She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p>
<p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p>
<p>Find the number of ways she can travel to the bottom-right cell.</p>
<p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var> 1 ≦ H, W ≦ 100,000</var></li>
<li><var> 1 ≦ A < H</var></li>
<li><var> 1 ≦ B < 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> |
s805700011 | p04046 | Accepted | h, w, a, b = map(int, input().split())
n = 2 * 10 ** 5
k = 2 * 10 ** 5
mod = 10**9 + 7
modinv_table = [-1] * (k+1)
modinv_table[1] = 1
for i in range(2, k+1):
modinv_table[i] = (-modinv_table[mod % i] * (mod // i)) % mod
def prepare_binomial_coefficients(n, k):
for i in range(1,n+1):
bc_num[i] = i * bc_num[i-1] % mod
bc_num[0] = 0
for i in range(1,k+1):
bc_den[i] = modinv_table[i] * bc_den[i-1] % mod
return
def binomial_coefficients(n, k):
if n == 0 and k == 0:
return 1
return bc_num[n] * bc_den[k] * bc_den[n-k] % mod
bc_num = [1]*(n+1)
bc_den = [1]*(n+1)
prepare_binomial_coefficients(n, n)
mids = [0]*(w-b)
dis_mid = h - a - 1
for i in range(w-b):
mids[i] = binomial_coefficients(dis_mid+i+b,i+b)
#print(mids)
mids_down = [0]*(w-b)
dis_mid = a
for i in range(w-b):
mids_down[i] = binomial_coefficients(dis_mid+w-b-i-1,w-b-i-1)
#print(mids_down)
for i in range(w-b-1):
mids_down[i] -= mids_down[i+1]
#print(mids_down)
for i in range(w-b):
mids[i] = (mids[i] * mids_down[i]) % mod
#print(mids)
print(sum(mids)%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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s444470453 | p04046 | Accepted | h, w, a, b = map(int, input().split())
n = 2 * 10 ** 5
k = 2 * 10 ** 5
mod = 10**9 + 7
modinv_table = [-1] * (k+1)
modinv_table[1] = 1
for i in range(2, k+1):
modinv_table[i] = (-modinv_table[mod % i] * (mod // i)) % mod
def prepare_binomial_coefficients(n, k):
for i in range(1,n+1):
bc_num[i] = i * bc_num[i-1] % mod
bc_num[0] = 0
for i in range(1,k+1):
bc_den[i] = modinv_table[i] * bc_den[i-1] % mod
return
def binomial_coefficients(n, k):
if n == 0 and k == 0:
return 1
return bc_num[n] * bc_den[k] * bc_den[n-k] % mod
bc_num = [1]*(n+1)
bc_den = [1]*(n+1)
prepare_binomial_coefficients(n, n)
mids = [0]*(w-b)
dis_mid = h - a - 1
for i in range(w-b):
mids[i] = binomial_coefficients(dis_mid+i+b,i+b)
#print(mids)
mids_down = [0]*(w-b)
dis_mid = a
for i in range(w-b):
mids_down[i] = binomial_coefficients(dis_mid+w-b-i-1,w-b-i-1)
#print(mids_down)
for i in range(w-b-1):
mids_down[i] -= mids_down[i+1]
#print(mids_down)
for i in range(w-b):
mids[i] = (mids[i] * mids_down[i]) % mod
#print(mids)
print(sum(mids)%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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s179125845 | p04046 | Accepted | mod = 10 ** 9 + 7
h, w, a, b = map(int, input().split())
def comb(n, r):
if n < r:return 0
if n < 0 or r < 0:return 0
return fa[n] * fi[r] % mod * fi[n - r] % mod
fa = [1] * (h + w + 1)
fi = [1] * (h + w + 1)
for i in range(1, h + w + 1):
fa[i] = fa[i - 1] * i % mod
fi[i] = pow(fa[i], mod - 2, mod)
ans = 0
for i in range(h - a):
ans += comb(b + i - 1, b - 1) * comb(h + w - b - i - 2, w - b - 1)
ans %= mod
print(ans % mod) | 2 3 1 1
| 2
| <span class="lang-en">
<p>Score : <var>400</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns.
Iroha is now standing in the top-left cell.
She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p>
<p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p>
<p>Find the number of ways she can travel to the bottom-right cell.</p>
<p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var> 1 ≦ H, W ≦ 100,000</var></li>
<li><var> 1 ≦ A < H</var></li>
<li><var> 1 ≦ B < 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> |
s548124321 | p04046 | Accepted | import sys
from functools import lru_cache
sys.setrecursionlimit(10**9)
def mul(x, y, mod):
x %= mod
y %= mod
return x * y % mod
def div(x, y, mod):
x %= mod
y %= mod
return x * mod_pow(y, mod - 2, mod) % mod
@lru_cache(maxsize=None)
def mod_pow(a, p, mod):
if p == 0:
return 1
if p % 2 == 0:
root = mod_pow(a, p / 2, mod)
return root * root % mod
else:
return a * mod_pow(a, p - 1, mod) % mod
def dnm(n, mod, factorials):
denominators = [0] * (n + 1)
denominators[n] = div(1, factorials, mod)
for i in reversed(range(n)):
denominators[i] = mul(denominators[i+1], i+1, mod)
return denominators
def nmr(n, mod):
factorials = [0] * (n + 1)
factorials[0] = 1
for i in range(n):
factorials[i + 1] = (i + 1) * factorials[i] % mod
return factorials
def cmb(a, b, mod, factorials, factorial_divs):
if len(factorials) == 0:
raise
af = factorials[a]
bf = factorial_divs[b]
abf = factorial_divs[a - b]
return mul(mul(af, bf, mod), abf, mod)
MOD=10**9+7
H,W,A,B=map(int,input().split())
ans = 0
n = nmr(H+W-2, MOD)
d = dnm(H+W-2, MOD, n[H+W-2])
for i in range(H-A):
x = cmb(B-1+i, B-1, MOD, n, d)
y = cmb(H+W-3-(B-1+i), W-B-1, MOD, n, d)
ans += x * y % 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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s829535738 | p04046 | Accepted | import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7
class CmbMod:
def __init__(self, n, p):
"""
二項係数nCr(n個の区別できるものからr個のものを選ぶ組み合わせの数)をpで割った余りを求める
"""
self.n = n
self.p = p
self.fact = [1, 1]
self.factinv = [1, 1]
self.inv = [0, 1]
def cmb_mod(self, n, r):
"""
二項係数nCr(mod p)をO(r)にて計算。nが大きいがrは小さい時に使用。
"""
numer, denom = 1, 1
for i in range(r):
numer = (numer * (n - i)) % self.p
denom = (denom * (i + 1)) % self.p
return (numer * pow(denom, self.p - 2, self.p)) % self.p
def prep(self):
"""
二項係数nCr(mod p)をO(1)で求める為の前処理をO(N)にて実行。
"""
for i in range(2, self.n + 1):
self.fact.append((self.fact[-1] * i) % self.p)
self.inv.append((-self.inv[self.p % i] * (self.p // i)) % self.p)
self.factinv.append((self.factinv[-1] * self.inv[-1]) % self.p)
def cmb_mod_with_prep(self, n, r):
"""
二項係数nCr(mod p)をO(1)で求める。事前にprepを実行する事。
"""
if (r < 0) or (n < r):
return 0
r = min(r, n - r)
return self.fact[n] * self.factinv[r] * self.factinv[n - r] % self.p
def resolve():
H, W, A, B = map(int, input().split())
H -= 1
W -= 1
cmb = CmbMod(H + W, mod)
cmb.prep()
total = cmb.cmb_mod_with_prep(H + W, H)
for w in range(B):
tmp = (cmb.cmb_mod_with_prep(H - A + w, w) * cmb.cmb_mod_with_prep(A - 1 + W - w, A - 1)) % mod
total -= tmp
total %= mod
print(total)
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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s470247790 | p04046 | Accepted | LIMIT = 2 * 10 ** 5
fac = [0] * (LIMIT + 10)
fac[0] = 1
inv = [0] * (LIMIT + 10)
MOD = 1_000_000_007
n = 1
for i in range(1, LIMIT + 1):
n *= i
n %= MOD
fac[i] = n
inv[LIMIT] = pow(fac[LIMIT], MOD - 2, MOD)
for i in range(LIMIT, -1, -1):
inv[i - 1] = (inv[i] * i) % MOD
def C(n, r):
return (fac[n] * inv[n-r] * inv[r]) % MOD
H, W, A, B = map(int, input().split())
ans = 0
B1 = B - 1
WB1 = W - B - 1
for i in range(H - A):
ans += C(i + B1, B1) * C(H - i - 1 + WB1, WB1)
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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s609453539 | p04046 | Accepted | H, W, A, B = map(int, input().split())
# H行W列 左下から縦A横B
def mod(num):
return num % (10 ** 9 + 7)
n = H + W + 1
fac = [0] * n
rev = [0] * n
fac[0] = 1
for i in range(1, n):
fac[i] = mod(fac[i-1] * i)
# 逆元の計算 x**(-1) ≡ x**(10**9 + 5) (mod 10**9 + 7)
rev[n-1] = pow(fac[n-1], 10**9 + 5, 10**9 + 7)
for i in range(n - 2, 0, -1):
rev[i] = mod(rev[i + 1] * (i + 1))
rev[0] = 1
t1 = [0] * (W - B)
for i in range(W - B):
t1[i] = mod(fac[H - A - 1 + i + B] * mod(rev[B + i] * rev[H - A - 1]))
res = 0
for i in range(W - B):
r = t1[i] * mod(fac[A - 1 + W - B - 1 - i] * mod(rev[A - 1] * rev[W - B - 1 - i]))
res = mod(res + r)
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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s371618820 | p04046 | Accepted | import sys
import numpy as np
from heapq import heappush, heappop
from bisect import bisect_left as bi_l, bisect_right as bi_r
from collections import deque, Counter, defaultdict
from itertools import combinations, product
import string
inf = float('inf')
MOD = 10**9+7
# MOD = 998244353
class NumberTheory():
def __init__(self, n=2*10**6, numpy=True):
self.n = n
self.np_flg = numpy
self.is_prime_number, self.prime_numbers = self.sieve_of_eratosthenes(n)
def sieve_of_eratosthenes(self, n):
if self.np_flg:
sieve = np.ones(n+1, dtype=np.int64); sieve[:2] = 0
for i in range(2, int(n**.5)+1):
if sieve[i]: sieve[i*2::i] = 0
prime_numbers = np.flatnonzero(sieve)
else:
sieve = [1] * (n+1); sieve[0] = sieve[1] = 0
for i in range(2, int(n**.5)+1):
if not sieve[i]: continue
for j in range(i*2, n+1, i): sieve[j] = 0
prime_numbers = [i for i in range(2, n+1) if sieve[i]]
return sieve, prime_numbers
def prime_factorize(self, n):
res = dict()
if n < 2: return res
border = int(n**.5)
for p in self.prime_numbers:
if p > border: break
while n % p == 0: res[p] = res.get(p, 0)+1; n //= p
if n == 1: return res
res[n] = 1; return res
def prime_factorize_factorial(self, n):
res = dict()
for i in range(2, n+1):
for p, c in self.prime_factorize(i).items(): res[p] = res.get(p, 0)+c
return res
@staticmethod
def gcd(a, b): return gcd(b, a%b) if b else abs(a)
@staticmethod
def lcm(a, b): return abs(a // gcd(a, b) * b)
@staticmethod
def find_divisors(n):
divisors = []
for i in range(1, int(n**.5)+1):
if n%i: continue
divisors.append(i)
j = n // i
if j != i: divisors.append(j)
return divisors
@staticmethod
def base_convert(n, b):
if not n: return [0]
res = []
while n:
n, r = divmod(n, b)
if r < 0: n += 1; r -= b
res.append(r)
return res
class UnionFind():
def __init__(self, n=10**6):
self.root = list(range(n))
self.height = [0] * n
self.size = [1] * n
def find_root(self, u):
if self.root[u] == u: return u
self.root[u] = self.find_root(self.root[u])
return self.root[u]
def unite(self, u, v):
ru = self.find_root(u)
rv = self.find_root(v)
if ru == rv: return
hu = self.height[ru]
hv = self.height[rv]
if hu >= hv:
self.root[rv] = ru
self.size[ru] += self.size[rv]
self.height[ru] = max(hu, hv+1)
else:
self.root[ru] = rv
self.size[rv] += self.size[ru]
class Combinatorics():
def __init__(self, N=10**9, n=10**6, mod=10**9+7, numpy=True):
self.mod = mod
self.nCr = dict()
self.np_flg=numpy
self.make_mod_tables(N, n)
sys.setrecursionlimit(10**6)
def choose(self, n, r, mod=None): # no mod, or mod ≠ prime
if r > n or r < 0: return 0
if r == 0: return 1
if (n, r) in self.nCr: return self.nCr[(n, r)]
if not mod:
self.nCr[(n, r)] = (self.choose(n-1, r) + self.choose(n-1, r-1))
else:
self.nCr[(n, r)] = (self.choose(n-1, r, mod) + self.choose(n-1, r-1, mod)) % mod
return self.nCr[(n,r)]
def cumprod(self, a):
p = self.mod
l = len(a); sql = int(np.sqrt(l)+1)
a = np.resize(a, sql**2).reshape(sql, sql)
for i in range(sql-1): a[:, i+1] *= a[:, i]; a[:, i+1] %= p
for i in range(sql-1): a[i+1] *= a[i, -1]; a[i+1] %= p
return np.ravel(a)[:l]
def make_mod_tables(self, N, n):
p = self.mod
if self.np_flg:
fac = np.arange(n+1); fac[0] = 1; fac = self.cumprod(fac)
ifac = np.arange(n+1, 0, -1); ifac[0] = pow(int(fac[-1]), p-2, p)
ifac = self.cumprod(ifac)[n::-1]
n_choose = np.arange(N+1, N-n, -1); n_choose[0] = 1;
n_choose[1:] = self.cumprod(n_choose[1:])*ifac[1:n+1]%p
else:
fac = [None]*(n+1); fac[0] = 1
for i in range(n): fac[i+1] = fac[i]*(i+1)%p
ifac = [None]*(n+1); ifac[n] = pow(fac[n], p-2, p)
for i in range(n, 0, -1): ifac[i-1] = ifac[i]*i%p
n_choose = [None] * (n+1); n_choose[0] = 1
for i in range(n): n_choose[i+1] = n_choose[i]*(N-i)%p
for i in range(n+1): n_choose[i] = n_choose[i]*ifac[i]%p
self.fac, self.ifac, self.mod_n_choose = fac, ifac, n_choose
def mod_choose(self, n, r):
return self.fac[n]*self.ifac[r]%self.mod*self.ifac[n-r]%self.mod
def z_algorithm(s):
n = len(s)
a = [0] * n; a[0] = n
l = r = -1
for i in range(1, n):
if r >= i: a[i] = min(a[i-l], r-i)
while i + a[i] < n and s[i+a[i]] == s[a[i]]: a[i] += 1
if i+a[i] >= r: l, r = i, i+a[i]
return a
class ABC001():
def A():
h1, h2 = map(int, sys.stdin.read().split())
print(h1-h2)
def B(): pass
def C(): pass
def D(): pass
class ABC002():
def A():
x, y = map(int, sys.stdin.readline().split())
print(max(x, y))
def B():
vowels = set('aeiou')
s = sys.stdin.readline().rstrip()
t = ''
for c in s:
if c in vowels: continue
t += c
print(t)
def C():
*coords, = map(int, sys.stdin.readline().split())
def triangle_area(x0, y0, x1, y1, x2, y2):
x1 -= x0; x2 -= x0; y1 -= y0; y2 -= y0;
return abs(x1*y2 - x2*y1) / 2
print(triangle_area(*coords))
def D():
n, m = map(int, sys.stdin.readline().split())
edges = set()
for _ in range(m):
x, y = map(int, sys.stdin.readline().split())
x -= 1; y -= 1
edges.add((x, y))
cand = []
for i in range(1, 1<<n):
s = [j for j in range(n) if i>>j & 1]
for x, y in combinations(s, 2):
if (x, y) not in edges: break
else:
cand.append(len(s))
print(max(cand))
class ABC003():
def A():
n = int(sys.stdin.readline().rstrip())
print((n+1)*5000)
def B():
atcoder = set('atcoder')
s, t = sys.stdin.read().split()
for i in range(len(s)):
if s[i] == t[i]: continue
if s[i] == '@' and t[i] in atcoder: continue
if t[i] == '@' and s[i] in atcoder: continue
print('You will lose')
return
print('You can win')
def C():
n, k, *r = map(int, sys.stdin.read().split())
res = 0
for x in sorted(r)[-k:]:
res = (res+x) / 2
print(res)
def D(): pass
class ABC004():
def A():
print(int(sys.stdin.readline().rstrip())*2)
def B():
c = [sys.stdin.readline().rstrip() for _ in range(4)]
for l in c[::-1]:
print(l[::-1])
def C():
n = int(sys.stdin.readline().rstrip())
n %= 30
res = list(range(1, 7))
for i in range(n):
i %= 5
res[i], res[i+1] = res[i+1], res[i]
print(''.join(map(str, res)))
def D(): pass
class ABC005():
def A():
x, y = map(int, sys.stdin.readline().split())
print(y//x)
def B():
n, *t = map(int, sys.stdin.read().split())
print(min(t))
def C():
t = int(sys.stdin.readline().rstrip())
n = int(sys.stdin.readline().rstrip())
a = [int(x) for x in sys.stdin.readline().split()]
m = int(sys.stdin.readline().rstrip())
b = [int(x) for x in sys.stdin.readline().split()]
i = 0
for p in b:
if i == n: print('no'); return
while p-a[i] > t:
i += 1
if i == n: print('no'); return
if a[i] > p: print('no'); return
i += 1
print('yes')
def D():
n = int(sys.stdin.readline().rstrip())
d = np.array([sys.stdin.readline().split() for _ in range(n)], np.int64)
s = d.cumsum(axis=0).cumsum(axis=1)
s = np.pad(s, 1)
max_del = np.zeros((n+1, n+1), dtype=np.int64)
for y in range(1, n+1):
for x in range(1, n+1):
max_del[y, x] = np.amax(s[y:n+1, x:n+1] - s[0:n-y+1, x:n+1] - s[y:n+1, 0:n-x+1] + s[0:n-y+1, 0:n-x+1])
res = np.arange(n**2+1)[:, None]
i = np.arange(1, n+1)
res = max_del[i, np.minimum(res//i, n)].max(axis=1)
q = int(sys.stdin.readline().rstrip())
p = np.array(sys.stdin.read().split(), dtype=np.int64)
print(*res[p], sep='\n')
class ABC006():
def A():
n = sys.stdin.readline().rstrip()
if '3' in n: print('YES')
elif int(n)%3 == 0: print('YES')
else: print('NO')
def B():
mod = 10007
t = [0, 0, 1]
for _ in range(1001001):
t.append(t[-1]+t[-2]+t[-3]); t[-1] %= mod
n = int(sys.stdin.readline().rstrip())
print(t[n-1])
def C():
n, m = map(int, sys.stdin.readline().split())
cnt = [0, 0, 0]
if m == 1: cnt = [-1, -1, -1]
else:
if m & 1: m -= 3; cnt[1] += 1; n -= 1
cnt[2] = m//2 - n
cnt[0] = n - cnt[2]
if cnt[0]<0 or cnt[1]<0 or cnt[2]<0: print(-1, -1, -1)
else: print(*cnt, sep=' ')
def D():
n, *c = map(int, sys.stdin.read().split())
lis = [inf]*n
for x in c: lis[bi_l(lis, x)] = x
print(n - bi_l(lis, inf))
class ABC007():
def A():
n = int(sys.stdin.readline().rstrip())
print(n-1)
def B():
s = sys.stdin.readline().rstrip()
if s == 'a': print(-1)
else: print('a')
def C():
r, c = map(int, sys.stdin.readline().split())
sy, sx = map(int, sys.stdin.readline().split())
gy, gx = map(int, sys.stdin.readline().split())
sy -= 1; sx -=1; gy -= 1; gx -= 1
maze = [sys.stdin.readline().rstrip() for _ in range(r)]
queue = deque([(sy, sx)])
dist = np.full((r, c), np.inf); dist[sy, sx] = 0
while queue:
y, x = queue.popleft()
for i, j in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
i += y; j += x
if maze[i][j] == '#' or dist[i, j] != np.inf: continue
dist[i, j] = dist[y, x] + 1
queue.append((i, j))
print(int(dist[gy, gx]))
def D(): pass
class ABC008():
def A():
s, t = map(int, sys.stdin.readline().split())
print(t-s+1)
def B():
n, *s = sys.stdin.read().split()
res = defaultdict(int)
for name in s: res[name] += 1
print(sorted(res.items(), key=lambda x: x[1])[-1][0])
def C():
n, *a = map(int, sys.stdin.read().split())
a = np.array(a)
c = n - np.count_nonzero(a[:, None]%a, axis=1)
print(np.sum((c+1)//2/c))
def D(): pass
class ABC009():
def A():
n = int(sys.stdin.readline().rstrip())
print((n+1)//2)
def B():
n, *a = map(int, sys.stdin.read().split())
print(sorted(set(a))[-2])
def C():
n, k = map(int, sys.stdin.readline().split())
s = list(sys.stdin.readline().rstrip())
cost = [1]*n
r = k
for i in range(n-1):
q = []
for j in range(i+1, n):
if s[j] < s[i] and cost[i]+cost[j] <= r:
heappush(q, (s[j], cost[i]+cost[j], -j))
if not q: continue
_, c, j = heappop(q); j = -j
s[i], s[j] = s[j], s[i]
r -= c
cost[i] = cost[j] = 0
print(''.join(s))
def D(): pass
class ABC010():
def A():
print(sys.stdin.readline().rstrip()+'pp')
def B():
n, *a = map(int, sys.stdin.read().split())
tot = 0
for x in a:
c = 0
while x%2==0 or x%3==2:
x -= 1
c += 1
tot += c
print(tot)
def C():
sx, sy, gx, gy, t, v, n, *xy = map(int, sys.stdin.read().split())
x, y = np.array(xy).reshape(-1, 2).T
def dist(x1, y1, x2, y2):
return np.sqrt((x2-x1)**2 + (y2-y1)**2)
ans = 'YES' if (dist(sx, sy, x, y)+dist(x, y, gx, gy) <= v*t).any() else 'NO'
print(ans)
def D(): pass
class ABC011():
def A():
n = int(sys.stdin.readline().rstrip())
print(n%12+1)
def B():
s = sys.stdin.readline().rstrip()
print(s[0].upper()+s[1:].lower())
def C():
n, *ng = map(int, sys.stdin.read().split())
ng = set(ng)
if n in ng: print('NO')
else:
r = 100
while n > 0:
if r == 0: print('NO'); return
for i in range(3, 0, -1):
if (n-i) in ng: continue
n -= i
r -= 1
break
else: print('NO'); return
print('YES')
def D(): pass
class ABC041():
def A():
s, i = sys.stdin.read().split()
i = int(i)
print(s[i-1])
def B():
a, b, c = map(int, sys.stdin.readline().split())
ans = a * b % MOD * c % MOD
print(ans)
def C():
n, *a = map(int, sys.stdin.read().split())
for i, h in sorted(enumerate(a), key=lambda x: -x[1]):
print(i+1)
def D():
n, m, *xy = map(int, sys.stdin.read().split())
*xy, = zip(*[iter(xy)]*2)
edges = [0] * n
for x, y in xy:
x -= 1; y -= 1
edges[x] |= 1<<y
comb = [None] * (1<<n); comb[0] = 1
def count(edges, bit):
if comb[bit] is not None: return comb[bit]
comb[bit] = 0
for i in range(n):
if (bit>>i) & 1 and not edges[i]:
nxt_bit = bit & ~(1<<i)
nxt_edges = edges.copy()
for j in range(n):
nxt_edges[j] &= ~(1<<i)
cnt = count(nxt_edges, nxt_bit)
comb[bit] += cnt
return comb[bit]
print(count(edges, (1<<n)-1))
class ABC042():
def A():
a = [int(x) for x in sys.stdin.readline().split()]
c = Counter(a)
print('YES' if c[5]==2 and c[7]==1 else 'NO')
def B():
n, l, *s = sys.stdin.read().split()
print(''.join(sorted(s)))
def C():
n, k, *d = sys.stdin.read().split()
l = len(n)
ok = sorted(set(string.digits)-set(d))
cand = [int(''.join(p)) for p in product(ok, repeat=l)] + [int(min(x for x in ok if x > '0')+min(ok)*l)]
print(cand[bi_l(cand, int(n))])
def D():
h, w, a, b = map(int, sys.stdin.read().split())
combinatorics = Combinatorics(n=2*10**5, mod=MOD, numpy=True)
tot = combinatorics.mod_choose(h+w-2, h-1)
i = np.arange(h-a, h)
ng = np.sum(combinatorics.mod_choose(i+b-1, i) * combinatorics.mod_choose(h-i+w-b-2, h-1-i) % MOD)
tot -= ng; tot %= MOD
print(tot)
class ABC170():
def A():
x = [int(x) for x in sys.stdin.readline().split()]
for i in range(5):
if x[i] != i+1:
print(i+1)
break
def B():
x, y = map(int, sys.stdin.readline().split())
print('Yes' if 2*x <= y <= 4*x and y%2 == 0 else 'No')
def C():
x, n, *p = map(int, sys.stdin.read().split())
a = list(set(range(102)) - set(p))
a = [(abs(y-x), y) for y in a]
print(sorted(a)[0][1])
def D():
n, *a = map(int, sys.stdin.read().split())
cand = set(a)
cnt = 0
for x, c in sorted(Counter(a).items()):
cnt += c == 1 and x in cand
cand -= set(range(x*2, 10**6+1, x))
print(cnt)
def E():
n, q = map(int, sys.stdin.readline().split())
queue = []
num_kindergarten = 2*10**5
queue_kindergarten = [[] for _ in range(num_kindergarten)]
highest_kindergarten = [None] * num_kindergarten
where = [None] * n
rate = [None] * n
def entry(i, k):
where[i] = k
while queue_kindergarten[k]:
r, j = heappop(queue_kindergarten[k])
if where[j] != k or j == i: continue
if rate[i] >= -r:
highest_kindergarten[k] = rate[i]
heappush(queue, (rate[i], k, i))
heappush(queue_kindergarten[k], (r, j))
break
else:
highest_kindergarten[k] = rate[i]
heappush(queue, (rate[i], k, i))
heappush(queue_kindergarten[k], (-rate[i], i))
def transfer(i, k):
now = where[i]
while queue_kindergarten[now]:
r, j = heappop(queue_kindergarten[now])
if where[j] != now or j == i: continue
if highest_kindergarten[now] != -r:
highest_kindergarten[now] = -r
heappush(queue, (-r, now, j))
heappush(queue_kindergarten[now], (r, j))
break
else:
highest_kindergarten[now] = None
entry(i, k)
def inquire():
while True:
r, k, i = heappop(queue)
if where[i] != k or r != highest_kindergarten[k]: continue
heappush(queue, (r, k, i))
return r
for i in range(n):
a, b = map(int, sys.stdin.readline().split())
rate[i] = a
entry(i, b-1)
for _ in range(q):
c, d = map(int, sys.stdin.readline().split())
transfer(c-1, d-1)
print(inquire())
def F(): pass
class ABC171():
def A():
c = sys.stdin.readline().rstrip()
print('A' if c < 'a' else 'a')
def B():
n, k, *p = map(int, sys.stdin.read().split())
print(sum(sorted(p)[:k]))
def C():
n = int(sys.stdin.readline().rstrip())
n -= 1
l = 1
while True:
if n < pow(26, l):
break
n -= pow(26, l)
l += 1
res = ''.join([chr(ord('a')+d%26) for d in NumberTheory.base_convert(n, 26)][::-1])
res = 'a'*(l-len(res)) + res
print(res)
def D():
n = int(sys.stdin.readline().rstrip())
a = [int(x) for x in sys.stdin.readline().split()]
s = sum(a)
cnt = Counter(a)
q = int(sys.stdin.readline().rstrip())
for _ in range(q):
b, c = map(int, sys.stdin.readline().split())
s += (c-b)*cnt[b]
print(s)
cnt[c] += cnt[b]; cnt[b] = 0
def E():
n, *a = map(int, sys.stdin.read().split())
s = 0
for x in a: s ^= x
b = map(lambda x: x^s, a)
print(*b, sep=' ')
def F(): pass
class ABC172():
def A(): pass
def B(): pass
def C(): pass
def D(): pass
def E(): pass
def F(): pass
class ABC173():
def A():
n = int(sys.stdin.readline().rstrip())
charge = (n+999)//1000 * 1000 - n
print(charge)
def B():
n, *s = sys.stdin.read().split()
c = Counter(s)
for v in 'AC, WA, TLE, RE'.split(', '):
print(f'{v} x {c[v]}')
def C():
h, w, k = map(int, sys.stdin.readline().split())
c = [sys.stdin.readline().rstrip() for _ in range(h)]
tot = 0
for i in range(1<<h):
for j in range(1<<w):
cnt = 0
for y in range(h):
for x in range(w):
if i>>y & 1 or j>>x & 1:
continue
cnt += c[y][x] == '#'
tot += cnt == k
print(tot)
def D():
n, *a = map(int, sys.stdin.read().split())
a.sort(reverse=True)
res = a[0] + sum(a[1:1+(n-2)//2])*2 + a[1+(n-2)//2]*(n & 1)
print(res)
def E():
MOD = 10**9+7
n, k, *a = map(int, sys.stdin.read().split())
minus = [x for x in a if x < 0]
plus = [x for x in a if x > 0]
if len(plus) + len(minus)//2*2 >= k: # plus
*minus, = map(abs, minus)
minus.sort(reverse=True)
plus.sort(reverse=True)
cand = []
if len(minus)&1: minus = minus[:-1]
for i in range(0, len(minus)-1, 2):
cand.append(minus[i]*minus[i+1]%MOD)
if k & 1:
res = plus[0]
plus = plus[1:]
else:
res = 1
if len(plus)&1: plus = plus[:-1]
for i in range(0, len(plus)-1, 2):
cand.append(plus[i]*plus[i+1]%MOD)
cand.sort(reverse=True)
for x in cand[:k//2]:
res *= x
res %= MOD
print(res)
elif 0 in a:
print(0)
else:
cand = sorted(map(abs, a))
res = 1
for i in range(k):
res *= cand[i]
res %= MOD
res = MOD - res
print(res)
pass
def F(): pass
if __name__ == '__main__':
ABC042.D() | 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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s354779992 | p04046 | Accepted | import sys
import numpy as np
from heapq import heappush, heappop
from bisect import bisect_left as bi_l, bisect_right as bi_r
from collections import deque, Counter, defaultdict
from itertools import combinations, product
import string
inf = float('inf')
MOD = 10**9+7
# MOD = 998244353
class NumberTheory():
def __init__(self, n=2*10**6, numpy=True):
self.n = n
self.np_flg = numpy
self.is_prime_number, self.prime_numbers = self.sieve_of_eratosthenes(n)
def sieve_of_eratosthenes(self, n):
if self.np_flg:
sieve = np.ones(n+1, dtype=np.int64); sieve[:2] = 0
for i in range(2, int(n**.5)+1):
if sieve[i]: sieve[i*2::i] = 0
prime_numbers = np.flatnonzero(sieve)
else:
sieve = [1] * (n+1); sieve[0] = sieve[1] = 0
for i in range(2, int(n**.5)+1):
if not sieve[i]: continue
for j in range(i*2, n+1, i): sieve[j] = 0
prime_numbers = [i for i in range(2, n+1) if sieve[i]]
return sieve, prime_numbers
def prime_factorize(self, n):
res = dict()
if n < 2: return res
border = int(n**.5)
for p in self.prime_numbers:
if p > border: break
while n % p == 0: res[p] = res.get(p, 0)+1; n //= p
if n == 1: return res
res[n] = 1; return res
def prime_factorize_factorial(self, n):
res = dict()
for i in range(2, n+1):
for p, c in self.prime_factorize(i).items(): res[p] = res.get(p, 0)+c
return res
@staticmethod
def gcd(a, b): return gcd(b, a%b) if b else abs(a)
@staticmethod
def lcm(a, b): return abs(a // gcd(a, b) * b)
@staticmethod
def find_divisors(n):
divisors = []
for i in range(1, int(n**.5)+1):
if n%i: continue
divisors.append(i)
j = n // i
if j != i: divisors.append(j)
return divisors
@staticmethod
def base_convert(n, b):
if not n: return [0]
res = []
while n:
n, r = divmod(n, b)
if r < 0: n += 1; r -= b
res.append(r)
return res
class UnionFind():
def __init__(self, n=10**6):
self.root = list(range(n))
self.height = [0] * n
self.size = [1] * n
def find_root(self, u):
if self.root[u] == u: return u
self.root[u] = self.find_root(self.root[u])
return self.root[u]
def unite(self, u, v):
ru = self.find_root(u)
rv = self.find_root(v)
if ru == rv: return
hu = self.height[ru]
hv = self.height[rv]
if hu >= hv:
self.root[rv] = ru
self.size[ru] += self.size[rv]
self.height[ru] = max(hu, hv+1)
else:
self.root[ru] = rv
self.size[rv] += self.size[ru]
class Combinatorics():
def __init__(self, N=10**9, n=10**6, mod=10**9+7, numpy=True):
self.mod = mod
self.nCr = dict()
self.np_flg=numpy
self.make_mod_tables(N, n)
sys.setrecursionlimit(10**6)
def choose(self, n, r, mod=None): # no mod, or mod ≠ prime
if r > n or r < 0: return 0
if r == 0: return 1
if (n, r) in self.nCr: return self.nCr[(n, r)]
if not mod:
self.nCr[(n, r)] = (self.choose(n-1, r) + self.choose(n-1, r-1))
else:
self.nCr[(n, r)] = (self.choose(n-1, r, mod) + self.choose(n-1, r-1, mod)) % mod
return self.nCr[(n,r)]
def cumprod(self, a):
p = self.mod
l = len(a); sql = int(np.sqrt(l)+1)
a = np.resize(a, sql**2).reshape(sql, sql)
for i in range(sql-1): a[:, i+1] *= a[:, i]; a[:, i+1] %= p
for i in range(sql-1): a[i+1] *= a[i, -1]; a[i+1] %= p
return np.ravel(a)[:l]
def make_mod_tables(self, N, n):
p = self.mod
if self.np_flg:
fac = np.arange(n+1); fac[0] = 1; fac = self.cumprod(fac)
ifac = np.arange(n+1, 0, -1); ifac[0] = pow(int(fac[-1]), p-2, p)
ifac = self.cumprod(ifac)[n::-1]
n_choose = np.arange(N+1, N-n, -1); n_choose[0] = 1;
n_choose[1:] = self.cumprod(n_choose[1:])*ifac[1:n+1]%p
else:
fac = [None]*(n+1); fac[0] = 1
for i in range(n): fac[i+1] = fac[i]*(i+1)%p
ifac = [None]*(n+1); ifac[n] = pow(fac[n], p-2, p)
for i in range(n, 0, -1): ifac[i-1] = ifac[i]*i%p
n_choose = [None] * (n+1); n_choose[0] = 1
for i in range(n): n_choose[i+1] = n_choose[i]*(N-i)%p
for i in range(n+1): n_choose[i] = n_choose[i]*ifac[i]%p
self.fac, self.ifac, self.mod_n_choose = fac, ifac, n_choose
def mod_choose(self, n, r):
return self.fac[n]*self.ifac[r]%self.mod*self.ifac[n-r]%self.mod
def z_algorithm(s):
n = len(s)
a = [0] * n; a[0] = n
l = r = -1
for i in range(1, n):
if r >= i: a[i] = min(a[i-l], r-i)
while i + a[i] < n and s[i+a[i]] == s[a[i]]: a[i] += 1
if i+a[i] >= r: l, r = i, i+a[i]
return a
class ABC001():
def A():
h1, h2 = map(int, sys.stdin.read().split())
print(h1-h2)
def B(): pass
def C(): pass
def D(): pass
class ABC002():
def A():
x, y = map(int, sys.stdin.readline().split())
print(max(x, y))
def B():
vowels = set('aeiou')
s = sys.stdin.readline().rstrip()
t = ''
for c in s:
if c in vowels: continue
t += c
print(t)
def C():
*coords, = map(int, sys.stdin.readline().split())
def triangle_area(x0, y0, x1, y1, x2, y2):
x1 -= x0; x2 -= x0; y1 -= y0; y2 -= y0;
return abs(x1*y2 - x2*y1) / 2
print(triangle_area(*coords))
def D():
n, m = map(int, sys.stdin.readline().split())
edges = set()
for _ in range(m):
x, y = map(int, sys.stdin.readline().split())
x -= 1; y -= 1
edges.add((x, y))
cand = []
for i in range(1, 1<<n):
s = [j for j in range(n) if i>>j & 1]
for x, y in combinations(s, 2):
if (x, y) not in edges: break
else:
cand.append(len(s))
print(max(cand))
class ABC003():
def A():
n = int(sys.stdin.readline().rstrip())
print((n+1)*5000)
def B():
atcoder = set('atcoder')
s, t = sys.stdin.read().split()
for i in range(len(s)):
if s[i] == t[i]: continue
if s[i] == '@' and t[i] in atcoder: continue
if t[i] == '@' and s[i] in atcoder: continue
print('You will lose')
return
print('You can win')
def C():
n, k, *r = map(int, sys.stdin.read().split())
res = 0
for x in sorted(r)[-k:]:
res = (res+x) / 2
print(res)
def D(): pass
class ABC004():
def A():
print(int(sys.stdin.readline().rstrip())*2)
def B():
c = [sys.stdin.readline().rstrip() for _ in range(4)]
for l in c[::-1]:
print(l[::-1])
def C():
n = int(sys.stdin.readline().rstrip())
n %= 30
res = list(range(1, 7))
for i in range(n):
i %= 5
res[i], res[i+1] = res[i+1], res[i]
print(''.join(map(str, res)))
def D(): pass
class ABC005():
def A():
x, y = map(int, sys.stdin.readline().split())
print(y//x)
def B():
n, *t = map(int, sys.stdin.read().split())
print(min(t))
def C():
t = int(sys.stdin.readline().rstrip())
n = int(sys.stdin.readline().rstrip())
a = [int(x) for x in sys.stdin.readline().split()]
m = int(sys.stdin.readline().rstrip())
b = [int(x) for x in sys.stdin.readline().split()]
i = 0
for p in b:
if i == n: print('no'); return
while p-a[i] > t:
i += 1
if i == n: print('no'); return
if a[i] > p: print('no'); return
i += 1
print('yes')
def D():
n = int(sys.stdin.readline().rstrip())
d = np.array([sys.stdin.readline().split() for _ in range(n)], np.int64)
s = d.cumsum(axis=0).cumsum(axis=1)
s = np.pad(s, 1)
max_del = np.zeros((n+1, n+1), dtype=np.int64)
for y in range(1, n+1):
for x in range(1, n+1):
max_del[y, x] = np.amax(s[y:n+1, x:n+1] - s[0:n-y+1, x:n+1] - s[y:n+1, 0:n-x+1] + s[0:n-y+1, 0:n-x+1])
res = np.arange(n**2+1)[:, None]
i = np.arange(1, n+1)
res = max_del[i, np.minimum(res//i, n)].max(axis=1)
q = int(sys.stdin.readline().rstrip())
p = np.array(sys.stdin.read().split(), dtype=np.int64)
print(*res[p], sep='\n')
class ABC006():
def A():
n = sys.stdin.readline().rstrip()
if '3' in n: print('YES')
elif int(n)%3 == 0: print('YES')
else: print('NO')
def B():
mod = 10007
t = [0, 0, 1]
for _ in range(1001001):
t.append(t[-1]+t[-2]+t[-3]); t[-1] %= mod
n = int(sys.stdin.readline().rstrip())
print(t[n-1])
def C():
n, m = map(int, sys.stdin.readline().split())
cnt = [0, 0, 0]
if m == 1: cnt = [-1, -1, -1]
else:
if m & 1: m -= 3; cnt[1] += 1; n -= 1
cnt[2] = m//2 - n
cnt[0] = n - cnt[2]
if cnt[0]<0 or cnt[1]<0 or cnt[2]<0: print(-1, -1, -1)
else: print(*cnt, sep=' ')
def D():
n, *c = map(int, sys.stdin.read().split())
lis = [inf]*n
for x in c: lis[bi_l(lis, x)] = x
print(n - bi_l(lis, inf))
class ABC007():
def A():
n = int(sys.stdin.readline().rstrip())
print(n-1)
def B():
s = sys.stdin.readline().rstrip()
if s == 'a': print(-1)
else: print('a')
def C():
r, c = map(int, sys.stdin.readline().split())
sy, sx = map(int, sys.stdin.readline().split())
gy, gx = map(int, sys.stdin.readline().split())
sy -= 1; sx -=1; gy -= 1; gx -= 1
maze = [sys.stdin.readline().rstrip() for _ in range(r)]
queue = deque([(sy, sx)])
dist = np.full((r, c), np.inf); dist[sy, sx] = 0
while queue:
y, x = queue.popleft()
for i, j in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
i += y; j += x
if maze[i][j] == '#' or dist[i, j] != np.inf: continue
dist[i, j] = dist[y, x] + 1
queue.append((i, j))
print(int(dist[gy, gx]))
def D(): pass
class ABC008():
def A():
s, t = map(int, sys.stdin.readline().split())
print(t-s+1)
def B():
n, *s = sys.stdin.read().split()
res = defaultdict(int)
for name in s: res[name] += 1
print(sorted(res.items(), key=lambda x: x[1])[-1][0])
def C():
n, *a = map(int, sys.stdin.read().split())
a = np.array(a)
c = n - np.count_nonzero(a[:, None]%a, axis=1)
print(np.sum((c+1)//2/c))
def D(): pass
class ABC009():
def A():
n = int(sys.stdin.readline().rstrip())
print((n+1)//2)
def B():
n, *a = map(int, sys.stdin.read().split())
print(sorted(set(a))[-2])
def C():
n, k = map(int, sys.stdin.readline().split())
s = list(sys.stdin.readline().rstrip())
cost = [1]*n
r = k
for i in range(n-1):
q = []
for j in range(i+1, n):
if s[j] < s[i] and cost[i]+cost[j] <= r:
heappush(q, (s[j], cost[i]+cost[j], -j))
if not q: continue
_, c, j = heappop(q); j = -j
s[i], s[j] = s[j], s[i]
r -= c
cost[i] = cost[j] = 0
print(''.join(s))
def D(): pass
class ABC010():
def A():
print(sys.stdin.readline().rstrip()+'pp')
def B():
n, *a = map(int, sys.stdin.read().split())
tot = 0
for x in a:
c = 0
while x%2==0 or x%3==2:
x -= 1
c += 1
tot += c
print(tot)
def C():
sx, sy, gx, gy, t, v, n, *xy = map(int, sys.stdin.read().split())
x, y = np.array(xy).reshape(-1, 2).T
def dist(x1, y1, x2, y2):
return np.sqrt((x2-x1)**2 + (y2-y1)**2)
ans = 'YES' if (dist(sx, sy, x, y)+dist(x, y, gx, gy) <= v*t).any() else 'NO'
print(ans)
def D(): pass
class ABC011():
def A():
n = int(sys.stdin.readline().rstrip())
print(n%12+1)
def B():
s = sys.stdin.readline().rstrip()
print(s[0].upper()+s[1:].lower())
def C():
n, *ng = map(int, sys.stdin.read().split())
ng = set(ng)
if n in ng: print('NO')
else:
r = 100
while n > 0:
if r == 0: print('NO'); return
for i in range(3, 0, -1):
if (n-i) in ng: continue
n -= i
r -= 1
break
else: print('NO'); return
print('YES')
def D(): pass
class ABC041():
def A():
s, i = sys.stdin.read().split()
i = int(i)
print(s[i-1])
def B():
a, b, c = map(int, sys.stdin.readline().split())
ans = a * b % MOD * c % MOD
print(ans)
def C():
n, *a = map(int, sys.stdin.read().split())
for i, h in sorted(enumerate(a), key=lambda x: -x[1]):
print(i+1)
def D():
n, m, *xy = map(int, sys.stdin.read().split())
*xy, = zip(*[iter(xy)]*2)
edges = [0] * n
for x, y in xy:
x -= 1; y -= 1
edges[x] |= 1<<y
comb = [None] * (1<<n); comb[0] = 1
def count(edges, bit):
if comb[bit] is not None: return comb[bit]
comb[bit] = 0
for i in range(n):
if (bit>>i) & 1 and not edges[i]:
nxt_bit = bit & ~(1<<i)
nxt_edges = edges.copy()
for j in range(n):
nxt_edges[j] &= ~(1<<i)
cnt = count(nxt_edges, nxt_bit)
comb[bit] += cnt
return comb[bit]
print(count(edges, (1<<n)-1))
class ABC042():
def A():
a = [int(x) for x in sys.stdin.readline().split()]
c = Counter(a)
print('YES' if c[5]==2 and c[7]==1 else 'NO')
def B():
n, l, *s = sys.stdin.read().split()
print(''.join(sorted(s)))
def C():
n, k, *d = sys.stdin.read().split()
l = len(n)
ok = sorted(set(string.digits)-set(d))
cand = [int(''.join(p)) for p in product(ok, repeat=l)] + [int(min(x for x in ok if x > '0')+min(ok)*l)]
print(cand[bi_l(cand, int(n))])
def D():
h, w, a, b = map(int, sys.stdin.read().split())
combinatorics = Combinatorics(mod=MOD, numpy=True)
tot = combinatorics.mod_choose(h+w-2, h-1)
i = np.arange(h-a, h)
ng = np.sum(combinatorics.mod_choose(i+b-1, i) * combinatorics.mod_choose(h-i+w-b-2, h-1-i) % MOD)
tot -= ng; tot %= MOD
print(tot)
class ABC170():
def A():
x = [int(x) for x in sys.stdin.readline().split()]
for i in range(5):
if x[i] != i+1:
print(i+1)
break
def B():
x, y = map(int, sys.stdin.readline().split())
print('Yes' if 2*x <= y <= 4*x and y%2 == 0 else 'No')
def C():
x, n, *p = map(int, sys.stdin.read().split())
a = list(set(range(102)) - set(p))
a = [(abs(y-x), y) for y in a]
print(sorted(a)[0][1])
def D():
n, *a = map(int, sys.stdin.read().split())
cand = set(a)
cnt = 0
for x, c in sorted(Counter(a).items()):
cnt += c == 1 and x in cand
cand -= set(range(x*2, 10**6+1, x))
print(cnt)
def E():
n, q = map(int, sys.stdin.readline().split())
queue = []
num_kindergarten = 2*10**5
queue_kindergarten = [[] for _ in range(num_kindergarten)]
highest_kindergarten = [None] * num_kindergarten
where = [None] * n
rate = [None] * n
def entry(i, k):
where[i] = k
while queue_kindergarten[k]:
r, j = heappop(queue_kindergarten[k])
if where[j] != k or j == i: continue
if rate[i] >= -r:
highest_kindergarten[k] = rate[i]
heappush(queue, (rate[i], k, i))
heappush(queue_kindergarten[k], (r, j))
break
else:
highest_kindergarten[k] = rate[i]
heappush(queue, (rate[i], k, i))
heappush(queue_kindergarten[k], (-rate[i], i))
def transfer(i, k):
now = where[i]
while queue_kindergarten[now]:
r, j = heappop(queue_kindergarten[now])
if where[j] != now or j == i: continue
if highest_kindergarten[now] != -r:
highest_kindergarten[now] = -r
heappush(queue, (-r, now, j))
heappush(queue_kindergarten[now], (r, j))
break
else:
highest_kindergarten[now] = None
entry(i, k)
def inquire():
while True:
r, k, i = heappop(queue)
if where[i] != k or r != highest_kindergarten[k]: continue
heappush(queue, (r, k, i))
return r
for i in range(n):
a, b = map(int, sys.stdin.readline().split())
rate[i] = a
entry(i, b-1)
for _ in range(q):
c, d = map(int, sys.stdin.readline().split())
transfer(c-1, d-1)
print(inquire())
def F(): pass
class ABC171():
def A():
c = sys.stdin.readline().rstrip()
print('A' if c < 'a' else 'a')
def B():
n, k, *p = map(int, sys.stdin.read().split())
print(sum(sorted(p)[:k]))
def C():
n = int(sys.stdin.readline().rstrip())
n -= 1
l = 1
while True:
if n < pow(26, l):
break
n -= pow(26, l)
l += 1
res = ''.join([chr(ord('a')+d%26) for d in NumberTheory.base_convert(n, 26)][::-1])
res = 'a'*(l-len(res)) + res
print(res)
def D():
n = int(sys.stdin.readline().rstrip())
a = [int(x) for x in sys.stdin.readline().split()]
s = sum(a)
cnt = Counter(a)
q = int(sys.stdin.readline().rstrip())
for _ in range(q):
b, c = map(int, sys.stdin.readline().split())
s += (c-b)*cnt[b]
print(s)
cnt[c] += cnt[b]; cnt[b] = 0
def E():
n, *a = map(int, sys.stdin.read().split())
s = 0
for x in a: s ^= x
b = map(lambda x: x^s, a)
print(*b, sep=' ')
def F(): pass
class ABC172():
def A(): pass
def B(): pass
def C(): pass
def D(): pass
def E(): pass
def F(): pass
class ABC173():
def A():
n = int(sys.stdin.readline().rstrip())
charge = (n+999)//1000 * 1000 - n
print(charge)
def B():
n, *s = sys.stdin.read().split()
c = Counter(s)
for v in 'AC, WA, TLE, RE'.split(', '):
print(f'{v} x {c[v]}')
def C():
h, w, k = map(int, sys.stdin.readline().split())
c = [sys.stdin.readline().rstrip() for _ in range(h)]
tot = 0
for i in range(1<<h):
for j in range(1<<w):
cnt = 0
for y in range(h):
for x in range(w):
if i>>y & 1 or j>>x & 1:
continue
cnt += c[y][x] == '#'
tot += cnt == k
print(tot)
def D():
n, *a = map(int, sys.stdin.read().split())
a.sort(reverse=True)
res = a[0] + sum(a[1:1+(n-2)//2])*2 + a[1+(n-2)//2]*(n & 1)
print(res)
def E():
MOD = 10**9+7
n, k, *a = map(int, sys.stdin.read().split())
minus = [x for x in a if x < 0]
plus = [x for x in a if x > 0]
if len(plus) + len(minus)//2*2 >= k: # plus
*minus, = map(abs, minus)
minus.sort(reverse=True)
plus.sort(reverse=True)
cand = []
if len(minus)&1: minus = minus[:-1]
for i in range(0, len(minus)-1, 2):
cand.append(minus[i]*minus[i+1]%MOD)
if k & 1:
res = plus[0]
plus = plus[1:]
else:
res = 1
if len(plus)&1: plus = plus[:-1]
for i in range(0, len(plus)-1, 2):
cand.append(plus[i]*plus[i+1]%MOD)
cand.sort(reverse=True)
for x in cand[:k//2]:
res *= x
res %= MOD
print(res)
elif 0 in a:
print(0)
else:
cand = sorted(map(abs, a))
res = 1
for i in range(k):
res *= cand[i]
res %= MOD
res = MOD - res
print(res)
pass
def F(): pass
if __name__ == '__main__':
ABC042.D() | 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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s849093151 | p04046 | Accepted | import sys
import numpy as np
from heapq import heappush, heappop
from bisect import bisect_left as bi_l, bisect_right as bi_r
from collections import deque, Counter, defaultdict
from itertools import combinations, product
import string
inf = float('inf')
MOD = 10**9+7
# MOD = 998244353
class NumberTheory():
def __init__(self, n=2*10**6, numpy=True):
self.n = n
self.np_flg = numpy
self.is_prime_number, self.prime_numbers = self.sieve_of_eratosthenes(n)
def sieve_of_eratosthenes(self, n):
if self.np_flg:
sieve = np.ones(n+1, dtype=np.int64); sieve[:2] = 0
for i in range(2, int(n**.5)+1):
if sieve[i]: sieve[i*2::i] = 0
prime_numbers = np.flatnonzero(sieve)
else:
sieve = [1] * (n+1); sieve[0] = sieve[1] = 0
for i in range(2, int(n**.5)+1):
if not sieve[i]: continue
for j in range(i*2, n+1, i): sieve[j] = 0
prime_numbers = [i for i in range(2, n+1) if sieve[i]]
return sieve, prime_numbers
def prime_factorize(self, n):
res = dict()
if n < 2: return res
border = int(n**.5)
for p in self.prime_numbers:
if p > border: break
while n % p == 0: res[p] = res.get(p, 0)+1; n //= p
if n == 1: return res
res[n] = 1; return res
def prime_factorize_factorial(self, n):
res = dict()
for i in range(2, n+1):
for p, c in self.prime_factorize(i).items(): res[p] = res.get(p, 0)+c
return res
@staticmethod
def gcd(a, b): return gcd(b, a%b) if b else abs(a)
@staticmethod
def lcm(a, b): return abs(a // gcd(a, b) * b)
@staticmethod
def find_divisors(n):
divisors = []
for i in range(1, int(n**.5)+1):
if n%i: continue
divisors.append(i)
j = n // i
if j != i: divisors.append(j)
return divisors
@staticmethod
def base_convert(n, b):
if not n: return [0]
res = []
while n:
n, r = divmod(n, b)
if r < 0: n += 1; r -= b
res.append(r)
return res
class UnionFind():
def __init__(self, n=10**6):
self.root = list(range(n))
self.height = [0] * n
self.size = [1] * n
def find_root(self, u):
if self.root[u] == u: return u
self.root[u] = self.find_root(self.root[u])
return self.root[u]
def unite(self, u, v):
ru = self.find_root(u)
rv = self.find_root(v)
if ru == rv: return
hu = self.height[ru]
hv = self.height[rv]
if hu >= hv:
self.root[rv] = ru
self.size[ru] += self.size[rv]
self.height[ru] = max(hu, hv+1)
else:
self.root[ru] = rv
self.size[rv] += self.size[ru]
class Combinatorics():
def __init__(self, N=10**9, n=10**6, mod=10**9+7, numpy=True):
self.mod = mod
self.nCr = dict()
self.np_flg=numpy
self.make_mod_tables(N, n)
sys.setrecursionlimit(10**6)
def choose(self, n, r, mod=None): # no mod, or mod ≠ prime
if r > n or r < 0: return 0
if r == 0: return 1
if (n, r) in self.nCr: return self.nCr[(n, r)]
if not mod:
self.nCr[(n, r)] = (self.choose(n-1, r) + self.choose(n-1, r-1))
else:
self.nCr[(n, r)] = (self.choose(n-1, r, mod) + self.choose(n-1, r-1, mod)) % mod
return self.nCr[(n,r)]
def cumprod(self, a):
p = self.mod
l = len(a); sql = int(np.sqrt(l)+1)
a = np.resize(a, sql**2).reshape(sql, sql)
for i in range(sql-1): a[:, i+1] *= a[:, i]; a[:, i+1] %= p
for i in range(sql-1): a[i+1] *= a[i, -1]; a[i+1] %= p
return np.ravel(a)[:l]
def make_mod_tables(self, N, n):
p = self.mod
if self.np_flg:
fac = np.arange(n+1); fac[0] = 1; fac = self.cumprod(fac)
ifac = np.arange(n+1, 0, -1); ifac[0] = pow(int(fac[-1]), p-2, p)
ifac = self.cumprod(ifac)[n::-1]
n_choose = np.arange(N+1, N-n, -1); n_choose[0] = 1;
n_choose[1:] = self.cumprod(n_choose[1:])*ifac[1:n+1]%p
else:
fac = [None]*(n+1); fac[0] = 1
for i in range(n): fac[i+1] = fac[i]*(i+1)%p
ifac = [None]*(n+1); ifac[n] = pow(fac[n], p-2, p)
for i in range(n, 0, -1): ifac[i-1] = ifac[i]*i%p
n_choose = [None] * (n+1); n_choose[0] = 1
for i in range(n): n_choose[i+1] = n_choose[i]*(N-i)%p
for i in range(n+1): n_choose[i] = n_choose[i]*ifac[i]%p
self.fac, self.ifac, self.mod_n_choose = fac, ifac, n_choose
def mod_choose(self, n, r):
return self.fac[n]*self.ifac[r]%self.mod*self.ifac[n-r]%self.mod
def z_algorithm(s):
n = len(s)
a = [0] * n; a[0] = n
l = r = -1
for i in range(1, n):
if r >= i: a[i] = min(a[i-l], r-i)
while i + a[i] < n and s[i+a[i]] == s[a[i]]: a[i] += 1
if i+a[i] >= r: l, r = i, i+a[i]
return a
class ABC001():
def A():
h1, h2 = map(int, sys.stdin.read().split())
print(h1-h2)
def B(): pass
def C(): pass
def D(): pass
class ABC002():
def A():
x, y = map(int, sys.stdin.readline().split())
print(max(x, y))
def B():
vowels = set('aeiou')
s = sys.stdin.readline().rstrip()
t = ''
for c in s:
if c in vowels: continue
t += c
print(t)
def C():
*coords, = map(int, sys.stdin.readline().split())
def triangle_area(x0, y0, x1, y1, x2, y2):
x1 -= x0; x2 -= x0; y1 -= y0; y2 -= y0;
return abs(x1*y2 - x2*y1) / 2
print(triangle_area(*coords))
def D():
n, m = map(int, sys.stdin.readline().split())
edges = set()
for _ in range(m):
x, y = map(int, sys.stdin.readline().split())
x -= 1; y -= 1
edges.add((x, y))
cand = []
for i in range(1, 1<<n):
s = [j for j in range(n) if i>>j & 1]
for x, y in combinations(s, 2):
if (x, y) not in edges: break
else:
cand.append(len(s))
print(max(cand))
class ABC003():
def A():
n = int(sys.stdin.readline().rstrip())
print((n+1)*5000)
def B():
atcoder = set('atcoder')
s, t = sys.stdin.read().split()
for i in range(len(s)):
if s[i] == t[i]: continue
if s[i] == '@' and t[i] in atcoder: continue
if t[i] == '@' and s[i] in atcoder: continue
print('You will lose')
return
print('You can win')
def C():
n, k, *r = map(int, sys.stdin.read().split())
res = 0
for x in sorted(r)[-k:]:
res = (res+x) / 2
print(res)
def D(): pass
class ABC004():
def A():
print(int(sys.stdin.readline().rstrip())*2)
def B():
c = [sys.stdin.readline().rstrip() for _ in range(4)]
for l in c[::-1]:
print(l[::-1])
def C():
n = int(sys.stdin.readline().rstrip())
n %= 30
res = list(range(1, 7))
for i in range(n):
i %= 5
res[i], res[i+1] = res[i+1], res[i]
print(''.join(map(str, res)))
def D(): pass
class ABC005():
def A():
x, y = map(int, sys.stdin.readline().split())
print(y//x)
def B():
n, *t = map(int, sys.stdin.read().split())
print(min(t))
def C():
t = int(sys.stdin.readline().rstrip())
n = int(sys.stdin.readline().rstrip())
a = [int(x) for x in sys.stdin.readline().split()]
m = int(sys.stdin.readline().rstrip())
b = [int(x) for x in sys.stdin.readline().split()]
i = 0
for p in b:
if i == n: print('no'); return
while p-a[i] > t:
i += 1
if i == n: print('no'); return
if a[i] > p: print('no'); return
i += 1
print('yes')
def D():
n = int(sys.stdin.readline().rstrip())
d = np.array([sys.stdin.readline().split() for _ in range(n)], np.int64)
s = d.cumsum(axis=0).cumsum(axis=1)
s = np.pad(s, 1)
max_del = np.zeros((n+1, n+1), dtype=np.int64)
for y in range(1, n+1):
for x in range(1, n+1):
max_del[y, x] = np.amax(s[y:n+1, x:n+1] - s[0:n-y+1, x:n+1] - s[y:n+1, 0:n-x+1] + s[0:n-y+1, 0:n-x+1])
res = np.arange(n**2+1)[:, None]
i = np.arange(1, n+1)
res = max_del[i, np.minimum(res//i, n)].max(axis=1)
q = int(sys.stdin.readline().rstrip())
p = np.array(sys.stdin.read().split(), dtype=np.int64)
print(*res[p], sep='\n')
class ABC006():
def A():
n = sys.stdin.readline().rstrip()
if '3' in n: print('YES')
elif int(n)%3 == 0: print('YES')
else: print('NO')
def B():
mod = 10007
t = [0, 0, 1]
for _ in range(1001001):
t.append(t[-1]+t[-2]+t[-3]); t[-1] %= mod
n = int(sys.stdin.readline().rstrip())
print(t[n-1])
def C():
n, m = map(int, sys.stdin.readline().split())
cnt = [0, 0, 0]
if m == 1: cnt = [-1, -1, -1]
else:
if m & 1: m -= 3; cnt[1] += 1; n -= 1
cnt[2] = m//2 - n
cnt[0] = n - cnt[2]
if cnt[0]<0 or cnt[1]<0 or cnt[2]<0: print(-1, -1, -1)
else: print(*cnt, sep=' ')
def D():
n, *c = map(int, sys.stdin.read().split())
lis = [inf]*n
for x in c: lis[bi_l(lis, x)] = x
print(n - bi_l(lis, inf))
class ABC007():
def A():
n = int(sys.stdin.readline().rstrip())
print(n-1)
def B():
s = sys.stdin.readline().rstrip()
if s == 'a': print(-1)
else: print('a')
def C():
r, c = map(int, sys.stdin.readline().split())
sy, sx = map(int, sys.stdin.readline().split())
gy, gx = map(int, sys.stdin.readline().split())
sy -= 1; sx -=1; gy -= 1; gx -= 1
maze = [sys.stdin.readline().rstrip() for _ in range(r)]
queue = deque([(sy, sx)])
dist = np.full((r, c), np.inf); dist[sy, sx] = 0
while queue:
y, x = queue.popleft()
for i, j in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
i += y; j += x
if maze[i][j] == '#' or dist[i, j] != np.inf: continue
dist[i, j] = dist[y, x] + 1
queue.append((i, j))
print(int(dist[gy, gx]))
def D(): pass
class ABC008():
def A():
s, t = map(int, sys.stdin.readline().split())
print(t-s+1)
def B():
n, *s = sys.stdin.read().split()
res = defaultdict(int)
for name in s: res[name] += 1
print(sorted(res.items(), key=lambda x: x[1])[-1][0])
def C():
n, *a = map(int, sys.stdin.read().split())
a = np.array(a)
c = n - np.count_nonzero(a[:, None]%a, axis=1)
print(np.sum((c+1)//2/c))
def D(): pass
class ABC009():
def A():
n = int(sys.stdin.readline().rstrip())
print((n+1)//2)
def B():
n, *a = map(int, sys.stdin.read().split())
print(sorted(set(a))[-2])
def C():
n, k = map(int, sys.stdin.readline().split())
s = list(sys.stdin.readline().rstrip())
cost = [1]*n
r = k
for i in range(n-1):
q = []
for j in range(i+1, n):
if s[j] < s[i] and cost[i]+cost[j] <= r:
heappush(q, (s[j], cost[i]+cost[j], -j))
if not q: continue
_, c, j = heappop(q); j = -j
s[i], s[j] = s[j], s[i]
r -= c
cost[i] = cost[j] = 0
print(''.join(s))
def D(): pass
class ABC010():
def A():
print(sys.stdin.readline().rstrip()+'pp')
def B():
n, *a = map(int, sys.stdin.read().split())
tot = 0
for x in a:
c = 0
while x%2==0 or x%3==2:
x -= 1
c += 1
tot += c
print(tot)
def C():
sx, sy, gx, gy, t, v, n, *xy = map(int, sys.stdin.read().split())
x, y = np.array(xy).reshape(-1, 2).T
def dist(x1, y1, x2, y2):
return np.sqrt((x2-x1)**2 + (y2-y1)**2)
ans = 'YES' if (dist(sx, sy, x, y)+dist(x, y, gx, gy) <= v*t).any() else 'NO'
print(ans)
def D(): pass
class ABC011():
def A():
n = int(sys.stdin.readline().rstrip())
print(n%12+1)
def B():
s = sys.stdin.readline().rstrip()
print(s[0].upper()+s[1:].lower())
def C():
n, *ng = map(int, sys.stdin.read().split())
ng = set(ng)
if n in ng: print('NO')
else:
r = 100
while n > 0:
if r == 0: print('NO'); return
for i in range(3, 0, -1):
if (n-i) in ng: continue
n -= i
r -= 1
break
else: print('NO'); return
print('YES')
def D(): pass
class ABC041():
def A():
s, i = sys.stdin.read().split()
i = int(i)
print(s[i-1])
def B():
a, b, c = map(int, sys.stdin.readline().split())
ans = a * b % MOD * c % MOD
print(ans)
def C():
n, *a = map(int, sys.stdin.read().split())
for i, h in sorted(enumerate(a), key=lambda x: -x[1]):
print(i+1)
def D():
n, m, *xy = map(int, sys.stdin.read().split())
*xy, = zip(*[iter(xy)]*2)
edges = [0] * n
for x, y in xy:
x -= 1; y -= 1
edges[x] |= 1<<y
comb = [None] * (1<<n); comb[0] = 1
def count(edges, bit):
if comb[bit] is not None: return comb[bit]
comb[bit] = 0
for i in range(n):
if (bit>>i) & 1 and not edges[i]:
nxt_bit = bit & ~(1<<i)
nxt_edges = edges.copy()
for j in range(n):
nxt_edges[j] &= ~(1<<i)
cnt = count(nxt_edges, nxt_bit)
comb[bit] += cnt
return comb[bit]
print(count(edges, (1<<n)-1))
class ABC042():
def A():
a = [int(x) for x in sys.stdin.readline().split()]
c = Counter(a)
print('YES' if c[5]==2 and c[7]==1 else 'NO')
def B():
n, l, *s = sys.stdin.read().split()
print(''.join(sorted(s)))
def C():
n, k, *d = sys.stdin.read().split()
l = len(n)
ok = sorted(set(string.digits)-set(d))
cand = [int(''.join(p)) for p in product(ok, repeat=l)] + [int(min(x for x in ok if x > '0')+min(ok)*l)]
print(cand[bi_l(cand, int(n))])
def D(): pass
h, w, a, b = map(int, sys.stdin.read().split())
combinatorics = Combinatorics(mod=MOD)
tot = combinatorics.mod_choose(h+w-2, h-1)
for i in range(h-a, h):
tot -= combinatorics.mod_choose(i+b-1, i) * combinatorics.mod_choose(h-i+w-b-2, h-1-i)
tot %= MOD
print(tot)
class ABC170():
def A():
x = [int(x) for x in sys.stdin.readline().split()]
for i in range(5):
if x[i] != i+1:
print(i+1)
break
def B():
x, y = map(int, sys.stdin.readline().split())
print('Yes' if 2*x <= y <= 4*x and y%2 == 0 else 'No')
def C():
x, n, *p = map(int, sys.stdin.read().split())
a = list(set(range(102)) - set(p))
a = [(abs(y-x), y) for y in a]
print(sorted(a)[0][1])
def D():
n, *a = map(int, sys.stdin.read().split())
cand = set(a)
cnt = 0
for x, c in sorted(Counter(a).items()):
cnt += c == 1 and x in cand
cand -= set(range(x*2, 10**6+1, x))
print(cnt)
def E():
n, q = map(int, sys.stdin.readline().split())
queue = []
num_kindergarten = 2*10**5
queue_kindergarten = [[] for _ in range(num_kindergarten)]
highest_kindergarten = [None] * num_kindergarten
where = [None] * n
rate = [None] * n
def entry(i, k):
where[i] = k
while queue_kindergarten[k]:
r, j = heappop(queue_kindergarten[k])
if where[j] != k or j == i: continue
if rate[i] >= -r:
highest_kindergarten[k] = rate[i]
heappush(queue, (rate[i], k, i))
heappush(queue_kindergarten[k], (r, j))
break
else:
highest_kindergarten[k] = rate[i]
heappush(queue, (rate[i], k, i))
heappush(queue_kindergarten[k], (-rate[i], i))
def transfer(i, k):
now = where[i]
while queue_kindergarten[now]:
r, j = heappop(queue_kindergarten[now])
if where[j] != now or j == i: continue
if highest_kindergarten[now] != -r:
highest_kindergarten[now] = -r
heappush(queue, (-r, now, j))
heappush(queue_kindergarten[now], (r, j))
break
else:
highest_kindergarten[now] = None
entry(i, k)
def inquire():
while True:
r, k, i = heappop(queue)
if where[i] != k or r != highest_kindergarten[k]: continue
heappush(queue, (r, k, i))
return r
for i in range(n):
a, b = map(int, sys.stdin.readline().split())
rate[i] = a
entry(i, b-1)
for _ in range(q):
c, d = map(int, sys.stdin.readline().split())
transfer(c-1, d-1)
print(inquire())
def F(): pass
class ABC171():
def A():
c = sys.stdin.readline().rstrip()
print('A' if c < 'a' else 'a')
def B():
n, k, *p = map(int, sys.stdin.read().split())
print(sum(sorted(p)[:k]))
def C():
n = int(sys.stdin.readline().rstrip())
n -= 1
l = 1
while True:
if n < pow(26, l):
break
n -= pow(26, l)
l += 1
res = ''.join([chr(ord('a')+d%26) for d in NumberTheory.base_convert(n, 26)][::-1])
res = 'a'*(l-len(res)) + res
print(res)
def D():
n = int(sys.stdin.readline().rstrip())
a = [int(x) for x in sys.stdin.readline().split()]
s = sum(a)
cnt = Counter(a)
q = int(sys.stdin.readline().rstrip())
for _ in range(q):
b, c = map(int, sys.stdin.readline().split())
s += (c-b)*cnt[b]
print(s)
cnt[c] += cnt[b]; cnt[b] = 0
def E():
n, *a = map(int, sys.stdin.read().split())
s = 0
for x in a: s ^= x
b = map(lambda x: x^s, a)
print(*b, sep=' ')
def F(): pass
class ABC172():
def A(): pass
def B(): pass
def C(): pass
def D(): pass
def E(): pass
def F(): pass
class ABC173():
def A():
n = int(sys.stdin.readline().rstrip())
charge = (n+999)//1000 * 1000 - n
print(charge)
def B():
n, *s = sys.stdin.read().split()
c = Counter(s)
for v in 'AC, WA, TLE, RE'.split(', '):
print(f'{v} x {c[v]}')
def C():
h, w, k = map(int, sys.stdin.readline().split())
c = [sys.stdin.readline().rstrip() for _ in range(h)]
tot = 0
for i in range(1<<h):
for j in range(1<<w):
cnt = 0
for y in range(h):
for x in range(w):
if i>>y & 1 or j>>x & 1:
continue
cnt += c[y][x] == '#'
tot += cnt == k
print(tot)
def D():
n, *a = map(int, sys.stdin.read().split())
a.sort(reverse=True)
res = a[0] + sum(a[1:1+(n-2)//2])*2 + a[1+(n-2)//2]*(n & 1)
print(res)
def E():
MOD = 10**9+7
n, k, *a = map(int, sys.stdin.read().split())
minus = [x for x in a if x < 0]
plus = [x for x in a if x > 0]
if len(plus) + len(minus)//2*2 >= k: # plus
*minus, = map(abs, minus)
minus.sort(reverse=True)
plus.sort(reverse=True)
cand = []
if len(minus)&1: minus = minus[:-1]
for i in range(0, len(minus)-1, 2):
cand.append(minus[i]*minus[i+1]%MOD)
if k & 1:
res = plus[0]
plus = plus[1:]
else:
res = 1
if len(plus)&1: plus = plus[:-1]
for i in range(0, len(plus)-1, 2):
cand.append(plus[i]*plus[i+1]%MOD)
cand.sort(reverse=True)
for x in cand[:k//2]:
res *= x
res %= MOD
print(res)
elif 0 in a:
print(0)
else:
cand = sorted(map(abs, a))
res = 1
for i in range(k):
res *= cand[i]
res %= MOD
res = MOD - res
print(res)
pass
def F(): pass
if __name__ == '__main__':
ABC042.D() | 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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s148732450 | p04046 | Accepted | h,w,a,b=map(int,input().split())
fac=[0]*200001#iの階乗mod(1000000007)
inv=[0]*200001#iの逆元mod(1000000007)
fac[0]=1
ans=0
for i in range(1,200001):
fac[i]=fac[i-1]*i%1000000007
inv[200000]=pow(fac[200000],1000000005,1000000007)
for i in range(199999,0,-1):
inv[i]=(inv[i+1]*(i+1))%1000000007
inv[0]=1
for i in range(h-a):
if i==0:
if h==1:
x=1
else:
x=(fac[w-b+h-2
-i]*inv[w-1-b]*inv[h-1-i])%1000000007
else:
x=((fac[b-1+i]*inv[b-1]*inv[i])%1000000007)*((fac[w-b+h-2-i]*inv[w-b-1]*inv[h-1-i])%1000000007)
ans=(ans+x)%1000000007
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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s455176771 | p04046 | Accepted | import sys
from functools import lru_cache
sys.setrecursionlimit(10**9)
def mul(x, y, mod):
x %= mod
y %= mod
return x * y % mod
def div(x, y, mod):
x %= mod
y %= mod
return x * mod_pow(y, mod - 2, mod) % mod
@lru_cache(maxsize=None)
def mod_pow(a, p, mod):
if p == 0:
return 1
if p % 2 == 0:
root = mod_pow(a, p / 2, mod)
return root * root % mod
else:
return a * mod_pow(a, p - 1, mod) % mod
def dnm(n, mod, factorials):
denominators = [0] * (n + 1)
denominators[n] = div(1, factorials, mod)
for i in reversed(range(n)):
denominators[i] = mul(denominators[i+1], i+1, mod)
return denominators
def nmr(n, mod):
factorials = [0] * (n + 1)
factorials[0] = 1
for i in range(n):
factorials[i + 1] = (i + 1) * factorials[i] % mod
return factorials
def cmb(a, b, mod, factorials, factorial_divs):
if len(factorials) == 0:
raise
af = factorials[a]
bf = factorial_divs[b]
abf = factorial_divs[a - b]
return mul(mul(af, bf, mod), abf, mod)
MOD=10**9+7
H,W,A,B=map(int,input().split())
ans = 0
n = nmr(H+W-2, MOD)
d = dnm(H+W-2, MOD, n[H+W-2])
for i in range(H-A):
x = cmb(B-1+i, B-1, MOD, n, d)
y = cmb(H+W-3-(B-1+i), W-B-1, MOD, n, d)
ans += x * y % 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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s177944718 | p04046 | Accepted | import sys
H,W,A,B = map(int,sys.stdin.readline().rstrip().split())
mod = 10**9+7
kaijou = [1]
for i in range(1,H+W-1):
kaijou.append((kaijou[-1]*i) % mod)
a = 0
for i in range(B):
b = kaijou[(H-A-1)+i]*pow(kaijou[i],mod-2,mod)*pow(kaijou[H-A-1],mod-2,mod)
b %= mod
b *= kaijou[(W+A-2)-i]*pow(kaijou[A-1],mod-2,mod)*pow(kaijou[W-1-i],mod-2,mod)
b %= mod
a += b
a %= mod
c = kaijou[H+W-2]*pow(kaijou[H-1],mod-2,mod)*pow(kaijou[W-1],mod-2,mod)
c %= mod
print((c-a) % 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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s295710462 | p04046 | Accepted | #!/usr/bin/python
# -*- coding: UTF-8 -*-
import sys
def get_ints():
return map(int, sys.stdin.readline().strip().split())
def mul(x, y, mod):
x %= mod
y %= mod
return x * y % mod
def div(x, y, mod):
x %= mod
y %= mod
return x * mod_pow(y, mod - 2, mod) % mod
def mod_pow(a, p, mod):
if p == 0:
return 1
if p % 2 == 0:
root = mod_pow(a, p / 2, mod)
return root * root % mod
else:
return a * mod_pow(a, p - 1, mod) % mod
def pre_factorial_divs(n, mod, factorials):
factorial_divs = [0] * (n + 1)
factorial_divs[n] = div(1, factorials, mod)
for i in reversed(range(n)):
factorial_divs[i] = mul(factorial_divs[i+1], i+1, mod)
return factorial_divs
def pre_factorials(n, mod):
factorials = [0] * (n + 1)
factorials[0] = 1
for i in range(n):
factorials[i + 1] = (i + 1) * factorials[i] % mod
return factorials
def fast_fast_fermat_comb(a, b, mod, factorials, factorial_divs):
if len(factorials) == 0:
raise
af = factorials[a]
bf = factorial_divs[b]
abf = factorial_divs[a - b]
return mul(mul(af, bf, mod), abf, mod)
def main():
h, w, a, b = get_ints()
ans = 0
mod = 10 ** 9 + 7
factorials = pre_factorials(h+w-2, mod)
factorial_divs = pre_factorial_divs(h+w-2, mod, factorials[h+w-2])
for i in range(h-a):
x = fast_fast_fermat_comb(b-1+i, b-1, mod, factorials, factorial_divs)
y = fast_fast_fermat_comb(h+w-3-(b-1+i), w-b-1, mod, factorials, factorial_divs)
ans += x * y % 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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s563161959 | p04046 | Accepted | h,w,a,b=map(int,input().split())
p=10**9+7
#p=127
def modp_factorial(n):
s=1
for x in range(1,h+1):
s=(s*x) % p
return s
def modp_prod(lst):
s=1
for x in lst:
s=(s*x)%p
return s
def inv(n):
s=1
q=p-2
while q>0:
if q&1:
s=(s*n) % p
n=(n*n) % p
q>>=1
return s
l=[1]
f=1
for x in range(1,h+w):
f=(f*x) % p
l.append(f)
invl=[inv(l[-1])]
for n in range(h+w-1,1,-1):
invl.append((invl[-1]*n) % p)
invl.append(1)
invl.reverse()
s=0
for x in range(1,h-a+1):
s=(s+modp_prod([l[x+b-2],invl[x-1],invl[b-1]\
,l[w-b+h-x-1],invl[h-x],invl[w-b-1]])) % p
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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s083985714 | p04046 | Accepted | h,w,a,b = map(int,input().split())
mod = 10**9+7
MAX = 2*10**5
fact = [1]*(MAX+1)
for i in range(1, MAX+1):
fact[i] = (fact[i-1]*i) % mod
inv = [1]*(MAX+1)
for i in range(2, MAX+1):
inv[i] = inv[mod % i]*(mod-mod//i) % mod
fact_inv = [1]*(MAX+1)
for i in range(1, MAX+1):
fact_inv[i] = fact_inv[i-1] * inv[i] % mod
def comb(n, k):
if n < k:
return 0
return fact[n] * fact_inv[n-k] * fact_inv[k] % mod
ans = 0
for k in range(b+1,w+1):
ans += comb(k+h-a-2,h-a-1)*comb(w-k+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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s126996691 | p04046 | Accepted | 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 = 3 * 10 ** 5 # 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) % p)
inv.append((-inv[p % i] * (p // i)) % p)
factinv.append((factinv[-1] * inv[-1]) % p)
if __name__ == "__main__":
H, W, A, B = map(int, input().split())
#HW = [[0] * W for i in range(H)]
p = 10 ** 9 + 7
#print(HW)
#visited = [[0] * W for i in range(H)]
ans = 0
for i in range(H - A):
if i == 0:
ans += cmb(B + i, i, p) * cmb(H + W - B - 2 - i, H - 1 - i, p)
ans %= p
else:
ans += cmb(B + i - 1, i, p) * cmb(H + W - B - 2 - i, H - 1 - i, p)
ans %= p
print(ans)
| 2 3 1 1
| 2
| <span class="lang-en">
<p>Score : <var>400</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns.
Iroha is now standing in the top-left cell.
She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p>
<p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p>
<p>Find the number of ways she can travel to the bottom-right cell.</p>
<p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var> 1 ≦ H, W ≦ 100,000</var></li>
<li><var> 1 ≦ A < H</var></li>
<li><var> 1 ≦ B < 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> |
s806747189 | p04046 | Accepted | import math
h, w, a, b = [int(i) for i in input().split()]
MOD = 10**9 + 7
N = h + w
fact = [1]*(N+1)
rfact = [1]*(N+1)
r = 1
for i in range(1, N+1):
fact[i] = r = r * i % MOD
rfact[N] = r = pow(fact[N], MOD-2, MOD)
for i in range(N, 0, -1):
rfact[i-1] = r = r * i % MOD
def comb(n, k):
return fact[n] * rfact[k] * rfact[n-k] % MOD
tmp = []
for i in range(b, w):
tmp.append(comb(i+h-a-1, i))
ans = 0
cnt = 0
for i in range(w-b-1, -1, -1):
t = comb(i + a - 1, a - 1)
ans += t * tmp[cnt]
cnt += 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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s062565704 | p04046 | Accepted | h, w, a, b = map(int, input().split())
modulo = 10 ** 9 + 7
factorials = [1]
for i in range(1, h + w + 1):
factorials.append((factorials[-1] * i) % modulo)
inverses = []
for f in factorials:
inverses.append(pow(f, modulo - 2, modulo))
def cnk(n, k):
return (factorials[n] * inverses[k] * inverses[n - k]) % modulo
line_before = [0] * w
for j in range(b, w):
line_before[j] = cnk(h - a - 1 + j, h - a - 1)
s = 0
for j in range(b, w):
s += cnk(a + w - j - 2, a - 1) * line_before[j]
s %= modulo
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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s260437334 | p04046 | Accepted | import sys
input = sys.stdin.readline
H,W,A,B= map(int,input().split())
MOD = 10**9 + 7
num = 200010
fact = [1] * (num+1)
ifact = [1] * (num+1)
for i in range(1,num+1):
fact[i] = (fact[i-1] * i) % MOD
ifact[-1] = pow(fact[-1],MOD-2,MOD)
for i in range(1,num+1)[::-1]:
ifact[i-1] = (ifact[i] * i)% MOD
def nCr(n,r):
if r > n:
return 0
return (fact[n] * ifact[r] * ifact[n-r]) % MOD
ans = 0
for i in range(B+1,W+1):
ans += nCr((i-1)+(H-A-1),i-1) * nCr((A-1)+(W-i),A-1)
ans %= MOD
print(ans)
| 2 3 1 1
| 2
| <span class="lang-en">
<p>Score : <var>400</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns.
Iroha is now standing in the top-left cell.
She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p>
<p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p>
<p>Find the number of ways she can travel to the bottom-right cell.</p>
<p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var> 1 ≦ H, W ≦ 100,000</var></li>
<li><var> 1 ≦ A < H</var></li>
<li><var> 1 ≦ B < 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> |
s967868214 | p04046 | Accepted | H, W, A, B = map(int, input().split())
C = H - A
D = W - B
p = 1000000007
def power(a, b): #a^b (mod p)を求める #二分累乗法を使う
if b == 0:
return 1
elif b % 2 == 0:
return power(a, b//2) ** 2 % p
else:
return power(a, b//2) ** 2 * a % p
f = [1] #f[i] = i! % p
for i in range(H+W):
f.append(f[i] * (i+1) % p)
I_f = [0] * (H+W+1) #I_f[i] = (i!)^(-1) % p
I_f[H+W] = power(f[H+W], p-2)
for i in reversed(range(H+W)):
I_f[i] = I_f[i+1] * (i+1) % p
def combi(a, b): #(a+b)!/a!b! (mod p)を求める
return f[a+b] * I_f[a] * I_f[b] % p
x = 0
for i in range(C):
x = (x + combi(i, B-1) * combi(D-1, H-i-1)) % p
print(x)
| 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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s827275144 | p04046 | Accepted | import sys
readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)
def comb_mod(n, r):
res = 1
r = min(n - r, r)
for i in range(r):
res *= (n - i)
res %= MOD
res *= pow((r - i), MOD - 2, MOD)
return res
def main():
h, w, a, b = map(int, readline().split())
ans = 0
comb1 = 1
comb2 = comb_mod(w - b - 1 + h - 1, w - b - 1)
for i in range(h - a):
ans += comb1 * comb2
ans %= MOD
comb1 *= (b - 1 + i + 1)
comb1 %= MOD
comb1 *= pow(i + 1, MOD - 2, MOD)
comb1 %= MOD
comb2 *= pow(w - 1 - b + h - 1 - i, MOD - 2, MOD)
comb2 %= MOD
comb2 *= w - 1 - b + h- 1 - i - (w - b - 1)
comb2 %= 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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s169659581 | p04046 | Accepted |
def read_int():
return int(input().strip())
def read_ints():
return list(map(int, input().strip().split(' ')))
factorial = [1]
modulo = 10**9+7
inverse_factorial = [0]
def pow1(a, p):
if p == 0:
return 1
half = pow1(a, p//2)
total = (half*half)%modulo
if p%2 == 0:
return total
return (total*a)%modulo
def modpow(a, p):
ans = 1
while p:
if p&1 == 1:
ans = (ans*a)%modulo
a = (a*a)%modulo
p >>= 1
return ans
def nCr(n, r):
if r == 0 or r == n:
return 1
return (((factorial[n]*inverse_factorial[n-r])%modulo)*inverse_factorial[r])%modulo
def solve():
"""
2 3 1 1
...
x.. A
B
C(2, 1)*C(3, 2)
m 0 n 1 C(m, n) 1
m 1 n 2 C(m, n) 2
m 0 n 2 C(m, n) 4
m 1 n 3 C(m, n) 12
"""
H, W, A, B = read_ints()
for i in range(1, H+W):
f = (factorial[-1]*i)%modulo
factorial.append(f)
inv_f = modpow(f, modulo-2)
inverse_factorial.append(inv_f)
T = 0
for d in range(B, W):
# d = 0, 1
y0 = d
x0 = H-A-1
y1 = W-d-1
x1 = A-1
T = (T+(nCr(x0+y0, x0)*nCr(x1+y1, x1))%modulo)%modulo
return T
if __name__ == '__main__':
print(solve())
| 2 3 1 1
| 2
| <span class="lang-en">
<p>Score : <var>400</var> points</p>
<div class="part">
<section>
<h3>Problem Statement</h3><p>We have a large square grid with <var>H</var> rows and <var>W</var> columns.
Iroha is now standing in the top-left cell.
She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.</p>
<p>However, she cannot enter the cells in the intersection of the bottom <var>A</var> rows and the leftmost <var>B</var> columns. (That is, there are <var>A×B</var> forbidden cells.) There is no restriction on entering the other cells.</p>
<p>Find the number of ways she can travel to the bottom-right cell.</p>
<p>Since this number can be extremely large, print the number modulo <var>10^9+7</var>.</p>
</section>
</div>
<div class="part">
<section>
<h3>Constraints</h3><ul>
<li><var> 1 ≦ H, W ≦ 100,000</var></li>
<li><var> 1 ≦ A < H</var></li>
<li><var> 1 ≦ B < 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> |
s436950929 | p04046 | Accepted | H, W, A, B = map(int,input().split())
mod = 10**9+7
C = [1,1]
inv = [0,1]
Cinv = [1,1]
for i in range(2,H+W+1):
C.append((C[-1]*i)%mod)
inv.append((-inv[mod%i]*(mod//i)%mod))
Cinv.append(Cinv[-1]*inv[-1]%mod)
ans = 0
for i in range(H-A):
a = (C[i+B-1]*C[H-1-i+W-B-1])%mod
a = (a*Cinv[B-1])%mod
a = (a*Cinv[i])%mod
a = (a*Cinv[W-B-1])%mod
a = (a*Cinv[H-i-1])%mod
ans += a
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 < H</var></li>
<li><var> 1 ≦ B < 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> |
s505323631 | p04046 | Accepted | import sys
import numpy as np
import random
from decimal import Decimal
import itertools
import re
import bisect
from collections import deque, Counter
from functools import lru_cache
sys.setrecursionlimit(10**9)
INF = 10**13
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
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)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
def SERIES(n): return np.fromstring(sys.stdin.buffer.read(), dtype=np.int32, sep=' ')
def GRID(h,w): return np.fromstring(sys.stdin.buffer.read(), dtype=np.int32, sep=' ').reshape(h,-1)[:,:w]
def GRIDfromString(h,w): return np.frombuffer(sys.stdin.buffer.read(), 'S1').reshape(h,-1)[:,:w]
MOD = 1000000007
def main():
h, w, a, b = LI()
def comb(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 ** 5 * 2
fact = [1, 1]
factinv = [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)
factinv.append((factinv[-1]) * inv[-1] % p)
ans = 0
for i in range(1,w-b+1):
ans += comb(h-a+b+i-2, max(h-a-1, b+i-1), MOD) * comb(a+w-b-i-1, max(a-1, w-b-i), 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 < H</var></li>
<li><var> 1 ≦ B < 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> |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.