結果

問題 No.140 みんなで旅行
ユーザー Yang33Yang33
提出日時 2018-05-03 14:46:14
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 17 ms / 5,000 ms
コード長 3,324 bytes
コンパイル時間 1,817 ms
コンパイル使用メモリ 176,420 KB
実行使用メモリ 5,524 KB
最終ジャッジ日時 2023-09-10 09:18:20
合計ジャッジ時間 3,256 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

using VS = vector<string>;    using LL = long long;
using VI = vector<int>;       using VVI = vector<VI>;
using PII = pair<int, int>;   using PLL = pair<LL, LL>;
using VL = vector<LL>;        using VVL = vector<VL>;

#define ALL(a)  begin((a)),end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))
#define FOR(i, s, e) for (int(i) = (s); (i) < (e); (i)++)
#define FORR(i, s, e) for (int(i) = (s); (i) > (e); (i)--)
#define debug(x) cerr << #x << ": " << x << endl
const int INF = 1e9;                          const LL LINF = 1e16;
const LL MOD = 1000000007;                    const double PI = acos(-1.0);
int DX[8] = { 0, 0, 1, -1, 1, 1, -1, -1 };    int DY[8] = { 1, -1, 0, 0, 1, -1, 1, -1 };

/* -----  2018/05/02  Problem: yukicoder 140  / Link: http://yukicoder.me/problems/no/140  ----- */
/* ------問題------

N 組の夫婦、計(2×N)人で旅行に行くことにした。
(2×N)人は幾つかのグループに別れ、それぞれグループメンバの車で移動することになった。

各夫婦は皆大型車を持っており、グループに1台車があればグループ全員が移動できる。
ただし、各夫婦が車を使わせてくれるのは、夫婦が同一のグループにいる場合だけである。
すなわち、各グループが無事に車で旅行に行くためには、グループに1組以上夫婦のペアが含まれていなければならない。

条件を満たすグループの分け方が何通りあるかを答えよ。
答えは非常に大きくなる可能性があるので、109+7の剰余を取った値を答えること。

-----問題ここまで----- */
/* -----解説等-----



----解説ここまで---- */


const long long mod = 1e9 + 7;

long long modpow(long long a, long long b) {
	if (b == 0) return 1;
	return modpow(a * a % mod, b / 2) * (b & 1 ? a : 1) % mod;
}

long long modinv(long long a) {
	return modpow(a, mod - 2);
}

vector<long long> fact, inv_fact;

void init_fact(int n) {
	fact.resize(n);
	fact[0] = 1;
	for (int i = 1; i < n; i++) {
		fact[i] = i * fact[i - 1] % mod;
	}
	inv_fact.resize(n);
	inv_fact[n - 1] = modinv(fact[n - 1]);
	for (int i = n - 2; i >= 0; i--) {
		inv_fact[i] = (i + 1) * inv_fact[i + 1] % mod;
	}
}

long long nCr(int n, int r) {
	if (n < r || n < 0 || r < 0) return 0;
	return fact[n] * inv_fact[r] % mod * inv_fact[n - r] % mod;
}

vector<vector<long long>> S;

void stirling_number(int n) {
	S.resize(n, vector<long long>(n, 0));
	for (int i = 1; i < n; i++) {
		S[i][1] = S[i][i] = 1;
		for (int j = 2; j < i; j++) {
			S[i][j] = (S[i - 1][j - 1] + j*S[i - 1][j]) % mod;
		}
	}
}

long long nSr(int n, int r) { // 区別できるn個のものを区別できないrグループに分類する場合の数
	return S[n][r];
}

LL N;

LL ans = 0LL;

int main() {
	cin.tie(0);
	ios_base::sync_with_stdio(false);

	cin >> N;
	init_fact(2 * N + 1);
	stirling_number(N + 1);
	// 
	FOR(i, 1, N + 1) {
		FOR(j, 1, i + 1) { // 集合の選択 -> 分類 -> 重複せず配布
			ans += (nCr(N,i) * nSr(i,j)%MOD * modpow(j*(j-1),N-i)) % MOD;
			ans %= MOD;
		}
	}
	cout << ans << "\n";

	return 0;
}
0