結果

問題 No.562 超高速一人かるた small
ユーザー nebukuro09nebukuro09
提出日時 2017-08-26 00:32:10
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 328 ms / 3,000 ms
コード長 1,642 bytes
コンパイル時間 1,055 ms
コンパイル使用メモリ 115,980 KB
実行使用メモリ 17,480 KB
最終ジャッジ日時 2023-09-03 16:06:23
合計ジャッジ時間 5,960 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 33 ms
4,380 KB
testcase_09 AC 327 ms
15,968 KB
testcase_10 AC 70 ms
5,664 KB
testcase_11 AC 150 ms
10,544 KB
testcase_12 AC 35 ms
4,380 KB
testcase_13 AC 3 ms
4,380 KB
testcase_14 AC 321 ms
16,220 KB
testcase_15 AC 324 ms
16,164 KB
testcase_16 AC 313 ms
16,992 KB
testcase_17 AC 319 ms
16,184 KB
testcase_18 AC 324 ms
17,268 KB
testcase_19 AC 328 ms
17,480 KB
testcase_20 AC 323 ms
16,732 KB
testcase_21 AC 326 ms
16,684 KB
testcase_22 AC 321 ms
16,212 KB
testcase_23 AC 320 ms
16,476 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop;

immutable long MOD = 10^^9 + 7;

void main() {
    auto N = readln.chomp.to!int;
    auto S = N.iota.map!(_ => readln.chomp).array;
    S.sort();

    auto LCP = new long[][](N, N);
    foreach (i; 0..N) {
        foreach (j; 0..N) {
            for (int a = 0, b = 0; a < S[i].length && b < S[j].length && S[i][a] == S[j][b]; a++, b++)
            LCP[i][j]++;
        }
    }

    auto dp = new long[](1 << N);
    auto B = (1 << N).iota.array;
    B.sort!((a, b) => a.popcnt < b.popcnt);
    auto F = new long[](N+1);
    F[0] = 1;
    foreach (i; 0..N) F[i + 1] = F[i] * (i + 1) % MOD;

    foreach (mask; B) {
        foreach (i; 0..N) {
            if (!(mask & (1 << i))) {
                long tmp = 0;
                long l = i - 1;
                long r = i + 1;
                while (l >= 0 && (mask & (1 << l))) l--;
                while (r <  N && (mask & (1 << r))) r++;
                if (l >= 0) tmp = max(tmp, LCP[i][l]);
                if (r <  N) tmp = max(tmp, LCP[i][r]);
                tmp = tmp + 1;
                (dp[mask | (1 << i)] += dp[mask] + tmp * F[mask.popcnt] % MOD) %= MOD;
            }
        }
    }

    auto ans = new long[](N+1);
    foreach(i; 0..(1<<N)) {
        (ans[i.popcnt] += dp[i]) %= MOD;
    }

    ans[1..N+1].each!writeln;
}

long powmod(long a, long x, long m) {
    long ret = 1;
    while (x) {
        if (x % 2) ret = ret * a % m;
        a = a * a % m;
        x /= 2;
    }
    return ret;
}
0