結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
59,796 KB
testcase_01 AC 46 ms
59,796 KB
testcase_02 AC 46 ms
59,796 KB
testcase_03 AC 45 ms
59,796 KB
testcase_04 AC 45 ms
59,796 KB
testcase_05 AC 46 ms
59,796 KB
testcase_06 AC 45 ms
59,796 KB
testcase_07 AC 46 ms
59,796 KB
testcase_08 AC 215 ms
118,772 KB
testcase_09 AC 325 ms
125,640 KB
testcase_10 AC 139 ms
112,672 KB
testcase_11 AC 216 ms
118,780 KB
testcase_12 AC 212 ms
119,160 KB
testcase_13 AC 140 ms
112,520 KB
testcase_14 AC 216 ms
119,352 KB
testcase_15 AC 222 ms
120,364 KB
testcase_16 AC 231 ms
124,104 KB
testcase_17 AC 232 ms
125,828 KB
testcase_18 AC 214 ms
119,344 KB
testcase_19 AC 196 ms
119,136 KB
testcase_20 AC 306 ms
125,960 KB
testcase_21 AC 305 ms
126,160 KB
testcase_22 AC 243 ms
122,288 KB
testcase_23 AC 251 ms
119,428 KB
testcase_24 AC 193 ms
119,908 KB
testcase_25 AC 194 ms
117,188 KB
testcase_26 AC 197 ms
117,192 KB
testcase_27 AC 54 ms
66,760 KB
testcase_28 AC 82 ms
84,056 KB
testcase_29 AC 128 ms
99,916 KB
testcase_30 AC 117 ms
98,248 KB
testcase_31 AC 85 ms
86,248 KB
testcase_32 AC 91 ms
89,392 KB
testcase_33 AC 66 ms
74,848 KB
testcase_34 AC 150 ms
100,180 KB
testcase_35 AC 115 ms
98,508 KB
testcase_36 AC 192 ms
113,072 KB
testcase_37 AC 197 ms
113,420 KB
testcase_38 AC 78 ms
82,080 KB
testcase_39 AC 178 ms
108,828 KB
testcase_40 AC 54 ms
66,760 KB
testcase_41 AC 108 ms
96,928 KB
testcase_42 AC 79 ms
82,364 KB
testcase_43 AC 153 ms
99,616 KB
testcase_44 AC 207 ms
117,916 KB
testcase_45 AC 143 ms
98,068 KB
testcase_46 AC 53 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(200000, 300000)
mod2 = RandomMod(7*10**8, 10**9)
b2 = random.randrange(200000, 300000)
 
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