結果

問題 No.2254 Reverse Only
ユーザー titiatitia
提出日時 2023-04-09 02:27:01
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,056 ms / 2,000 ms
コード長 1,535 bytes
コンパイル時間 137 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 57,208 KB
最終ジャッジ日時 2024-04-14 21:07:39
合計ジャッジ時間 17,233 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
11,008 KB
testcase_01 AC 30 ms
11,008 KB
testcase_02 AC 30 ms
11,008 KB
testcase_03 AC 29 ms
10,752 KB
testcase_04 AC 30 ms
10,880 KB
testcase_05 AC 30 ms
10,880 KB
testcase_06 AC 30 ms
11,008 KB
testcase_07 AC 30 ms
10,752 KB
testcase_08 AC 281 ms
38,404 KB
testcase_09 AC 1,035 ms
54,756 KB
testcase_10 AC 163 ms
38,536 KB
testcase_11 AC 273 ms
38,536 KB
testcase_12 AC 263 ms
38,384 KB
testcase_13 AC 276 ms
38,528 KB
testcase_14 AC 272 ms
38,404 KB
testcase_15 AC 279 ms
38,408 KB
testcase_16 AC 1,043 ms
54,656 KB
testcase_17 AC 1,056 ms
54,652 KB
testcase_18 AC 263 ms
38,408 KB
testcase_19 AC 227 ms
38,664 KB
testcase_20 AC 1,009 ms
54,768 KB
testcase_21 AC 1,017 ms
54,896 KB
testcase_22 AC 842 ms
46,752 KB
testcase_23 AC 939 ms
54,784 KB
testcase_24 AC 932 ms
57,208 KB
testcase_25 AC 953 ms
54,772 KB
testcase_26 AC 934 ms
54,804 KB
testcase_27 AC 36 ms
11,648 KB
testcase_28 AC 76 ms
17,488 KB
testcase_29 AC 136 ms
23,468 KB
testcase_30 AC 120 ms
21,868 KB
testcase_31 AC 79 ms
17,864 KB
testcase_32 AC 86 ms
18,328 KB
testcase_33 AC 55 ms
14,444 KB
testcase_34 AC 172 ms
27,152 KB
testcase_35 AC 115 ms
21,484 KB
testcase_36 AC 237 ms
35,396 KB
testcase_37 AC 251 ms
35,104 KB
testcase_38 AC 69 ms
16,328 KB
testcase_39 AC 224 ms
32,480 KB
testcase_40 AC 35 ms
11,648 KB
testcase_41 AC 107 ms
20,824 KB
testcase_42 AC 70 ms
16,748 KB
testcase_43 AC 179 ms
29,496 KB
testcase_44 AC 259 ms
36,604 KB
testcase_45 AC 165 ms
26,916 KB
testcase_46 AC 34 ms
11,520 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

N,K=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))

if sorted(A)!=sorted(B):
    print("No")
    exit()

if K==N:
    if A==B or A==B[::-1]:
        print("Yes")
    else:
        print("No")
    exit()

elif K>N:
    if A==B:
        print("Yes")
    else:
        print("No")
    exit()

elif K<N-1:
    print("Yes")
    exit()

# Rolling Hash

LEN=N

p=200000 # 文字の種類
mod=67280421310721 # Hashがぶつからない, pと互いに素な数を適当に指定
INV=pow(p,mod-2,mod)
kake=pow(p,LEN-1,mod)

score=0
for i in range(N):
    score=(score*p%mod+B[i])%mod

TABLE=[0] # Rolling Hashのテーブル. 最初は0

for i in range(N):
    TABLE.append((p*TABLE[-1]%mod+A[i])%mod) # テーブルを埋める

def hash_ij(i,j): # [i,j)のハッシュ値を求める
    return (TABLE[j]-TABLE[i]*pow(p,j-i,mod))%mod

LIST=[TABLE[-1]]

now=TABLE[-1]

for i in range(N-1,-1,-1):
    a=A[i]
    now-=a

    now=now*INV%mod

    now=(now+a*kake)%mod

    LIST.append(now)

TABLE=[0] # Rolling Hashのテーブル. 最初は0
A=A[::-1]

for i in range(N):
    TABLE.append((p*TABLE[-1]%mod+A[i])%mod) # テーブルを埋める

def hash_ij(i,j): # [i,j)のハッシュ値を求める
    return (TABLE[j]-TABLE[i]*pow(p,j-i,mod))%mod

LIST.append(TABLE[-1])

now=TABLE[-1]
for i in range(N-1,-1,-1):
    a=A[i]
    now-=a
    now=now*INV%mod
    now=(now+a*kake)%mod
    LIST.append(now)

if score in LIST:
    print("Yes")
else:
    print("No")
0