結果

問題 No.562 超高速一人かるた small
ユーザー finefine
提出日時 2017-08-26 00:11:08
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,465 bytes
コンパイル時間 1,727 ms
コンパイル使用メモリ 167,304 KB
実行使用メモリ 347,776 KB
最終ジャッジ日時 2024-04-23 16:24:16
合計ジャッジ時間 48,395 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 3 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 12 ms
6,400 KB
testcase_08 AC 330 ms
46,592 KB
testcase_09 TLE -
testcase_10 AC 785 ms
89,472 KB
testcase_11 AC 1,704 ms
175,488 KB
testcase_12 AC 328 ms
46,464 KB
testcase_13 AC 6 ms
5,376 KB
testcase_14 TLE -
testcase_15 TLE -
testcase_16 TLE -
testcase_17 TLE -
testcase_18 TLE -
testcase_19 TLE -
testcase_20 TLE -
testcase_21 TLE -
testcase_22 TLE -
testcase_23 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

const ll MOD = 1e9 + 7;

ll cost[20][20], perm[21][21];
ll dp1[1 << 20][20], dp2[1 << 20][21], cnt[1 << 20];

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n;
	cin >> n;
	vector<string> s(n);
	for (int i = 0; i < n; i++) {
		cin >> s[i];
	}

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < i; j++) {
			cost[i][j] = 1;
			for (int k = 0; k < min(s[i].length(), s[j].length()); k++) {
				if (s[i][k] != s[j][k]) break;
				cost[i][j]++;
			}
			cost[j][i] = cost[i][j];
		}
	}

	int hoge = (1 << n) - 1;

	for (int i = 0; i < n; i++) dp1[1 << i][i] = 1;
	for (int i = 0; i <= hoge; i++) {
		for (int j = 0; j < n; j++) {
			if (i & (1 << j)) {
				for (int k = 0; k < n; k++) {
					if (i & (1 << k)) continue;
					dp1[i | (1 << k)][j] = max(dp1[i][j], cost[k][j]);
				}
			}
		}
	}

	for (int i = 0; i < n; i++) perm[i][0] = 1;
	for (int i = 1; i < n; i++) {
		for (int j = 1; j <= i; j++) {
			perm[i][j] = perm[i][j - 1] * (i - j + 1) % MOD;
		}
	}

	for (int i = 0; i <= hoge; i++) cnt[i] = __builtin_popcount(i);

	for (int i = 0; i < n; i++) {
		for (int j = 0; j <= hoge; j++) {
			if (cnt[j] < i) continue;
			for (int k = 0; k < n; k++) {
				if (j & (1 << k)) continue;
				(dp2[j | (1 << k)][i + 1] += dp2[j][i] + perm[cnt[j]][i] * dp1[j | (1 << k)][k] % MOD) %= MOD; 
			}
		}
	}

	for (int i = 1; i <= n; i++) {
		cout << dp2[hoge][i] << endl;
	}
	return 0;
}
0