結果

問題 No.599 回文かい
ユーザー mattu34mattu34
提出日時 2023-04-14 12:02:15
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,160 ms / 4,000 ms
コード長 1,253 bytes
コンパイル時間 432 ms
コンパイル使用メモリ 82,148 KB
実行使用メモリ 86,596 KB
最終ジャッジ日時 2024-04-18 11:23:09
合計ジャッジ時間 7,170 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
61,576 KB
testcase_01 AC 44 ms
60,476 KB
testcase_02 AC 43 ms
60,284 KB
testcase_03 AC 41 ms
60,160 KB
testcase_04 AC 47 ms
62,812 KB
testcase_05 AC 51 ms
65,384 KB
testcase_06 AC 42 ms
60,352 KB
testcase_07 AC 44 ms
62,380 KB
testcase_08 AC 43 ms
61,084 KB
testcase_09 AC 50 ms
64,568 KB
testcase_10 AC 52 ms
65,184 KB
testcase_11 AC 49 ms
64,744 KB
testcase_12 AC 51 ms
64,940 KB
testcase_13 AC 55 ms
66,112 KB
testcase_14 AC 228 ms
80,804 KB
testcase_15 AC 63 ms
68,760 KB
testcase_16 AC 1,788 ms
84,452 KB
testcase_17 AC 2,160 ms
86,596 KB
testcase_18 AC 43 ms
60,964 KB
testcase_19 AC 46 ms
60,456 KB
testcase_20 AC 45 ms
61,312 KB
evil_0.txt AC 1,072 ms
79,788 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD=10**9+7
import sys
sys.setrecursionlimit(int(1E+7))
import random

import math

def is_prime(n):
    sqrt_n = math.ceil(math.sqrt(n))
    for i in range(2, sqrt_n):
        if n % i == 0:
            return False
    return True

def RandomMod(l,r):
    while(True):
        res=random.randrange(l,r)
        if is_prime(res):
            return res
dp=[1]*(10**4+1)
vis=[0]*(10**4+1)
def dfs(L,R):
    if R<L:
        return 1
    if vis[L]:
        return dp[L]
    
    l,r=L,R
    while(l<r):
        x1=(s1[l+1]-s1[L]*b1l[l+1-L])%mod1
        x2=(s2[l+1]-s2[L]*b2l[l+1-L])%mod2
        y1=(s1[R+1]-s1[r]*b1l[R+1-r])%mod1
        y2=(s2[R+1]-s2[r]*b2l[R+1-r])%mod2
        if x1==y1 and x2==y2:
            dp[L]+=dfs(l+1,r-1)
            dp[L]%=MOD
        l+=1
        r-=1
    vis[L]=1
    return dp[L]

    
S=input()
L=len(S)
mod1=RandomMod(7*10**7,10**9)
mod2=RandomMod(7*10**7,10**9)
b1=random.randrange(100,200)
b2=random.randrange(100,200)

s1=[0]*(L+1)
s2=[0]*(L+1)

b1l=[1]*(L+1)
b2l=[1]*(L+1)

for i in range(L):
    b1l[i+1]=b1l[i]*b1%mod1
    b2l[i+1]=b2l[i]*b2%mod2

t1=0
t2=0
for i in range(L):
    t1=(t1*b1+(ord(S[i])-ord("a")+1))%mod1
    t2=(t2*b2+(ord(S[i])-ord("a")+1))%mod2
    s1[i+1]=t1
    s2[i+1]=t2

print(dfs(0,L-1))
0