結果

問題 No.140 みんなで旅行
ユーザー なおなお
提出日時 2015-01-30 03:05:24
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 105 ms / 5,000 ms
コード長 1,786 bytes
コンパイル時間 1,414 ms
コンパイル使用メモリ 145,628 KB
実行使用メモリ 6,072 KB
最終ジャッジ日時 2023-09-05 08:19:36
合計ジャッジ時間 2,927 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
5,808 KB
testcase_01 AC 3 ms
5,792 KB
testcase_02 AC 7 ms
5,772 KB
testcase_03 AC 4 ms
5,936 KB
testcase_04 AC 3 ms
5,864 KB
testcase_05 AC 4 ms
5,880 KB
testcase_06 AC 4 ms
5,772 KB
testcase_07 AC 4 ms
5,860 KB
testcase_08 AC 3 ms
5,788 KB
testcase_09 AC 4 ms
6,000 KB
testcase_10 AC 3 ms
6,072 KB
testcase_11 AC 105 ms
5,868 KB
testcase_12 AC 6 ms
5,888 KB
testcase_13 AC 4 ms
5,792 KB
testcase_14 AC 105 ms
5,792 KB
testcase_15 AC 104 ms
5,796 KB
testcase_16 AC 40 ms
5,788 KB
testcase_17 AC 20 ms
5,892 KB
testcase_18 AC 80 ms
5,808 KB
testcase_19 AC 89 ms
5,772 KB
testcase_20 AC 16 ms
5,768 KB
testcase_21 AC 4 ms
5,788 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
#define REP(i, n)           for(int(i)=0;(i)<(n);++(i))
#define FOR(i, f, t)        for(int(i)=(f);(i)<(t);(++i))
const int MOD = int(1e9+7);

ll extgcd(ll a,ll b,ll &m,ll &n){ll g=a;m=1;n=0;if(b)g=extgcd(b,a%b,n,m),n-=(a/b)*m;return g;}
ll divmod(ll n,ll m,ll mod){ll a,b;extgcd(m,mod,a,b);return((n*a)%mod+mod)%mod;}

const int factmax = 2000;
ll fact[factmax+1], factinv[factmax+1];
void factmod(){
    fact[0]=fact[1]=1;
    for(int i=2;i<=factmax;i++)fact[i]=(fact[i-1]*i)%MOD;
    factinv[factmax]=divmod(1,fact[factmax],MOD); //inv
    for(int i=factmax;i>0;i--)factinv[i-1]=(factinv[i]*i)%MOD;
}
int Cmod(int n, int r){
    if(n > factmax) throw;
    if(n < r) return 0;
    return fact[n] * factinv[r] % MOD * factinv[n-r] % MOD;
}

int binpow(ll x, ll k, int m){
    if(k==0) return 1;
    if(k%2==0) return binpow(((x%m)*(x%m))%m, k/2, m);
    else return ((x%m)*binpow(x, k-1, m)) % m;
}

// n個をgグループに分ける通り数
const int grpmax = 555;
ll grpmemo[grpmax+1][grpmax+1];
ll grpmod(int n, int g){
    if(n > grpmax || g > grpmax) throw;
    if(n == 0 && g == 0) return 1;
    if(n < g || g <= 0) return 0;
    if(grpmemo[n][g] >= 0) return grpmemo[n][g];
    ll res = (grpmod(n-1, g-1) + (g * grpmod(n-1, g) % MOD)) % MOD;
    return grpmemo[n][g] = res;
}

int main(){
    memset(grpmemo, -1, sizeof(grpmemo));
    factmod();
    int N; cin >> N;
    
    ll res = 0;
    for(int g = 1; g <= N; g++){
        for(int p = 0; p <= N; p++){
            ll k = Cmod(N, p) * grpmod(p, g) % MOD;
            k = k * binpow(g*(g-1)%MOD, N-p, MOD) % MOD;
            res = (res + k) % MOD;
        }
    }
    cout << res << endl;
    return 0;
}
0