結果

問題 No.389 ロジックパズルの組み合わせ
ユーザー rpy3cpprpy3cpp
提出日時 2016-07-10 18:12:59
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 109 ms / 2,000 ms
コード長 1,123 bytes
コンパイル時間 167 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 18,648 KB
最終ジャッジ日時 2024-04-21 12:03:41
合計ジャッジ時間 6,635 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 26 ms
10,752 KB
testcase_01 AC 28 ms
10,752 KB
testcase_02 AC 26 ms
10,752 KB
testcase_03 AC 26 ms
10,752 KB
testcase_04 AC 26 ms
10,752 KB
testcase_05 AC 25 ms
10,752 KB
testcase_06 AC 24 ms
10,624 KB
testcase_07 AC 25 ms
10,624 KB
testcase_08 AC 25 ms
10,752 KB
testcase_09 AC 43 ms
11,392 KB
testcase_10 AC 36 ms
11,392 KB
testcase_11 AC 47 ms
11,852 KB
testcase_12 AC 31 ms
11,264 KB
testcase_13 AC 58 ms
13,712 KB
testcase_14 AC 40 ms
11,756 KB
testcase_15 AC 32 ms
11,136 KB
testcase_16 AC 36 ms
11,264 KB
testcase_17 AC 45 ms
11,648 KB
testcase_18 AC 85 ms
16,348 KB
testcase_19 AC 25 ms
10,752 KB
testcase_20 AC 25 ms
10,624 KB
testcase_21 AC 24 ms
10,752 KB
testcase_22 AC 25 ms
10,624 KB
testcase_23 AC 26 ms
10,624 KB
testcase_24 AC 24 ms
10,752 KB
testcase_25 AC 25 ms
10,624 KB
testcase_26 AC 24 ms
10,624 KB
testcase_27 AC 24 ms
10,752 KB
testcase_28 AC 25 ms
10,752 KB
testcase_29 AC 26 ms
10,624 KB
testcase_30 AC 27 ms
10,752 KB
testcase_31 AC 25 ms
10,624 KB
testcase_32 AC 26 ms
10,752 KB
testcase_33 AC 24 ms
10,752 KB
testcase_34 AC 25 ms
10,624 KB
testcase_35 AC 26 ms
10,624 KB
testcase_36 AC 27 ms
10,752 KB
testcase_37 AC 29 ms
10,624 KB
testcase_38 AC 25 ms
10,752 KB
testcase_39 AC 25 ms
10,624 KB
testcase_40 AC 25 ms
10,624 KB
testcase_41 AC 25 ms
10,624 KB
testcase_42 AC 24 ms
10,624 KB
testcase_43 AC 24 ms
10,752 KB
testcase_44 AC 24 ms
10,752 KB
testcase_45 AC 25 ms
10,752 KB
testcase_46 AC 25 ms
10,624 KB
testcase_47 AC 25 ms
10,752 KB
testcase_48 AC 25 ms
10,624 KB
testcase_49 AC 90 ms
16,508 KB
testcase_50 AC 33 ms
11,648 KB
testcase_51 AC 32 ms
11,136 KB
testcase_52 AC 42 ms
14,144 KB
testcase_53 AC 33 ms
11,392 KB
testcase_54 AC 43 ms
14,848 KB
testcase_55 AC 25 ms
10,752 KB
testcase_56 AC 30 ms
11,136 KB
testcase_57 AC 52 ms
13,592 KB
testcase_58 AC 75 ms
15,388 KB
testcase_59 AC 63 ms
14,012 KB
testcase_60 AC 43 ms
12,268 KB
testcase_61 AC 109 ms
18,648 KB
testcase_62 AC 44 ms
12,416 KB
testcase_63 AC 77 ms
15,868 KB
testcase_64 AC 40 ms
14,720 KB
testcase_65 AC 45 ms
11,512 KB
testcase_66 AC 51 ms
12,088 KB
testcase_67 AC 43 ms
12,376 KB
testcase_68 AC 43 ms
14,716 KB
testcase_69 AC 25 ms
10,752 KB
testcase_70 AC 24 ms
10,752 KB
testcase_71 AC 26 ms
10,752 KB
testcase_72 AC 25 ms
10,624 KB
testcase_73 AC 26 ms
10,752 KB
testcase_74 AC 26 ms
10,624 KB
testcase_75 AC 27 ms
10,752 KB
testcase_76 AC 27 ms
10,624 KB
testcase_77 AC 27 ms
10,752 KB
testcase_78 AC 27 ms
10,624 KB
testcase_79 AC 26 ms
10,624 KB
testcase_80 AC 29 ms
10,624 KB
testcase_81 AC 29 ms
10,752 KB
testcase_82 AC 27 ms
10,752 KB
testcase_83 AC 26 ms
10,624 KB
testcase_84 AC 26 ms
10,624 KB
testcase_85 AC 25 ms
10,752 KB
testcase_86 AC 25 ms
10,752 KB
testcase_87 AC 26 ms
10,752 KB
testcase_88 AC 26 ms
10,752 KB
testcase_89 AC 47 ms
12,668 KB
testcase_90 AC 60 ms
14,060 KB
testcase_91 AC 86 ms
17,084 KB
testcase_92 AC 35 ms
11,648 KB
testcase_93 AC 66 ms
14,684 KB
testcase_94 AC 28 ms
10,880 KB
testcase_95 AC 70 ms
14,032 KB
testcase_96 AC 57 ms
14,188 KB
testcase_97 AC 57 ms
13,412 KB
testcase_98 AC 45 ms
14,336 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

M = int(input())
Hs = list(map(int, input().split()))


def inv(a, m):
    ''' a * b = 1 (mod m) となる b を返す。 m は素数とする。
    b = a**(m-2) (mod m) となることが知られており、これを使って計算している。
    '''
    n = m - 2
    b = bin(n)[2:][-1::-1]
    ret = 1
    tmp = a
    if b[0] == '1':
        ret = a
    for bi in b[1:]:
        tmp *= tmp
        tmp %= m
        if bi == '1':
            ret *= tmp
            ret %= m
    return ret


def nCb_mod_p(n, b, mod):
    ''' nCb % mod を返す。 mod が素数で、mod > n のときのみ有効。
    '''
    if b > n - b:
        b = n - b
    num = 1
    for k in range(n, n-b, -1):
        num *= k
        num %= mod
    den = 1
    for k in range(1, b+1):
        den *= k
        den %= mod
    r = inv(den, mod)
    return (num * r) % mod


def solve(M, Hs):
    if sum(Hs) == 0:
        return 1
    mod = 10**9 + 7
    n = len(Hs)
    minM = sum(Hs) + n - 1
    if M < minM:
        return 'NA'
    if M == minM:
        return 1
    r = M - minM
    return nCb_mod_p(n + r, r, mod)

print(solve(M, Hs))
0