結果

問題 No.563 超高速一人かるた large
ユーザー koyumeishikoyumeishi
提出日時 2017-06-18 15:16:08
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 3,065 bytes
コンパイル時間 1,678 ms
コンパイル使用メモリ 123,740 KB
実行使用メモリ 14,016 KB
最終ジャッジ日時 2024-04-09 19:47:06
合計ジャッジ時間 6,009 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
13,880 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 TLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

/*
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;
}
0