結果

問題 No.140 みんなで旅行
ユーザー srup٩(๑`н´๑)۶srup٩(๑`н´๑)۶
提出日時 2016-09-17 13:33:34
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 9 ms / 5,000 ms
コード長 1,395 bytes
コンパイル時間 440 ms
コンパイル使用メモリ 61,396 KB
実行使用メモリ 9,032 KB
最終ジャッジ日時 2024-04-28 15:19:49
合計ジャッジ時間 1,099 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 1 ms
6,944 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 9 ms
8,908 KB
testcase_12 AC 3 ms
6,940 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 9 ms
9,032 KB
testcase_15 AC 9 ms
9,032 KB
testcase_16 AC 4 ms
8,188 KB
testcase_17 AC 3 ms
8,560 KB
testcase_18 AC 8 ms
8,644 KB
testcase_19 AC 7 ms
8,772 KB
testcase_20 AC 3 ms
7,740 KB
testcase_21 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0;i<(n);i++)
const int mod = 1e9 + 7;

//スターリング数 S(n, k) 区別できるn個のものを区別できないkグループに分類する方法の場合の数
//S[i][j] := i組の夫婦が同じグループに属し、合計yグループ作る場合の数
ll S[600][600];
//com[i][j] := iCj
ll com[600][600];

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

int main(void){
	int n; cin >> n;

	S[0][0] = 1;
	for (int i = 1; i <= n; ++i){
		for (int j = 1; j <= i; ++j){
			//スターリング数の漸化式
			S[i][j] = (S[i - 1][j - 1] + j * S[i - 1][j]) % mod;
		}
	}

	//combination
	com[0][0] = 1;
	for (int i = 1; i <= n; ++i){
		for (int j = 0; j <= i; ++j){
			//パスカルの3角形
			if(j == 0) com[i][j] = com[i - 1][j] % mod;
			else com[i][j] = (com[i - 1][j] + com[i - 1][j - 1]) % mod;
		}
	}

	ll sum = 0;
	for (int i = 1; i <= n; ++i){
		for (int j = 1; j <= i; ++j){
			//夫婦が同じグループに入るi組の選び方と、残りのn-	i組の夫婦の入れ方をかける
			sum += (com[n][i] * S[i][j]) % mod * powmod(j * (j - 1), n - i, mod) % mod;
			sum %= mod;
		}	
	}

	printf("%lld\n", sum % mod);
	return 0;
}
0