結果

問題 No.140 みんなで旅行
ユーザー noisy_noiminnoisy_noimin
提出日時 2019-06-17 16:36:04
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 8 ms / 5,000 ms
コード長 1,861 bytes
コンパイル時間 527 ms
コンパイル使用メモリ 66,456 KB
実行使用メモリ 6,824 KB
最終ジャッジ日時 2023-08-18 13:04:52
合計ジャッジ時間 1,699 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,384 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 7 ms
5,908 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 8 ms
6,824 KB
testcase_15 AC 8 ms
5,992 KB
testcase_16 AC 5 ms
6,688 KB
testcase_17 AC 3 ms
4,456 KB
testcase_18 AC 6 ms
6,492 KB
testcase_19 AC 6 ms
5,872 KB
testcase_20 AC 3 ms
4,380 KB
testcase_21 AC 1 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>

#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()

using namespace std;

using ll =  long long;

constexpr ll MOD = 1000000007;
constexpr int N_MAX = 556;

ll fact[N_MAX], rfact[N_MAX];

ll perm(ll n, ll r){
    return (fact[n] * rfact[r]) % MOD;
}

ll comb(ll n, ll r){
    return (perm(n, r) * rfact[n-r]) % MOD;
}

void init(ll n){
    fact[0] = fact[1] = 1;
    rfact[0] = rfact[1] = 1;
    for(int i=2;i<=n;++i) {
        fact[i] = (fact[i-1] * (ll)i) % MOD;
        rfact[i] = 1;
        ll k = MOD-2;
        ll a = fact[i];
        while(k > 0){
            if(k & 1){
                rfact[i] *= a;
                rfact[i] %= MOD;
            }
            a *= a;
            a %= MOD;
            k  >>= 1;
        }
    }
}

class Stirling {
    public:
    int n, m; // ボールの個数,グループ数
    ll s[2*N_MAX][N_MAX];
    Stirling(int n, int m, bool is_the_second_kind=true): n(n), m(m) {
        if(is_the_second_kind) {
            for(int i=0;i<=n;++i) {
                s[i][0] = (i == 0)?1:0;
                if(i == 0) continue;
                for(int j=1;j<=m;++j) {
                    s[i][j] = (s[i-1][j-1] + (s[i-1][j]*j) % MOD) % MOD;
                }
            }
        }
    }

    ll get(int i, int j) {
        return s[i][j];
    }
};

ll modpow(ll a, ll t) {
    ll ret = 1LL;
    while(t){
        if(t & 1LL){
            ret *= a;
            ret %= MOD;
        }
        a *= a;
        a %= MOD;
        t >>= 1;
    }
    return ret;
}

int main() {
    ll n;
    cin >> n;
    init(n);
    Stirling s = Stirling(n, n);

    ll ans = 0LL;
    for(ll x=1;x<=n;++x) {
        for(ll y=1;y<=x;++y) {
            ans += (((comb(n, x) * s.get(x, y)) % MOD) * modpow((y * (y-1)) % MOD, n-x)) % MOD;
            ans %= MOD;
        }
    }
    cout << ans << endl;
}
0