結果

問題 No.665 Bernoulli Bernoulli
ユーザー PachicobuePachicobue
提出日時 2018-03-10 04:02:59
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 499 ms / 2,000 ms
コード長 1,508 bytes
コンパイル時間 4,164 ms
コンパイル使用メモリ 211,684 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-19 13:52:28
合計ジャッジ時間 12,169 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 3 ms
5,376 KB
testcase_03 AC 489 ms
5,376 KB
testcase_04 AC 453 ms
5,376 KB
testcase_05 AC 436 ms
5,376 KB
testcase_06 AC 465 ms
5,376 KB
testcase_07 AC 411 ms
5,376 KB
testcase_08 AC 424 ms
5,376 KB
testcase_09 AC 472 ms
5,376 KB
testcase_10 AC 417 ms
5,376 KB
testcase_11 AC 472 ms
5,376 KB
testcase_12 AC 462 ms
5,376 KB
testcase_13 AC 483 ms
5,376 KB
testcase_14 AC 499 ms
5,376 KB
testcase_15 AC 419 ms
5,376 KB
testcase_16 AC 437 ms
5,376 KB
testcase_17 AC 423 ms
5,376 KB
testcase_18 AC 409 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize "O3"
#pragma GCC target "tune=native"
#pragma GCC target "avx"
#define NDEBUG 1
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

constexpr ll MOD = 1000000007LL;

constexpr int MAX = 10000;
int K;
ll ys[MAX + 2];

ll power(const ll p, const int n)
{
    if (n == 0) {
        return 1;
    }
    if (n % 2 == 1) {
        return power(p, n - 1) * p % MOD;
    } else {
        const ll pp = power(p, n / 2);
        return pp * pp % MOD;
    }
}

inline ll inverse(const ll p)
{
    return power(p, MOD - 2);
}

inline ll minu(const ll a, const ll b) { return (a >= b ? a - b : a + MOD - b); }

inline ll LagrangeInterpolation(const ll x)
{
    ll n = 1;
    for (ll i = 0; i <= K + 1; i++) {
        (n *= x - i) %= MOD;
    }
    ll ans = 0;
    for (ll i = 0; i <= K + 1; i++) {
        ll d = 1;
        for (ll j = 0; j <= K + 1; j++) {
            if (i == j) {
                continue;
            }
            (d *= minu(i, j)) %= MOD;
        }
        (ans += (n * inverse(x - i) % MOD) * (inverse(d) * ys[i] % MOD) % MOD) %= MOD;
    }
    return ans;
}

int main()
{
    ll N;
    cin >> N >> K;
    N %= MOD;
    if (N == 0) {
        cout << 0 << endl;
    } else {
        for (int i = 1; i <= K + 1; i++) {
            ys[i] = (ys[i - 1] + power(i, K)) % MOD;
            if (i == N) {
                cout << ys[i] << endl;
                return 0;
            }
        }
        cout << LagrangeInterpolation(N) << endl;
    }

    return 0;
}
0