結果

問題 No.389 ロジックパズルの組み合わせ
ユーザー 8989
提出日時 2022-02-06 19:35:04
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 268 ms / 2,000 ms
コード長 3,662 bytes
コンパイル時間 1,115 ms
コンパイル使用メモリ 87,132 KB
実行使用メモリ 264,052 KB
最終ジャッジ日時 2023-09-02 07:05:50
合計ジャッジ時間 27,746 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 198 ms
228,076 KB
testcase_01 AC 200 ms
228,052 KB
testcase_02 AC 199 ms
227,912 KB
testcase_03 AC 199 ms
228,032 KB
testcase_04 AC 200 ms
228,048 KB
testcase_05 AC 197 ms
228,532 KB
testcase_06 AC 203 ms
228,124 KB
testcase_07 AC 202 ms
228,756 KB
testcase_08 AC 200 ms
228,188 KB
testcase_09 AC 208 ms
231,984 KB
testcase_10 AC 214 ms
230,612 KB
testcase_11 AC 210 ms
235,180 KB
testcase_12 AC 205 ms
230,192 KB
testcase_13 AC 228 ms
253,300 KB
testcase_14 AC 209 ms
234,232 KB
testcase_15 AC 201 ms
229,180 KB
testcase_16 AC 204 ms
230,556 KB
testcase_17 AC 206 ms
233,744 KB
testcase_18 AC 255 ms
255,312 KB
testcase_19 AC 201 ms
228,112 KB
testcase_20 AC 200 ms
228,028 KB
testcase_21 AC 202 ms
228,164 KB
testcase_22 AC 199 ms
227,860 KB
testcase_23 AC 198 ms
228,320 KB
testcase_24 AC 197 ms
228,252 KB
testcase_25 AC 198 ms
228,128 KB
testcase_26 AC 198 ms
228,340 KB
testcase_27 AC 200 ms
228,076 KB
testcase_28 AC 197 ms
228,032 KB
testcase_29 AC 199 ms
228,100 KB
testcase_30 AC 198 ms
228,104 KB
testcase_31 AC 199 ms
228,144 KB
testcase_32 AC 198 ms
227,912 KB
testcase_33 AC 198 ms
227,792 KB
testcase_34 AC 202 ms
228,392 KB
testcase_35 AC 200 ms
228,336 KB
testcase_36 AC 199 ms
228,416 KB
testcase_37 AC 199 ms
228,048 KB
testcase_38 AC 198 ms
228,704 KB
testcase_39 AC 198 ms
227,928 KB
testcase_40 AC 199 ms
228,272 KB
testcase_41 AC 200 ms
228,408 KB
testcase_42 AC 200 ms
228,564 KB
testcase_43 AC 199 ms
228,208 KB
testcase_44 AC 199 ms
228,408 KB
testcase_45 AC 201 ms
228,024 KB
testcase_46 AC 198 ms
228,264 KB
testcase_47 AC 201 ms
228,104 KB
testcase_48 AC 201 ms
228,308 KB
testcase_49 AC 254 ms
255,120 KB
testcase_50 AC 206 ms
228,828 KB
testcase_51 AC 204 ms
228,636 KB
testcase_52 AC 208 ms
234,720 KB
testcase_53 AC 206 ms
230,044 KB
testcase_54 AC 210 ms
235,884 KB
testcase_55 AC 201 ms
228,480 KB
testcase_56 AC 201 ms
229,984 KB
testcase_57 AC 220 ms
247,688 KB
testcase_58 AC 250 ms
255,188 KB
testcase_59 AC 230 ms
256,932 KB
testcase_60 AC 213 ms
240,008 KB
testcase_61 AC 268 ms
253,316 KB
testcase_62 AC 212 ms
241,444 KB
testcase_63 AC 248 ms
254,892 KB
testcase_64 AC 211 ms
235,780 KB
testcase_65 AC 208 ms
232,432 KB
testcase_66 AC 214 ms
239,532 KB
testcase_67 AC 214 ms
238,184 KB
testcase_68 AC 213 ms
237,056 KB
testcase_69 AC 201 ms
228,132 KB
testcase_70 AC 199 ms
228,008 KB
testcase_71 AC 197 ms
227,888 KB
testcase_72 AC 203 ms
228,400 KB
testcase_73 AC 198 ms
228,196 KB
testcase_74 AC 201 ms
227,984 KB
testcase_75 AC 199 ms
228,332 KB
testcase_76 AC 200 ms
228,472 KB
testcase_77 AC 200 ms
228,260 KB
testcase_78 AC 202 ms
228,308 KB
testcase_79 AC 202 ms
227,984 KB
testcase_80 AC 201 ms
228,260 KB
testcase_81 AC 202 ms
228,212 KB
testcase_82 AC 201 ms
228,180 KB
testcase_83 AC 200 ms
228,260 KB
testcase_84 AC 200 ms
228,340 KB
testcase_85 AC 200 ms
228,264 KB
testcase_86 AC 199 ms
228,324 KB
testcase_87 AC 197 ms
228,180 KB
testcase_88 AC 199 ms
228,172 KB
testcase_89 AC 221 ms
242,940 KB
testcase_90 AC 230 ms
253,404 KB
testcase_91 AC 259 ms
255,124 KB
testcase_92 AC 211 ms
233,812 KB
testcase_93 AC 234 ms
256,568 KB
testcase_94 AC 204 ms
228,492 KB
testcase_95 AC 242 ms
264,052 KB
testcase_96 AC 227 ms
250,352 KB
testcase_97 AC 226 ms
250,012 KB
testcase_98 AC 214 ms
236,616 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def extgcd(a, b):
    if b:
        d, y, x = extgcd(b, a % b)
        y -= (a // b)*x
        return d, x, y
    return a, 1, 0
class Combination:
    def __init__(self, n_max, mod=10**9+7):
        # O(n_max + log(mod))
        self.mod = mod
        f = 1
        self.fac = fac = [f]
        for i in range(1, n_max+1):
            f = f * i % mod
            fac.append(f)
        f = pow(f, mod-2, mod)
        self.facinv = facinv = [f]
        for i in range(n_max, 0, -1):
            f = f * i % mod
            facinv.append(f)
        facinv.reverse()

    # "n 要素を" は区別できる n 要素
    # "k グループ" はちょうど k グループ

    def __call__(self, n, r):  # self.C と同じ
        return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n-r] % self.mod

    def C(self, n, r):
        if not 0 <= r <= n: return 0
        return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n-r] % self.mod

    def P(self, n, r):
        if not 0 <= r <= n: return 0
        return self.fac[n] * self.facinv[n-r] % self.mod

    def H(self, n, r):
        if (n == 0 and r > 0) or r < 0: return 0
        return self.fac[n+r-1] * self.facinv[r] % self.mod * self.facinv[n-1] % self.mod

    def rising_factorial(self, n, r):  # 上昇階乗冪 n * (n+1) * ... * (n+r-1)
        return self.fac[n+r-1] * self.facinv[n-1] % self.mod
    def stirling_first(self, n, k):  # 第 1 種スターリング数  lru_cache を使うと O(nk)  # n 要素を k 個の巡回列に分割する場合の数
            if n == k: return 1
            if k == 0: return 0
            return (self.stirling_first(n-1, k-1) + (n-1)*self.stirling_first(n-1, k)) % self.mod
    
    def stirling_second(self, n, k):  # 第 2 種スターリング数 O(k + log(n))  # n 要素を区別のない k グループに分割する場合の数
            if n == k: return 1  # n==k==0 のときのため
            return self.facinv[k] * sum((-1)**(k-m) * self.C(k, m) * pow(m, n, self.mod) for m in range(1, k+1)) % self.mod
    
    def balls_and_boxes_3(self, n, k):  # n 要素を区別のある k グループに分割する場合の数  O(k + log(n))
            return sum((-1)**(k-m) * self.C(k, m) * pow(m, n, self.mod) for m in range(1, k+1)) % self.mod
    
    def bernoulli(self, n):  # ベルヌーイ数  lru_cache を使うと O(n**2 * log(mod))
            if n == 0: return 1
            if n % 2 and n >= 3: return 0  # 高速化
            return (- pow(n+1, self.mod-2, self.mod) * sum(self.C(n+1, k) * self.bernoulli(k) % self.mod for k in range(n))) % self.mod
    
    def faulhaber(self, k, n):  # べき乗和 0^k + 1^k + ... + (n-1)^k
            # bernoulli に lru_cache を使うと O(k**2 * log(mod))  bernoulli が計算済みなら O(k * log(mod))
            return pow(k+1, self.mod-2, self.mod) * sum(self.C(k+1, j) * self.bernoulli(j) % self.mod * pow(n, k-j+1, self.mod) % self.mod for j in range(k+1)) % self.mod
    
    def lah(self, n, k):  # n 要素を k 個の空でない順序付き集合に分割する場合の数  O(1)
            return self.C(n-1, k-1) * self.fac[n] % self.mod * self.facinv[k] % self.mod
    
    def bell(self, n, k):  # n 要素を k グループ以下に分割する場合の数  O(k**2 + k*log(mod))
            return sum(self.stirling_second(n, j) for j in range(1, k+1)) % self.mod
mod = 10**9+7
comb = Combination(1000000)
m = int(input())
h = list(map(int,input().split()))
if len(h) == 1 and h[0] == 0:
    print(1)
    exit()
P = sum(h) + len(h) - 1
if P > m:
  print("NA")
  exit()
a = comb.C(m - P + len(h),len(h))
if a == 0:
    print("NA")
else:
    print(a)
0