結果

問題 No.1846 Good Binary Matrix
ユーザー lam6er
提出日時 2025-03-20 18:44:39
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 587 ms / 2,000 ms
コード長 898 bytes
コンパイル時間 206 ms
コンパイル使用メモリ 82,768 KB
実行使用メモリ 86,680 KB
最終ジャッジ日時 2025-03-20 18:44:52
合計ジャッジ時間 8,116 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 10**9 + 7

H, W = map(int, input().split())

max_n = H
fact = [1] * (max_n + 1)
for i in range(2, max_n + 1):
    fact[i] = fact[i-1] * i % MOD

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

# Precompute pow2 up to H
pow2 = [1] * (H + 1)
for k in range(1, H + 1):
    pow2[k] = (pow2[k-1] * 2) % MOD

ans = 0
for i in range(H + 1):
    k = H - i
    base = (pow2[k] - 1) % MOD
    # Compute base^W mod MOD
    term_base = pow(base, W, MOD)
    # Compute combination C(H, i)
    comb = fact[H] * inv_fact[i] % MOD
    comb = comb * inv_fact[H - i] % MOD
    # Compute sign (-1)^i
    if i % 2 == 1:
        sign = MOD - 1
    else:
        sign = 1
    # Compute term
    term = (sign * comb) % MOD
    term = (term * term_base) % MOD
    ans = (ans + term) % MOD

print(ans)
0