結果
問題 | No.391 CODING WAR |
ユーザー | taq225 |
提出日時 | 2020-03-09 00:46:57 |
言語 | Python3 (3.12.2 + numpy 1.26.4 + scipy 1.12.0) |
結果 |
AC
|
実行時間 | 698 ms / 2,000 ms |
コード長 | 2,177 bytes |
コンパイル時間 | 128 ms |
コンパイル使用メモリ | 12,672 KB |
実行使用メモリ | 22,628 KB |
最終ジャッジ日時 | 2024-11-07 20:21:35 |
合計ジャッジ時間 | 6,273 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 32 ms
10,752 KB |
testcase_01 | AC | 29 ms
10,752 KB |
testcase_02 | AC | 30 ms
10,880 KB |
testcase_03 | AC | 30 ms
10,880 KB |
testcase_04 | AC | 31 ms
10,752 KB |
testcase_05 | AC | 31 ms
10,752 KB |
testcase_06 | AC | 30 ms
10,752 KB |
testcase_07 | AC | 32 ms
10,880 KB |
testcase_08 | AC | 31 ms
10,752 KB |
testcase_09 | AC | 698 ms
22,492 KB |
testcase_10 | AC | 580 ms
22,496 KB |
testcase_11 | AC | 470 ms
22,628 KB |
testcase_12 | AC | 31 ms
10,880 KB |
testcase_13 | AC | 642 ms
22,356 KB |
testcase_14 | AC | 549 ms
19,544 KB |
testcase_15 | AC | 620 ms
20,736 KB |
testcase_16 | AC | 382 ms
16,424 KB |
testcase_17 | AC | 429 ms
17,784 KB |
testcase_18 | AC | 310 ms
15,396 KB |
testcase_19 | AC | 312 ms
15,544 KB |
ソースコード
from collections import defaultdict class Combinatorics: def __init__(self, N, mod): ''' Preprocess for calculating binomial coefficients nCr (0 <= r <= n, 0 <= n <= N) over the finite field Z/(mod)Z. Input: N (int): maximum n mod (int): a prime number. The order of the field Z/(mod)Z over which nCr is calculated. ''' self.mod = mod self.fact = [0] * (N+1) # n! self.inverse = [None] + [0] * N # inverse of n in the field Z/(MOD)Z self.fact_inverse = [0] * (N+1) # inverse of n! in the field Z/(MOD)Z # preprocess self.fact[0] = self.fact[1] = 1 self.fact_inverse[0] = self.fact_inverse[1] = 1 self.inverse[1] = 1 for i in range(2, N+1): self.fact[i] = i * self.fact[i-1] % self.mod q, r = divmod(self.mod, i) self.inverse[i] = (- (q % self.mod) * self.inverse[r]) % self.mod self.fact_inverse[i] = self.inverse[i] * self.fact_inverse[i-1] % self.mod def perm(self, n, r): ''' Calculate nPr = n! / (n-r)! % mod ''' if n < r or n < 0 or r < 0: return 0 else: return (self.fact[n] * self.fact_inverse[n-r]) % self.mod def binom(self, n, r): ''' Calculate nCr = n! /(r! (n-r)!) % mod ''' if n < r or n < 0 or r < 0: return 0 else: return self.fact[n] * (self.fact_inverse[r] * self.fact_inverse[n-r] % self.mod) % self.mod def hom(self, n, r): ''' Calculate nHr = {n+r-1}Cr % mod. Assign r objects to one of n classes. Arrangement of r circles and n-1 partitions: o o o | o o | | | o | | | o o | | o ''' if n == 0 and r > 0: return 0 if n >= 0 and r == 0: return 1 return self.binom(n + r - 1, r) N, M = map(int, input().split()) MOD = 10**9 + 7 com = Combinatorics(M, MOD) ans = 0 for i in range(M): if i % 2 == 0: ans = (ans + com.binom(M, i) * pow(M - i, N, MOD)) % MOD else: ans = (ans - com.binom(M, i) * pow(M - i, N, MOD)) % MOD print(ans)