結果

問題 No.1785 Inequality Signs
ユーザー lam6er
提出日時 2025-03-20 18:49:32
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 209 ms / 2,000 ms
コード長 1,343 bytes
コンパイル時間 145 ms
コンパイル使用メモリ 82,328 KB
実行使用メモリ 70,340 KB
最終ジャッジ日時 2025-03-20 18:51:06
合計ジャッジ時間 7,795 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 52
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 10**9 + 7

n, K = map(int, input().split())

M = min(n - 1, K - 1)
if M < 0:
    print(0)
    exit()

# Precompute inverses for 1..M+2
max_inv = M + 2
inv = [1] * (max_inv + 1)
for i in range(2, max_inv + 1):
    inv[i] = pow(i, MOD - 2, MOD)

# Precompute factorial and inv factorial for combinations(n-1, m)
max_fact = max(n - 1, M)
fact = [1] * (max_fact + 1)
for i in range(1, max_fact + 1):
    fact[i] = fact[i-1] * i % MOD

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

# Precompute powers of 2 up to M
pow2 = [1] * (M + 1)
for i in range(1, M + 1):
    pow2[i] = pow2[i-1] * 2 % MOD

sum_ans = 0

if M < 0:
    print(0)
    exit()

comb_k = K % MOD  # C(K, 1)
sum_ans = 0
for m in range(M + 1):
    # Compute combination(n-1, m)
    if m > n - 1:
        comb_n = 0
    else:
        comb_n = fact[n-1] * inv_fact[m] % MOD
        comb_n = comb_n * inv_fact[n-1 - m] % MOD
    
    term = comb_k * comb_n % MOD
    term = term * pow2[m] % MOD
    sum_ans = (sum_ans + term) % MOD
    
    # Update comb_k for next m (m+1)
    new_m = m + 1
    if new_m + 1 > K:
        comb_k = 0
    else:
        comb_k = comb_k * (K - new_m) % MOD
        comb_k = comb_k * inv[new_m + 1] % MOD

print(sum_ans % MOD)
0