結果
問題 | No.573 a^2[i] = a[i] |
ユーザー | 89 |
提出日時 | 2022-02-06 19:25:38 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 243 ms / 2,000 ms |
コード長 | 3,531 bytes |
コンパイル時間 | 276 ms |
コンパイル使用メモリ | 86,916 KB |
実行使用メモリ | 230,108 KB |
最終ジャッジ日時 | 2023-09-02 07:05:18 |
合計ジャッジ時間 | 12,253 ms |
ジャッジサーバーID (参考情報) |
judge14 / judge12 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 202 ms
227,716 KB |
testcase_01 | AC | 205 ms
228,084 KB |
testcase_02 | AC | 199 ms
227,940 KB |
testcase_03 | AC | 198 ms
227,904 KB |
testcase_04 | AC | 199 ms
227,788 KB |
testcase_05 | AC | 198 ms
227,976 KB |
testcase_06 | AC | 202 ms
227,944 KB |
testcase_07 | AC | 197 ms
227,468 KB |
testcase_08 | AC | 197 ms
227,824 KB |
testcase_09 | AC | 199 ms
227,756 KB |
testcase_10 | AC | 198 ms
228,048 KB |
testcase_11 | AC | 201 ms
227,760 KB |
testcase_12 | AC | 198 ms
228,124 KB |
testcase_13 | AC | 202 ms
227,904 KB |
testcase_14 | AC | 201 ms
227,568 KB |
testcase_15 | AC | 202 ms
227,648 KB |
testcase_16 | AC | 198 ms
227,764 KB |
testcase_17 | AC | 197 ms
228,064 KB |
testcase_18 | AC | 196 ms
227,764 KB |
testcase_19 | AC | 197 ms
227,640 KB |
testcase_20 | AC | 197 ms
227,644 KB |
testcase_21 | AC | 195 ms
227,864 KB |
testcase_22 | AC | 202 ms
227,952 KB |
testcase_23 | AC | 195 ms
227,960 KB |
testcase_24 | AC | 193 ms
228,276 KB |
testcase_25 | AC | 194 ms
227,972 KB |
testcase_26 | AC | 195 ms
228,036 KB |
testcase_27 | AC | 196 ms
228,124 KB |
testcase_28 | AC | 197 ms
228,004 KB |
testcase_29 | AC | 197 ms
227,780 KB |
testcase_30 | AC | 196 ms
228,232 KB |
testcase_31 | AC | 197 ms
227,796 KB |
testcase_32 | AC | 196 ms
228,108 KB |
testcase_33 | AC | 195 ms
228,328 KB |
testcase_34 | AC | 207 ms
228,140 KB |
testcase_35 | AC | 200 ms
228,636 KB |
testcase_36 | AC | 197 ms
228,304 KB |
testcase_37 | AC | 197 ms
228,468 KB |
testcase_38 | AC | 197 ms
228,108 KB |
testcase_39 | AC | 202 ms
229,220 KB |
testcase_40 | AC | 201 ms
228,512 KB |
testcase_41 | AC | 198 ms
229,124 KB |
testcase_42 | AC | 200 ms
228,468 KB |
testcase_43 | AC | 204 ms
228,584 KB |
testcase_44 | AC | 205 ms
228,940 KB |
testcase_45 | AC | 201 ms
228,648 KB |
testcase_46 | AC | 243 ms
230,108 KB |
ソースコード
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 res = 0 comb = Combination(1000000) n = int(input()) for i in range(n+1): res += comb.C(n,i)*pow(i,n - i,mod) res %= mod print(res)