結果

問題 No.389 ロジックパズルの組み合わせ
ユーザー lam6er
提出日時 2025-03-20 18:49:51
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 151 ms / 2,000 ms
コード長 1,045 bytes
コンパイル時間 177 ms
コンパイル使用メモリ 82,696 KB
実行使用メモリ 152,296 KB
最終ジャッジ日時 2025-03-20 18:52:21
合計ジャッジ時間 9,768 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 99
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 10**9 + 7
max_fact = 10**6  # Since M can be up to 1e6, and n = D +k can be up to 1e6

# Precompute factorial and inverse factorial modulo MOD
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

def main():
    import sys
    input = sys.stdin.read().split()
    M = int(input[0])
    H = list(map(int, input[1:]))
    
    if len(H) == 1 and H[0] == 0:
        print(1)
        return
    
    if any(h <= 0 for h in H):
        print("NA")
        return
    
    sum_h = sum(H)
    k = len(H)
    required = sum_h + (k - 1)
    if required > M:
        print("NA")
        return
    
    D = M - required
    n = D + k
    if n > max_fact:
        print("NA")
        return
    
    comb = fact[n] * inv_fact[k] % MOD
    comb = comb * inv_fact[n - k] % MOD
    print(comb)
    
if __name__ == "__main__":
    main()
0