結果

問題 No.140 みんなで旅行
ユーザー mayoko_mayoko_
提出日時 2015-01-30 13:36:10
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 35 ms / 5,000 ms
コード長 1,841 bytes
コンパイル時間 593 ms
コンパイル使用メモリ 74,300 KB
実行使用メモリ 6,464 KB
最終ジャッジ日時 2023-09-05 08:21:22
合計ジャッジ時間 1,963 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
6,284 KB
testcase_01 AC 4 ms
6,144 KB
testcase_02 AC 5 ms
6,140 KB
testcase_03 AC 4 ms
6,140 KB
testcase_04 AC 4 ms
6,116 KB
testcase_05 AC 4 ms
6,352 KB
testcase_06 AC 4 ms
6,140 KB
testcase_07 AC 4 ms
6,276 KB
testcase_08 AC 4 ms
6,116 KB
testcase_09 AC 5 ms
6,464 KB
testcase_10 AC 5 ms
6,148 KB
testcase_11 AC 35 ms
6,196 KB
testcase_12 AC 5 ms
6,140 KB
testcase_13 AC 4 ms
6,132 KB
testcase_14 AC 35 ms
6,288 KB
testcase_15 AC 35 ms
6,148 KB
testcase_16 AC 15 ms
6,116 KB
testcase_17 AC 9 ms
6,216 KB
testcase_18 AC 28 ms
6,136 KB
testcase_19 AC 30 ms
6,152 KB
testcase_20 AC 8 ms
6,232 KB
testcase_21 AC 4 ms
6,156 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>
#include <utility>
#include <set>
#include <cctype>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cmath>

#define INF 1000000000

using namespace std;
typedef long long ll;

const int MAXN = 600;
const ll MOD = 1000000007;
ll fact[MAXN];
ll dp[MAXN][MAXN];

void init() {
    // fact
    fact[0] = 1;
    for (ll i = 1; i < MAXN; i++) {
        fact[i] = (fact[i-1] * i) % MOD;
    }
    // dp
    dp[0][0] = 1;
    for (int i = 1; i < MAXN; i++) {
        for (int j = 1; j <= i; j++) {
            dp[i][j] = (dp[i-1][j-1] + (j * dp[i-1][j])%MOD) % MOD;
        }
    }
}

ll mod_pow(ll p, ll n, ll m = MOD) {
    if (n == 0) return 1;
    if (p == 0) return 0;
    if (n == 1) return p % m;
    if (n % 2 == 0) {
        ll tmp = mod_pow(p, n/2, m);
        return (tmp * tmp) % m;
    } else {
        ll tmp = mod_pow(p, n-1, m);
        return (p * tmp) % m;
    }
}

// extgcd
ll extgcd(ll a, ll b, ll& x, ll& y) {
    ll d = a;
    if (b != 0) {
        d = extgcd(b, a % b, y, x);
        y -= (a / b) * x;
    } else {
        x = 1; y = 0;
    }
    return d;
}

// mod_inverse
ll mod_inverse(ll a, ll m = MOD) {
    ll x, y;
    extgcd(a, m, x, y);
    return (m+x%m) % m;
}

int main(void) {
    init();
    int N;
    cin >> N;
    ll ans = 0;
    for (int i = 1; i <= N; i++) {
        // i組作る
        ll tmp = (fact[N] * mod_inverse(fact[N-i])) % MOD;
        tmp = (tmp * mod_inverse(fact[i])) % MOD;
        for (int j = 1; j <= i; j++) {
            // jグループ作る
            ll x = (tmp * dp[i][j]) % MOD;
            x = (x * mod_pow(j*(j-1), N-i)) % MOD;
            ans += x;
            ans %= MOD;
        }
    }
    cout << ans << endl;
    return 0;
}
0