結果

問題 No.2254 Reverse Only
ユーザー shobonvipshobonvip
提出日時 2023-03-10 01:42:20
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 328 ms / 2,000 ms
コード長 1,812 bytes
コンパイル時間 169 ms
コンパイル使用メモリ 81,628 KB
実行使用メモリ 126,156 KB
最終ジャッジ日時 2023-10-18 06:34:32
合計ジャッジ時間 10,422 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
59,816 KB
testcase_01 AC 46 ms
59,816 KB
testcase_02 AC 46 ms
59,816 KB
testcase_03 AC 46 ms
59,816 KB
testcase_04 AC 45 ms
59,816 KB
testcase_05 AC 45 ms
59,816 KB
testcase_06 AC 45 ms
59,816 KB
testcase_07 AC 45 ms
59,816 KB
testcase_08 AC 212 ms
118,768 KB
testcase_09 AC 328 ms
125,640 KB
testcase_10 AC 133 ms
112,668 KB
testcase_11 AC 211 ms
118,780 KB
testcase_12 AC 206 ms
119,120 KB
testcase_13 AC 139 ms
112,544 KB
testcase_14 AC 210 ms
119,360 KB
testcase_15 AC 217 ms
120,368 KB
testcase_16 AC 231 ms
124,104 KB
testcase_17 AC 228 ms
125,820 KB
testcase_18 AC 212 ms
119,356 KB
testcase_19 AC 187 ms
119,132 KB
testcase_20 AC 300 ms
125,960 KB
testcase_21 AC 306 ms
126,156 KB
testcase_22 AC 240 ms
122,288 KB
testcase_23 AC 250 ms
119,432 KB
testcase_24 AC 190 ms
119,908 KB
testcase_25 AC 191 ms
117,204 KB
testcase_26 AC 190 ms
117,200 KB
testcase_27 AC 55 ms
66,760 KB
testcase_28 AC 81 ms
84,096 KB
testcase_29 AC 127 ms
99,920 KB
testcase_30 AC 115 ms
98,248 KB
testcase_31 AC 86 ms
86,248 KB
testcase_32 AC 91 ms
89,412 KB
testcase_33 AC 67 ms
74,848 KB
testcase_34 AC 146 ms
99,656 KB
testcase_35 AC 114 ms
98,512 KB
testcase_36 AC 187 ms
113,064 KB
testcase_37 AC 192 ms
113,412 KB
testcase_38 AC 77 ms
82,080 KB
testcase_39 AC 176 ms
108,836 KB
testcase_40 AC 53 ms
64,712 KB
testcase_41 AC 108 ms
96,928 KB
testcase_42 AC 80 ms
82,364 KB
testcase_43 AC 151 ms
99,648 KB
testcase_44 AC 204 ms
117,908 KB
testcase_45 AC 143 ms
98,864 KB
testcase_46 AC 54 ms
64,712 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import random
 
def isprime(n):
	if n == 1:
		return False
	for i in range(2, int(n**0.5)+1):
		if n % i == 0:
			return False
	return True
 
 
def RandomMod(l,r):
	ret = random.randrange(l, r)
	while not isprime(ret):
		ret = random.randrange(l, r)
	return ret
 
mod1 = RandomMod(7*10**8, 10**9)
b1 = random.randrange(300000, 400000)
mod2 = RandomMod(7*10**8, 10**9)
b2 = random.randrange(300000, 400000)
 
n,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))

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

if a == b:
	print("Yes")
	exit()

if sorted(a) != sorted(b):
	print("No")
	exit()

if k > n:
	print("No")
elif k <= n - 2:
	print("Yes")
elif k == n:
	if a[::-1] == b:
		print("Yes")
	else:
		print("No")
else:
	# k = n-1
	s1 = [0] * (n + 1)
	s2 = [0] * (n + 1)
	x1 = 0
	x2 = 0
	q1 = 0
	q2 = 0
	for i in range(n):
		x1 = (x1 * b1 + a[i]) % mod1
		x2 = (x2 * b2 + a[i]) % mod2
		q1 = (q1 * b1 + b[i]) % mod1
		q2 = (q2 * b2 + b[i]) % mod2
		s1[i+1] = x1
		s2[i+1] = x2
	t1 = [0] * (n + 1)
	t2 = [0] * (n + 1)
	x1 = 0
	x2 = 0
	for i in range(n):
		x1 = (x1 * b1 + a[n-1-i]) % mod1
		x2 = (x2 * b2 + a[n-1-i]) % mod2
		t1[i+1] = x1
		t2[i+1] = x2
	
	# a[:i] + a[i:]
	# s[i] + (s[n] - s[i] * b1l[n-i]) * b1l[i]
	for i in range(n + 1):
		y1 = (s1[i] + (s1[n] - s1[i] * b1l[n-i]) * b1l[i]) % mod1
		y2 = (s2[i] + (s2[n] - s2[i] * b2l[n-i]) * b2l[i]) % mod2
		#print(y1,y2,q1,q2)
		if y1 == q1 and y2 == q2:
			print("Yes")
			exit()
	
	for i in range(n + 1):
		y1 = (t1[i] + (t1[n] - t1[i] * b1l[n-i]) * b1l[i]) % mod1
		y2 = (t2[i] + (t2[n] - t2[i] * b2l[n-i]) * b2l[i]) % mod2
		if y1 == q1 and y2 == q2:
			print("Yes")
			exit()
	
	print("No")	
0