結果

問題 No.665 Bernoulli Bernoulli
ユーザー V_MelvilleV_Melville
提出日時 2023-09-27 23:26:30
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 333 ms / 2,000 ms
コード長 2,315 bytes
コンパイル時間 3,310 ms
コンパイル使用メモリ 248,176 KB
実行使用メモリ 18,820 KB
最終ジャッジ日時 2023-09-27 23:26:40
合計ジャッジ時間 9,226 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
18,436 KB
testcase_01 AC 15 ms
18,328 KB
testcase_02 AC 319 ms
18,680 KB
testcase_03 AC 322 ms
18,636 KB
testcase_04 AC 307 ms
18,760 KB
testcase_05 AC 295 ms
18,724 KB
testcase_06 AC 282 ms
18,524 KB
testcase_07 AC 276 ms
18,808 KB
testcase_08 AC 276 ms
18,736 KB
testcase_09 AC 316 ms
18,820 KB
testcase_10 AC 279 ms
18,732 KB
testcase_11 AC 321 ms
18,688 KB
testcase_12 AC 317 ms
18,516 KB
testcase_13 AC 333 ms
18,724 KB
testcase_14 AC 331 ms
18,820 KB
testcase_15 AC 286 ms
18,484 KB
testcase_16 AC 298 ms
18,624 KB
testcase_17 AC 289 ms
18,488 KB
testcase_18 AC 279 ms
18,768 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)

using namespace std;
using ll = long long;

//const int mod = 998244353;
const int mod = 1000000007;
struct mint {
    ll x;
    mint(ll x=0):x((x%mod+mod)%mod) {}
    mint operator-() const {
        return mint(-x);
    }
    mint& operator+=(const mint a) {
        if ((x += a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator-=(const mint a) {
        if ((x += mod-a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator*=(const mint a) {
        (x *= a.x) %= mod;
        return *this;
    }
    mint operator+(const mint a) const {
        return mint(*this) += a;
    }
    mint operator-(const mint a) const {
        return mint(*this) -= a;
    }
    mint operator*(const mint a) const {
        return mint(*this) *= a;
    }
    mint pow(ll t) const {
        if (!t) return 1;
        mint a = pow(t>>1);
        a *= a;
        if (t&1) a *= *this;
        return a;
    }

    // for prime mod
    mint inv() const {
        return pow(mod-2);
    }
    mint& operator/=(const mint a) {
        return *this *= a.inv();
    }
    mint operator/(const mint a) const {
        return mint(*this) /= a;
    }
};
istream& operator>>(istream& is, mint& a) {
    return is >> a.x;
}
ostream& operator<<(ostream& os, const mint& a) {
    return os << a.x;
}

// combination mod prime
struct combination {
  vector<mint> fact, ifact;
  combination(int n):fact(n+1),ifact(n+1) {
    fact[0] = 1;
    for (int i = 1; i <= n; ++i) fact[i] = fact[i-1]*i;
    ifact[n] = fact[n].inv();
    for (int i = n; i >= 1; --i) ifact[i-1] = ifact[i]*i;
  }
  mint operator()(int n, int k) {
    if (k < 0 || k > n) return 0;
    return fact[n]*ifact[k]*ifact[n-k];
  }
} comb(1000005);

int main() {
    ll n; int k;
    cin >> n >> k;
    
    vector<mint> B(k+1);
    B[0] = 1;
    for (int i = 1; i <= k; ++i) {
        mint now;
        rep(j, i) now -= comb(i+1, j)*B[j];
        now /= i+1;
        B[i] = now;
    }
    
    vector<mint> pown(k+2, 1);
    rep(i, k+1) pown[i+1] = pown[i]*n; 
    
    mint ans;
    rep(i, k+1) {
        mint now = comb(k+1, i)*B[i]*pown[k+1-i];
        if (~i&1) ans += now;
        else ans -= now;
    }
    ans /= k+1;
    
    cout << ans << '\n';
    
    return 0;
}
0