結果

問題 No.140 みんなで旅行
ユーザー syak_18syak_18
提出日時 2019-04-29 11:02:22
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 54 ms / 5,000 ms
コード長 1,577 bytes
コンパイル時間 3,429 ms
コンパイル使用メモリ 170,152 KB
実行使用メモリ 8,712 KB
最終ジャッジ日時 2023-08-25 16:41:11
合計ジャッジ時間 4,569 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
8,480 KB
testcase_01 AC 39 ms
8,712 KB
testcase_02 AC 40 ms
8,536 KB
testcase_03 AC 40 ms
8,448 KB
testcase_04 AC 40 ms
8,436 KB
testcase_05 AC 39 ms
8,560 KB
testcase_06 AC 40 ms
8,552 KB
testcase_07 AC 40 ms
8,536 KB
testcase_08 AC 39 ms
8,564 KB
testcase_09 AC 39 ms
8,556 KB
testcase_10 AC 39 ms
8,560 KB
testcase_11 AC 54 ms
8,568 KB
testcase_12 AC 40 ms
8,444 KB
testcase_13 AC 40 ms
8,624 KB
testcase_14 AC 54 ms
8,624 KB
testcase_15 AC 54 ms
8,476 KB
testcase_16 AC 44 ms
8,632 KB
testcase_17 AC 42 ms
8,628 KB
testcase_18 AC 50 ms
8,520 KB
testcase_19 AC 51 ms
8,432 KB
testcase_20 AC 40 ms
8,516 KB
testcase_21 AC 40 ms
8,448 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9+7;

ll fac[200000+1],ifac[200000+1];
 
ll mpow(ll x,ll n){
    ll res = 1;
    while(n != 0){
        if(n&1) res = res*x % mod;
        x = x*x % mod;
        n = n >> 1; 
    }
    return res;
}
ll comb(ll a,ll b){
    if(a==0 && b==0) return 1;
    if(a<b || a<0) return 0;
    ll tmp = ifac[a-b]*ifac[b] % mod;
    return tmp*fac[a] % mod;
}
void calc_fac(){
   fac[0] = 1;
   ifac[0] = 1;
   for(int i = 0;i < 200000;i ++){
       fac[i+1] = fac[i]*(i+1) % mod;
       ifac[i+1] = ifac[i]*mpow(i+1,mod-2) % mod;
   }
}
/*
写像十二相より
玉n個区別箱k個区別しないもとで箱に1個以上入るようなとき
S(n,k) = S(n-1,k-1) + kS(n-1,k) の漸化式より計算
O(nk) だが全ての状態を保存できる
*/
template <typename T> struct Stirling{
    
    vector<vector<T> > S;
    constexpr Stirling(int MAX) noexcept : S(MAX+1, vector<T>(MAX+1, 0)) {
        S[0][0] = 1;
        for (int n = 1; n <= MAX; ++n) {
            for (int k = 1; k <= n; ++k) {
                S[n][k] = (S[n-1][k-1] + S[n-1][k] * k)%mod;
            }
        }
    }
    constexpr T stir(int n, int k) {
        if (n < 0 || k < 0 || n < k) return 0;
        return S[n][k];
    }
};

int main(){
    int n;
    ll ans=0;
    cin >> n;
    Stirling<ll> s(555);
  	calc_fac();
    for(int i = 1;i <= n;i ++){
        for(int x = 1;x <= n;x ++){
            ans = (ans + comb(n,x)*s.stir(x,i)%mod*mpow(i*(i-1),n-x)%mod)%mod;
        }
    }
    cout << ans << endl;
    return 0;
}
0