結果

問題 No.665 Bernoulli Bernoulli
ユーザー V_Melville
提出日時 2023-09-27 23:26:30
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 372 ms / 2,000 ms
コード長 2,315 bytes
コンパイル時間 3,009 ms
コンパイル使用メモリ 250,732 KB
実行使用メモリ 19,072 KB
最終ジャッジ日時 2024-07-20 17:49:57
合計ジャッジ時間 9,688 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 15
権限があれば一括ダウンロードができます

ソースコード

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