結果

問題 No.665 Bernoulli Bernoulli
ユーザー nagissnagiss
提出日時 2019-09-25 02:53:01
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 85 ms / 2,000 ms
コード長 944 bytes
コンパイル時間 79 ms
コンパイル使用メモリ 11,888 KB
実行使用メモリ 11,088 KB
最終ジャッジ日時 2023-10-19 19:31:26
合計ジャッジ時間 2,299 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
10,144 KB
testcase_01 AC 32 ms
10,144 KB
testcase_02 AC 45 ms
10,668 KB
testcase_03 AC 85 ms
11,088 KB
testcase_04 AC 83 ms
11,068 KB
testcase_05 AC 82 ms
11,052 KB
testcase_06 AC 81 ms
11,044 KB
testcase_07 AC 82 ms
11,032 KB
testcase_08 AC 79 ms
11,032 KB
testcase_09 AC 83 ms
11,068 KB
testcase_10 AC 79 ms
11,032 KB
testcase_11 AC 85 ms
11,080 KB
testcase_12 AC 83 ms
11,068 KB
testcase_13 AC 84 ms
11,084 KB
testcase_14 AC 85 ms
11,088 KB
testcase_15 AC 78 ms
11,044 KB
testcase_16 AC 82 ms
11,040 KB
testcase_17 AC 79 ms
11,048 KB
testcase_18 AC 82 ms
11,032 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def fast_lagrange_interpolation(Y, x, mod=10**9+7):
    # X = [0, 1, 2, ... , n] のラグランジュ補間  O(nlog(mod))  # n==len(Y)-1
    if 0 <= x < len(Y):
        return Y[x] % mod
    factorial, f, numer = [1], 1, x
    for x_ in range(1, len(Y)):
        f = f * x_ % mod
        factorial.append(f)
        numer = numer * (x - x_) % mod
    y = 0
    for x_, (y_, denom1, denom2) in enumerate(zip(Y, factorial, factorial[::-1])):
        y = (y_ * numer * pow((x-x_)*denom1*denom2, mod-2, mod) - y) % mod
    return y


def faulhaber(k, n, mod=10**9+7):  # べき乗和 0^k + 1^k + ... + (n-1)^k
    # n に関する k+1 次式になるので最初の k+2 項を求めれば多項式補間できる  O(k log(mod))
    s, Y = 0, [0]  # 第 0 項は 0
    for x in range(k+1):
        s += pow(x, k, mod)
        Y.append(s)
    return fast_lagrange_interpolation(Y, n, mod)


n, k = map(int, input().split())
print(faulhaber(k, n+1))
0