結果

問題 No.562 超高速一人かるた small
ユーザー snrnsidysnrnsidy
提出日時 2021-11-20 23:25:08
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 624 ms / 3,000 ms
コード長 1,924 bytes
コンパイル時間 2,170 ms
コンパイル使用メモリ 199,344 KB
実行使用メモリ 11,656 KB
最終ジャッジ日時 2023-09-02 22:12:11
合計ジャッジ時間 11,214 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,368 KB
testcase_01 AC 1 ms
4,372 KB
testcase_02 AC 2 ms
4,368 KB
testcase_03 AC 2 ms
4,368 KB
testcase_04 AC 1 ms
4,372 KB
testcase_05 AC 2 ms
4,372 KB
testcase_06 AC 1 ms
4,372 KB
testcase_07 AC 5 ms
4,372 KB
testcase_08 AC 66 ms
4,372 KB
testcase_09 AC 623 ms
11,564 KB
testcase_10 AC 138 ms
5,608 KB
testcase_11 AC 291 ms
7,548 KB
testcase_12 AC 65 ms
4,372 KB
testcase_13 AC 3 ms
4,368 KB
testcase_14 AC 622 ms
11,616 KB
testcase_15 AC 620 ms
11,464 KB
testcase_16 AC 621 ms
11,656 KB
testcase_17 AC 621 ms
11,496 KB
testcase_18 AC 624 ms
11,568 KB
testcase_19 AC 624 ms
11,548 KB
testcase_20 AC 622 ms
11,496 KB
testcase_21 AC 622 ms
11,552 KB
testcase_22 AC 622 ms
11,452 KB
testcase_23 AC 623 ms
11,636 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

const long long int MOD = 1e9 + 7;
long long int dp[1<<20];
int n;
string s[20];
long long int cost[20][20];
long long int f[21];
long long int res[21];

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

    cin >> n;

    for(int i=0;i<n;i++)
    {
        cin >> s[i];
    }

    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            int idx = 0;
            if(i==j)
            {
                cost[i][j] = 1;
                continue;
            }
            while(1)
            {
                if(idx >= s[i].length() || idx >= s[j].length())
                {
                    break;
                }
                if(s[i][idx]!=s[j][idx]) break;
                idx++;
            }
            cost[i][j] = idx + 1;
        }
    }

    f[0] = 1;
    for(int i=1;i<=20;i++)
    {
        f[i] = f[i-1];
        f[i]%=MOD;
        f[i]*=i;
        f[i]%=MOD;
    }

    for(int i=0;i<(1<<n);i++)
    {
        int cnt = 0;
        for(int j=0;j<n;j++)
        {
            if((i&(1<<j))) cnt++;
        }
        for(int j=0;j<n;j++)
        {
            if((i&(1<<j))) continue;
            long long int MAX = 0;
            for(int k=0;k<n;k++)
            {
                if((i&(1<<k))) continue;
                MAX = max(MAX,cost[j][k]);
            }
            int bitmask = i + (1<<j);
            long long int val = MAX*f[cnt] + dp[i];
            val%=MOD;
            //cout << bitmask << ' ' << val << ' ' << i << ' ' << j << '\n';
            dp[bitmask] += val;
            dp[bitmask]%=MOD;
        }
    }

    for(int i=0;i<(1<<n);i++)
    {
        int cnt = 0;
        for(int j=0;j<n;j++)
        {
            if((i&(1<<j))) cnt++;
        }
        res[cnt] += dp[i];
        res[cnt]%=MOD;
    }

    for(int i=1;i<=n;i++)
    {
        cout << res[i] << '\n';
    }

    return 0;
}
0