結果

問題 No.562 超高速一人かるた small
ユーザー mkawa2mkawa2
提出日時 2022-01-23 16:07:52
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,181 ms / 3,000 ms
コード長 3,662 bytes
コンパイル時間 496 ms
コンパイル使用メモリ 82,056 KB
実行使用メモリ 85,604 KB
最終ジャッジ日時 2024-05-07 04:20:48
合計ジャッジ時間 16,562 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
55,696 KB
testcase_01 AC 43 ms
55,272 KB
testcase_02 AC 45 ms
56,648 KB
testcase_03 AC 42 ms
55,700 KB
testcase_04 AC 60 ms
68,748 KB
testcase_05 AC 69 ms
72,824 KB
testcase_06 AC 42 ms
56,272 KB
testcase_07 AC 84 ms
77,508 KB
testcase_08 AC 193 ms
78,232 KB
testcase_09 AC 1,172 ms
85,452 KB
testcase_10 AC 349 ms
79,376 KB
testcase_11 AC 625 ms
80,980 KB
testcase_12 AC 200 ms
77,992 KB
testcase_13 AC 75 ms
76,792 KB
testcase_14 AC 1,139 ms
85,560 KB
testcase_15 AC 1,169 ms
85,244 KB
testcase_16 AC 1,071 ms
85,444 KB
testcase_17 AC 1,146 ms
85,260 KB
testcase_18 AC 1,149 ms
85,204 KB
testcase_19 AC 1,160 ms
85,420 KB
testcase_20 AC 1,181 ms
85,604 KB
testcase_21 AC 1,177 ms
85,112 KB
testcase_22 AC 1,153 ms
85,448 KB
testcase_23 AC 1,146 ms
85,456 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(200005)
int1 = lambda x: int(x)-1
pDB = lambda *x: print(*x, end="\n", file=sys.stderr)
p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr)
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 = (1 << 63)-1
# inf = (1<<31)-1
md = 10**9+7
# md = 998244353

from random import randrange

class RollingHash:
    def __init__(self, target, BASE):
        self.BASE = BASE
        self.MOD = (1 << 61)-1
        self.N = len(target)
        self.table = [0]
        self.power = [1]
        for c in target:
            self.table += [self._mod(self.table[-1]*self.BASE)+ord(c)]
            self.power += [self._mod(self.power[-1]*self.BASE)]

    def _mod(self, a):
        a = (a >> 61)+(a & self.MOD)
        if a >= self.MOD: return a-self.MOD
        return a

    def hashing(self, s):
        h = 0
        for c in s:
            h = self._mod(h*self.BASE)+ord(c)
        return self._mod(h)

    def hash_lr(self, l, r):
        return self._mod(self.table[r]-self._mod(self.table[l]*self.power[r-l]))

    def find(self, s, start=0):
        h = self.hashing(s)
        for i in range(start, self.N-len(s)+1):
            if self.hash_lr(i, i+len(s)) == h:
                return i
        return -1

    def find_all(self, s):
        res = []
        h = self.hashing(s)
        for i in range(self.N-len(s)+1):
            if self.hash_lr(i, i+len(s)) == h:
                res.append(i)
        return res

def binary_search(l, r, ok, minimize):
    if minimize: l -= 1
    else: r += 1
    while l+1 < r:
        m = (l+r)//2
        if ok(m) ^ minimize: l = m
        else: r = m
    if minimize: return r
    return l

def ok(m):
    return hh[i].hash_lr(0, m) != hh[j].hash_lr(0, m)

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 = 100
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

def popcnt(a): return bin(a).count("1")

# from time import time

# tt = []

# tt.append(time())

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

# tt.append(time())

BASE = randrange(128, 256)
hh = [RollingHash(s, BASE) for s in ss]

# tt.append(time())

cost = [[0]*n for _ in range(n)]
for i in range(n):
    for j in range(i):
        cost[i][j] = cost[j][i] = binary_search(1, min(len(ss[i]), len(ss[j])), ok, True)

# tt.append(time())

ans = [0]*(n+1)
dp = [0]*(1 << n)
for s in range(1 << n):
    pc = popcnt(s)
    ans[pc] += dp[s]
    ans[pc] %= md
    dd = [i for i in range(n) if s >> i & 1 == 0]
    for i in range(len(dd)):
        d = dd[i]
        ns = s | 1 << d
        c = 0
        if i: c = cost[d][dd[i-1]]
        if i+1 < len(dd): c = max(c, cost[d][dd[i+1]])
        dp[ns] += (dp[s]+fac[pc]*c%md)%md
        dp[ns] %= md

# tt.append(time())

ans[-1] += fac[n]
ans[-1] %= md

for a in ans[1:]: print(a)

# for t0, t1 in zip(tt, tt[1:]):
#     print(t1-t0)
0