結果

問題 No.767 配られたジャパリまん
ユーザー nephrologistnephrologist
提出日時 2022-03-23 18:06:08
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,677 bytes
コンパイル時間 184 ms
コンパイル使用メモリ 82,464 KB
実行使用メモリ 111,208 KB
最終ジャッジ日時 2024-04-20 02:22:24
合計ジャッジ時間 6,316 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
58,112 KB
testcase_01 AC 37 ms
52,480 KB
testcase_02 AC 35 ms
52,608 KB
testcase_03 AC 36 ms
52,736 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 34 ms
52,224 KB
testcase_07 AC 35 ms
52,480 KB
testcase_08 AC 38 ms
58,752 KB
testcase_09 AC 67 ms
69,120 KB
testcase_10 AC 216 ms
78,984 KB
testcase_11 AC 72 ms
73,984 KB
testcase_12 AC 69 ms
73,088 KB
testcase_13 AC 216 ms
78,848 KB
testcase_14 AC 63 ms
69,888 KB
testcase_15 AC 47 ms
63,232 KB
testcase_16 AC 70 ms
73,472 KB
testcase_17 AC 44 ms
59,776 KB
testcase_18 AC 45 ms
62,336 KB
testcase_19 AC 75 ms
74,496 KB
testcase_20 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

# 問題の首座として、通れない、としたときにどうなるか、を高速で求めたい。
# 通れないは難しいから、通るものを数えて、全体から引く
# 全体から引くときに、各々引いて、,,,とやるとかぶるから包除原理
import sys

input = sys.stdin.buffer.readline
mod = 10 ** 8 + 7
h, w, k = map(int, input().split())

AB = [list(map(int, input().split())) for _ in range(k)]


N = h + w + 100 + 5
bikkuri = [0] * N
bikkuri[0] = 1
gyaku = [0] * N
for i in range(1, N):
    bikkuri[i] = (i * bikkuri[i - 1]) % mod
gyaku[N - 1] = pow(bikkuri[N - 1], mod - 2, mod)
for i in range(N)[::-1]:
    gyaku[i - 1] = (gyaku[i] * i) % mod


def comb(n, r):
    if r < 0 or n - r < 0:
        return 0
    return bikkuri[n] * gyaku[r] % mod * gyaku[n - r] % mod


popcount = [0] * (1 << k)

for i in range(1 << k):
    popcount[i] = popcount[i // 2] + i % 2


memo = [0] * (1 << k)

for i in range(1 << k):
    temp = 1
    px, py = 0, 0
    for j in range(k):
        if (i >> j) & 1:
            nx, ny = AB[j]
            dx = nx - px
            dy = ny - py
            temp *= comb(dx + dy, dy)
            temp %= mod
            px, py = nx, ny
    nx, ny = h, w

    dx = nx - px
    dy = ny - py

    temp *= comb(dx + dy, dy)
    temp %= mod
    memo[i] = temp

total = comb(h + w, w)


ans = [0] * (1 << k)

for i in range(1 << k):
    j = i
    while j:
        # print("i,j", i, j)
        keisu = 1 if popcount[j] % 2 else -1
        ans[i] += keisu * memo[j]
        ans[i] %= mod
        # print(keisu * memo[j])
        j -= 1
        j &= i

ans = [total - a for a in ans]

for a in ans:
    print(a % mod)
0