結果
| 問題 |
No.563 超高速一人かるた large
|
| コンテスト | |
| ユーザー |
koyumeishi
|
| 提出日時 | 2017-06-18 15:16:08 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 3,065 bytes |
| コンパイル時間 | 1,649 ms |
| コンパイル使用メモリ | 124,692 KB |
| 実行使用メモリ | 13,648 KB |
| 最終ジャッジ日時 | 2024-10-01 20:00:38 |
| 合計ジャッジ時間 | 5,959 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | TLE * 1 -- * 17 |
ソースコード
/*
O(n^2 |s|^2) dp
*/
#include <iostream>
#include <string>
#include <vector>
#include <cassert>
#include <random>
#include <algorithm>
#include <set>
#include <map>
#include <chrono>
using namespace std;
void timer(){
static chrono::steady_clock::time_point start = chrono::steady_clock::now();
auto now = chrono::steady_clock::now();
cerr << "elapsed time : " << chrono::duration_cast<chrono::milliseconds>(now-start).count() << "[ms]" << endl;
}
const long long mod = 1000000007;
long long ans[2000];
long long fact[2001];
long long nCr_[2001][2001];
long long mod_pow(long long x, long long y, long long mod){ //(x^y) % mod
if(x==0 && y!=0) return 0;
long long ret=1LL;
while(y>0LL){
if(y&1LL) ret = (ret * x) % mod;
x = (x*x) % mod;
y >>= 1LL;
}
return ret;
}
long long nCr(long long n, long long r){
if(nCr_[n][r]) return nCr_[n][r];
return nCr_[n][r] = fact[n] * mod_pow(fact[r], mod-2, mod) % mod * mod_pow(fact[n-r], mod-2, mod) % mod;
}
struct trie_node{
map<char, int> child;
int count;
};
vector<trie_node> trie;
// dp[i][x] := i 頂点で合計長 x の組み合わせ
vector<map<long long, long long>> dfs(int pos, int d){
if( trie[pos].count == 1 ){
vector<map<long long, long long>> dp(2);
dp[0][0] = dp[1][1] = 1;
return dp;
}
if( trie[pos].child.size() == 1 ){
return dfs(trie[pos].child.begin()->second, d+1);
}
vector<map<long long, long long>> dp( trie[pos].count+1 );
dp[0][0] = 1;
for(auto p : trie[pos].child){
if( p.first == '}' ) continue;
vector<map<long long, long long>> dp_( trie[pos].count+1 );
int nx = p.second;
auto tmp = dfs(nx, 1);
for(int i=0; i<trie[pos].count; i++){
for(int j=0; j<tmp.size(); j++){
for(auto a : dp[i]){
for(auto b : tmp[j]){
int k = a.first + b.first;
(dp_[i+j][k] += a.second * b.second % mod * nCr(i+j, i) % mod) %= mod;
}
}
}
}
swap(dp, dp_);
}
vector<map<long long,long long>> dp_( trie[pos].count+1 );
dp_[0][0] = 1;
for(int i=1; i<=trie[pos].count; i++){
for(auto p : dp[i]){
dp_[i][ p.first + d * min<int>(trie[pos].count-1, i) ] = p.second;
}
}
return dp_;
}
int main(){
timer();
fact[0] = 1;
for(int i=1; i<=2000; i++){
fact[i] = fact[i-1] * i % mod;
}
int n;
cin >> n;
vector<string> s(n);
for(int i=0; i<n; i++){
cin >> s[i];
s[i] += '{';
}
s.push_back("}");
trie.push_back( trie_node{{}, 0} );
for(int i=0; i<s.size(); i++){
int pos = 0;
for(int j=0; j<s[i].size(); j++){
trie[pos].count++;
if( trie[pos].child.count( s[i][j] ) == 0 ){
trie.push_back( trie_node{ {}, 0} );
trie[pos].child[s[i][j]] = trie.size()-1;
}
pos = trie[pos].child[s[i][j]];
}
trie[pos].count++;
}
auto res = dfs(0, 0);
for(int i=1; i<=n; i++){
for(auto p : res[i]){
(ans[i-1] += p.second * p.first % mod) %= mod;
}
}
for(int i=0; i<n; i++) cout << ans[i]%mod << endl;
timer();
return 0;
}
koyumeishi