結果
| 問題 | No.140 みんなで旅行 | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2023-11-03 12:45:22 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 21 ms / 5,000 ms | 
| コード長 | 2,009 bytes | 
| コンパイル時間 | 901 ms | 
| コンパイル使用メモリ | 104,284 KB | 
| 最終ジャッジ日時 | 2025-02-17 17:24:44 | 
| ジャッジサーバーID (参考情報) | judge4 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 19 | 
ソースコード
#include<iostream>
#include<set>
#include<algorithm>
#include<vector>
#include<string>
#include<set>
#include<map>
#include<stack>
#include<numeric>
#include<queue>
#include<cmath>
#include<deque>
using namespace std;
typedef long long ll;
const ll INF=1LL<<60;
typedef pair<int,int> P;
typedef pair<int,P> PP;
const ll MOD=1e9+7;
const int inf=1<<30;
//xのy乗
ll mod_pow(ll x,ll y,ll mod){
    ll power=x;
    ll ret = 1;
    while(y>0){
        if(y&1){
           ret = ret*power%mod;
        }
        power = power*power%mod;
        y = y>>1;
    }
    return ret;
}
struct combination{
    typedef long long ll;
    std::vector<ll> fact,ifact;
    combination(ll n): fact(n+1),ifact(n+1){
        fact[0]=1;//0!=1通り
        for(ll i=1;i<=n;i++) fact[i] = fact[i-1]*i%MOD;
        //ifact[n] = fact[n].inv();
        
        // n!の逆元がifact[n]
        ifact[n] = mod_pow(fact[n],MOD-2,MOD);// n!を(mod-2)乗した後でmodであまりをとった
        for(ll i=n;i>=1;i--) ifact[i-1] = ifact[i]*i%MOD;
    }
    ll operator()(ll n,ll k){
        if(k<0 || k>n) return 0;
        // n!/( k!*(n-k)! )
        return (fact[n]*ifact[k])%MOD * ifact[n-k]%MOD;
    }
};
combination comb(100000+10);
int main(){
    int N;
    cin>>N;
    vector<vector<ll>> S(N+1,vector<ll>(N+1,0));
    for(int n=1;n<=N;n++){
        S[n][1]=1;
        S[n][n]=1;
    }
    for(int k=2;k<=N;k++){ 
        for(int n=k+1;n<=N;n++){
            S[n][k]=S[n-1][k-1]+k*S[n-1][k]%MOD;
            S[n][k]%=MOD;
        }
    }
    ll ans=0;
    for(int n=1;n<=N;n++){
        for(int k=1;k<=N;k++){
            //kグループ. 
            //n組の夫婦のペア
            ll res=comb(N,n);
            //互いに区別できるn個のものを空でないkグループに分割する
            res*=S[n][k];
            res%=MOD;
            res*=mod_pow(k*(k-1)%MOD,N-n,MOD);
            res%=MOD;
            ans+=res;
            ans%=MOD;
        }
    }
    cout<<ans<<endl;
}
            
            
            
        