結果

問題 No.2181 LRM Question 2
ユーザー shobonvipshobonvip
提出日時 2023-01-07 00:28:41
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,433 bytes
コンパイル時間 301 ms
コンパイル使用メモリ 87,248 KB
実行使用メモリ 103,592 KB
最終ジャッジ日時 2023-08-20 17:50:25
合計ジャッジ時間 8,836 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 172 ms
80,536 KB
testcase_01 AC 173 ms
80,600 KB
testcase_02 AC 432 ms
95,172 KB
testcase_03 AC 173 ms
80,252 KB
testcase_04 AC 180 ms
81,200 KB
testcase_05 AC 177 ms
80,660 KB
testcase_06 AC 181 ms
80,868 KB
testcase_07 AC 175 ms
80,484 KB
testcase_08 AC 624 ms
103,040 KB
testcase_09 AC 394 ms
94,372 KB
testcase_10 AC 629 ms
103,112 KB
testcase_11 AC 639 ms
103,592 KB
testcase_12 AC 622 ms
102,744 KB
testcase_13 AC 191 ms
82,264 KB
testcase_14 WA -
testcase_15 AC 184 ms
81,676 KB
testcase_16 AC 255 ms
90,976 KB
testcase_17 AC 239 ms
83,552 KB
testcase_18 WA -
testcase_19 AC 204 ms
82,696 KB
testcase_20 AC 210 ms
84,196 KB
testcase_21 AC 271 ms
85,484 KB
testcase_22 WA -
testcase_23 AC 177 ms
80,616 KB
testcase_24 WA -
testcase_25 AC 176 ms
80,660 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import typing

def inv_gcd(a: int, b: int) -> typing.Tuple[int, int]:
	a %= b
	if a == 0:
		return (b, 0)
	s = b
	t = a
	m0 = 0
	m1 = 1
	while t:
		u = s // t
		s -= t * u
		m0 -= m1 * u
		s, t = t, s
		m0, m1 = m1, m0
	if m0 < 0:
		m0 += b // s
	return (s, m0)

def inv_mod(x: int, m: int) -> int:
	z = inv_gcd(x, m)
	return z[1]

def crt(r: typing.List[int], m: typing.List[int]) -> typing.Tuple[int, int]:
	r0 = 0
	m0 = 1
	for r1, m1 in zip(r, m):
		r1 %= m1
		if m0 < m1:
			r0, r1 = r1, r0
			m0, m1 = m1, m0
		if m0 % m1 == 0:
			if r0 % m1 != r1:
				return (0, 0)
			continue
		g, im = inv_gcd(m0, m1)
		u1 = m1 // g
		if (r1 - r0) % g:
			return (0, 0)
		x = (r1 - r0) // g % u1 * im % u1
		r0 += x * m0
		m0 *= u1
		if r0 < 0:r0 += m0
	return (r0, m0)

def pfact(m):
	pf = {}
	for i in range(2,int(m**0.5)+1):
		while m%i == 0:
			pf[i] = pf.get(i,0) + 1
			m //= i
	if m>1 : pf[m]=1
	return pf

def legendre(n, p):
	ret = 0
	while n > 0:
		n //= p
		ret += n
	return ret

l,r,m = map(int,input().split())
ans = (-2) * (r-l+1) % m
pf = pfact(m)

v = [[] for i in range(r-l+1)]
pl = []

for p, cnt in pf.items():
	mod = p ** cnt
	pl.append(mod)

	euler = (p-1) * p ** (cnt-1)
	vvv = [1] * (euler + 1)
	cc = 1
	ww = 1
	while cc <= euler:
		if ww % p == 0:
			ww += 1
			continue
		vvv[cc] = vvv[cc-1] * ww % mod
		ww += 1
		cc += 1
	
	#print(vvv[-10:])
	
	tmpm = 1
	ggg = 2*l-2
	while ggg > 0:
		#print(ggg)
		tmpm *= pow(vvv[euler], ggg // mod, mod)
		tmpm %= mod
		aim = ggg % mod
		for j in range(aim + 1):
			if j % p == 0:
				continue
			tmpm *= j
			tmpm %= mod
		ggg //= p

	#print()
	#print(tmpm, tmpz)

	tmpx = 1
	ggg = l-1
	while ggg > 0:
		tmpx *= pow(vvv[euler], ggg // mod, mod)
		tmpx %= mod
		aim = ggg % mod
		for j in range(aim + 1):
			if j % p == 0:
				continue
			tmpx *= j
			tmpx %= mod
		ggg //= p

	tmpz = inv_mod(tmpx, mod)
	tmpm *= tmpx * tmpx % mod
	tmpm %= mod
	
	tmpc = legendre(2*(l-1), p) - 2 * legendre(l-1, p)

	for i in range(l, r + 1):
		tt = i
		while tt % p == 0:
			tmpc -= 2
			tt //= p
		tmpv = inv_mod(tt, mod)
		tmpm *= tmpv * tmpv
		tmpm %= mod

		tt = 2 * i - 1
		while tt % p == 0:
			tmpc += 1
			tt //= p
		tmpm *= tt
		tmpm %= mod

		tt = 2 * i
		while tt % p == 0:
			tmpc += 1
			tt //= p
		tmpm *= tt
		tmpm %= mod

		if tmpc < cnt:
			v[i-l].append(tmpm * p ** tmpc % mod)
		else:
			v[i-l].append(0)

for i in range(r-l+1):
	ans += crt(v[i], pl)[0]
	ans %= m

print(ans)
0