結果

問題 No.562 超高速一人かるた small
ユーザー mkawa2mkawa2
提出日時 2022-01-23 15:52:23
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 3,337 bytes
コンパイル時間 214 ms
コンパイル使用メモリ 82,224 KB
実行使用メモリ 89,168 KB
最終ジャッジ日時 2024-05-07 04:04:32
合計ジャッジ時間 6,071 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
56,356 KB
testcase_01 AC 44 ms
56,536 KB
testcase_02 AC 44 ms
56,728 KB
testcase_03 AC 43 ms
56,472 KB
testcase_04 AC 70 ms
75,160 KB
testcase_05 AC 77 ms
77,008 KB
testcase_06 AC 48 ms
55,252 KB
testcase_07 AC 92 ms
77,348 KB
testcase_08 AC 438 ms
79,248 KB
testcase_09 TLE -
testcase_10 AC 943 ms
79,892 KB
testcase_11 AC 1,789 ms
82,308 KB
testcase_12 AC 457 ms
78,452 KB
testcase_13 AC 87 ms
77,796 KB
testcase_14 TLE -
testcase_15 TLE -
testcase_16 TLE -
testcase_17 TLE -
testcase_18 TLE -
testcase_19 TLE -
testcase_20 TLE -
testcase_21 TLE -
testcase_22 TLE -
testcase_23 TLE -
権限があれば一括ダウンロードができます

ソースコード

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")

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

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

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)

ans = [0]*(n+1)
dp = [0]*(1 << n)
for s in range(1 << n):
    pc = popcnt(s)
    ans[pc] += dp[s]
    ans[pc] %= md
    ii = [i for i in range(n) if s >> i & 1 == 0]
    for i in ii:
        dp[s | 1 << i] += dp[s]+fac[pc]*max(cost[i][j] for j in ii)%md
        dp[s | 1 << i] %= md

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

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