結果

問題 No.562 超高速一人かるた small
ユーザー mkawa2mkawa2
提出日時 2021-11-12 18:44:47
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,992 ms / 3,000 ms
コード長 2,431 bytes
コンパイル時間 208 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 99,840 KB
最終ジャッジ日時 2024-05-04 01:38:25
合計ジャッジ時間 23,256 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 52 ms
75,776 KB
testcase_01 AC 49 ms
75,904 KB
testcase_02 AC 51 ms
75,904 KB
testcase_03 AC 51 ms
76,032 KB
testcase_04 AC 63 ms
82,688 KB
testcase_05 AC 67 ms
84,864 KB
testcase_06 AC 52 ms
75,648 KB
testcase_07 AC 87 ms
91,776 KB
testcase_08 AC 288 ms
92,544 KB
testcase_09 AC 1,766 ms
99,772 KB
testcase_10 AC 488 ms
93,568 KB
testcase_11 AC 932 ms
95,488 KB
testcase_12 AC 254 ms
92,440 KB
testcase_13 AC 65 ms
83,712 KB
testcase_14 AC 1,725 ms
99,712 KB
testcase_15 AC 1,809 ms
99,800 KB
testcase_16 AC 1,992 ms
99,840 KB
testcase_17 AC 1,694 ms
99,620 KB
testcase_18 AC 1,735 ms
99,840 KB
testcase_19 AC 1,758 ms
99,712 KB
testcase_20 AC 1,769 ms
99,716 KB
testcase_21 AC 1,725 ms
99,660 KB
testcase_22 AC 1,917 ms
99,840 KB
testcase_23 AC 1,834 ms
99,728 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(200005)
int1 = lambda x: int(x)-1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
# inf = 18446744073709551615
inf = 4294967295
md = 10**9+7
# md = 998244353

def add(s, i):
    u = 0
    for c in s:
        if c not in trie[u]:
            trie[u][c] = len(trie)
            trie.append({})
            vv.append(0)
        u = trie[u][c]
        vv[u] |= 1 << i

    trie[u]["end"] = s

def search(s):
    bb, dd = [], []
    u = 0
    d = 0
    now = -1
    for c in s:
        u = trie[u][c]
        d += 1
        if vv[u] != now:
            now = vv[u]
            bb.append(now)
            dd.append(d)
    return bb, dd

def nHr(hn, hr):
    return nCr(hn+hr-1, hr-1)

def nPr(com_n, com_r):
    if com_r < 0: return 0
    if com_n < com_r: return 0
    return fac[com_n]*ifac[com_n-com_r]%md

def nCr(com_n, com_r):
    if com_r < 0: return 0
    if com_n < com_r: return 0
    return fac[com_n]*ifac[com_r]%md*ifac[com_n-com_r]%md

# 準備
n_max = 200005
fac = [1]
for i in range(1, n_max+1): fac.append(fac[-1]*i%md)
ifac = [1]*(n_max+1)
ifac[n_max] = pow(fac[n_max], md-2, md)
for i in range(n_max-1, 1, -1): ifac[i] = ifac[i+1]*(i+1)%md

trie = [{}]
vv = [0]

n = II()
ss = [SI()+"@" for _ in range(n)]

for i, s in enumerate(ss): add(s, i)
bbs, dds = [], []
for i, s in enumerate(ss):
    bb, dd = search(s)
    bbs.append(bb)
    dds.append(dd)

popcnt = lambda x: bin(x).count("1")

dp = [0]*(1 << n)
ans = [0]*(n+1)
for bit in range((1 << n)-1)[::-1]:
    k = n-popcnt(bit)
    for i in range(n):
        if bit >> i & 1: continue
        nbit = bit | 1 << i
        cost = 0
        for b, d in zip(bbs[i], dds[i]):
            if bit & b == 0:
                cost = d
                break
        dp[bit] += dp[nbit]+cost
        dp[bit] %= md
    dp[bit] = dp[bit]*pow(k, md-2, md)%md
    ans[k] += dp[bit]
    ans[k] %= md

for k in range(1, n+1):
    ans[k] *= fac[k]
    ans[k] %= md

print(*ans[1:], sep="\n")
0