結果

問題 No.665 Bernoulli Bernoulli
ユーザー nagiss
提出日時 2019-09-25 02:53:01
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 99 ms / 2,000 ms
コード長 944 bytes
コンパイル時間 82 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 11,904 KB
最終ジャッジ日時 2024-09-19 15:42:10
合計ジャッジ時間 2,630 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 15
権限があれば一括ダウンロードができます

ソースコード

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