結果

問題 No.140 みんなで旅行
ユーザー noisy_noiminnoisy_noimin
提出日時 2019-06-17 16:23:02
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 8 ms / 5,000 ms
コード長 2,230 bytes
コンパイル時間 823 ms
コンパイル使用メモリ 92,736 KB
実行使用メモリ 6,576 KB
最終ジャッジ日時 2023-08-18 12:37:45
合計ジャッジ時間 1,901 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cassert>
#include <iostream>
#include <iomanip>
#include <string>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <bitset>

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

using namespace std;

using ll =  long long;
using Pll = pair<ll, ll>;
using Pii = pair<int, int>;

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() {
    std::ios::sync_with_stdio(0); cin.tie(0);
    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