結果

問題 No.389 ロジックパズルの組み合わせ
ユーザー rpy3cpprpy3cpp
提出日時 2016-07-10 18:12:59
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 96 ms / 2,000 ms
コード長 1,123 bytes
コンパイル時間 275 ms
コンパイル使用メモリ 10,856 KB
実行使用メモリ 15,864 KB
最終ジャッジ日時 2023-08-03 13:41:28
合計ジャッジ時間 6,537 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
7,772 KB
testcase_01 AC 16 ms
7,788 KB
testcase_02 AC 16 ms
7,756 KB
testcase_03 AC 16 ms
7,780 KB
testcase_04 AC 16 ms
7,776 KB
testcase_05 AC 16 ms
7,824 KB
testcase_06 AC 16 ms
7,792 KB
testcase_07 AC 17 ms
7,844 KB
testcase_08 AC 16 ms
7,952 KB
testcase_09 AC 31 ms
8,924 KB
testcase_10 AC 26 ms
8,760 KB
testcase_11 AC 37 ms
9,064 KB
testcase_12 AC 20 ms
8,764 KB
testcase_13 AC 46 ms
11,096 KB
testcase_14 AC 29 ms
9,164 KB
testcase_15 AC 23 ms
8,592 KB
testcase_16 AC 27 ms
8,652 KB
testcase_17 AC 35 ms
9,092 KB
testcase_18 AC 74 ms
13,596 KB
testcase_19 AC 16 ms
7,752 KB
testcase_20 AC 16 ms
7,852 KB
testcase_21 AC 16 ms
7,856 KB
testcase_22 AC 16 ms
7,824 KB
testcase_23 AC 16 ms
7,792 KB
testcase_24 AC 16 ms
7,796 KB
testcase_25 AC 17 ms
7,868 KB
testcase_26 AC 16 ms
7,880 KB
testcase_27 AC 17 ms
7,796 KB
testcase_28 AC 17 ms
7,832 KB
testcase_29 AC 16 ms
7,876 KB
testcase_30 AC 16 ms
7,948 KB
testcase_31 AC 16 ms
7,780 KB
testcase_32 AC 16 ms
7,828 KB
testcase_33 AC 16 ms
7,756 KB
testcase_34 AC 16 ms
7,832 KB
testcase_35 AC 16 ms
7,828 KB
testcase_36 AC 17 ms
7,828 KB
testcase_37 AC 16 ms
7,924 KB
testcase_38 AC 16 ms
7,860 KB
testcase_39 AC 16 ms
7,788 KB
testcase_40 AC 17 ms
7,792 KB
testcase_41 AC 17 ms
7,772 KB
testcase_42 AC 16 ms
7,752 KB
testcase_43 AC 15 ms
7,880 KB
testcase_44 AC 16 ms
7,904 KB
testcase_45 AC 16 ms
7,864 KB
testcase_46 AC 16 ms
7,860 KB
testcase_47 AC 16 ms
7,900 KB
testcase_48 AC 16 ms
7,868 KB
testcase_49 AC 75 ms
13,844 KB
testcase_50 AC 20 ms
9,340 KB
testcase_51 AC 19 ms
8,336 KB
testcase_52 AC 30 ms
12,180 KB
testcase_53 AC 22 ms
8,972 KB
testcase_54 AC 33 ms
12,932 KB
testcase_55 AC 16 ms
8,368 KB
testcase_56 AC 21 ms
8,664 KB
testcase_57 AC 41 ms
11,036 KB
testcase_58 AC 64 ms
12,912 KB
testcase_59 AC 53 ms
11,480 KB
testcase_60 AC 33 ms
9,684 KB
testcase_61 AC 96 ms
15,864 KB
testcase_62 AC 34 ms
9,920 KB
testcase_63 AC 67 ms
13,160 KB
testcase_64 AC 32 ms
12,720 KB
testcase_65 AC 24 ms
8,888 KB
testcase_66 AC 32 ms
9,528 KB
testcase_67 AC 31 ms
10,024 KB
testcase_68 AC 33 ms
13,104 KB
testcase_69 AC 16 ms
7,848 KB
testcase_70 AC 16 ms
7,908 KB
testcase_71 AC 16 ms
7,812 KB
testcase_72 AC 16 ms
7,848 KB
testcase_73 AC 17 ms
7,924 KB
testcase_74 AC 16 ms
7,876 KB
testcase_75 AC 16 ms
7,800 KB
testcase_76 AC 16 ms
7,928 KB
testcase_77 AC 17 ms
7,796 KB
testcase_78 AC 16 ms
7,776 KB
testcase_79 AC 16 ms
7,828 KB
testcase_80 AC 16 ms
7,792 KB
testcase_81 AC 16 ms
7,776 KB
testcase_82 AC 15 ms
7,864 KB
testcase_83 AC 16 ms
7,872 KB
testcase_84 AC 16 ms
7,780 KB
testcase_85 AC 15 ms
7,936 KB
testcase_86 AC 16 ms
7,796 KB
testcase_87 AC 16 ms
7,780 KB
testcase_88 AC 16 ms
7,752 KB
testcase_89 AC 35 ms
10,192 KB
testcase_90 AC 49 ms
11,520 KB
testcase_91 AC 73 ms
13,664 KB
testcase_92 AC 25 ms
8,844 KB
testcase_93 AC 52 ms
11,668 KB
testcase_94 AC 17 ms
8,464 KB
testcase_95 AC 55 ms
11,572 KB
testcase_96 AC 45 ms
11,008 KB
testcase_97 AC 44 ms
10,668 KB
testcase_98 AC 33 ms
12,204 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