結果

問題 No.562 超高速一人かるた small
ユーザー finefine
提出日時 2017-08-26 01:16:30
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 478 ms / 3,000 ms
コード長 1,522 bytes
コンパイル時間 1,821 ms
コンパイル使用メモリ 175,512 KB
実行使用メモリ 183,904 KB
最終ジャッジ日時 2024-04-23 16:31:37
合計ジャッジ時間 8,112 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 3 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 4 ms
6,940 KB
testcase_08 AC 53 ms
29,156 KB
testcase_09 AC 460 ms
183,776 KB
testcase_10 AC 111 ms
50,652 KB
testcase_11 AC 229 ms
95,716 KB
testcase_12 AC 53 ms
26,464 KB
testcase_13 AC 4 ms
6,944 KB
testcase_14 AC 469 ms
183,772 KB
testcase_15 AC 464 ms
183,776 KB
testcase_16 AC 464 ms
183,900 KB
testcase_17 AC 476 ms
183,644 KB
testcase_18 AC 469 ms
183,772 KB
testcase_19 AC 469 ms
183,900 KB
testcase_20 AC 476 ms
183,776 KB
testcase_21 AC 478 ms
183,900 KB
testcase_22 AC 464 ms
183,648 KB
testcase_23 AC 473 ms
183,904 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

const ll MOD = 1e9 + 7;

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

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];
	}
	sort(s.begin(), s.end());

	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 = 1; i <= hoge; i++) {
		vector<int> tmp;
		for (int j = 0; j < n; j++) {
			if (i & (1 << j)) {
				tmp.push_back(j);
			}
		}

		for (int j = 0; j < tmp.size(); j++) {
			if (j > 0) {
				dp1[i][tmp[j]] = max(dp1[i][tmp[j]], cost[tmp[j - 1]][tmp[j]]);
			}
			if (j + 1 < tmp.size()) {
				dp1[i][tmp[j]] = max(dp1[i][tmp[j]], cost[tmp[j + 1]][tmp[j]]);
			}
		}
	}

	fact[0] = 1;
	for (int i = 1; i < n; i++) fact[i] = fact[i - 1] * i % MOD;;

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

	for (int i = hoge; i > 0; i--) {
		for (int j = 0; j < n; j++) {
			if (i & (1 << j)) {
				(dp2[i ^ (1 << j)] += dp2[i] + fact[n - cnt[i]] * dp1[i][j]) %= MOD;
			}
		}
	}

	for (int i = 0; i < hoge; i++) {
		(ans[n - cnt[i]] += dp2[i]) %= MOD;
	}

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