結果

問題 No.2181 LRM Question 2
ユーザー MasKoaTSMasKoaTS
提出日時 2022-10-19 20:47:00
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,982 ms / 2,000 ms
コード長 2,395 bytes
コンパイル時間 154 ms
コンパイル使用メモリ 82,060 KB
実行使用メモリ 382,496 KB
最終ジャッジ日時 2024-06-29 21:43:32
合計ジャッジ時間 15,870 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
52,352 KB
testcase_01 AC 38 ms
52,608 KB
testcase_02 AC 1,411 ms
297,048 KB
testcase_03 AC 37 ms
52,608 KB
testcase_04 AC 173 ms
110,768 KB
testcase_05 AC 46 ms
62,208 KB
testcase_06 AC 50 ms
63,744 KB
testcase_07 AC 41 ms
58,112 KB
testcase_08 AC 1,731 ms
381,664 KB
testcase_09 AC 1,244 ms
297,872 KB
testcase_10 AC 1,982 ms
379,480 KB
testcase_11 AC 1,952 ms
382,496 KB
testcase_12 AC 1,123 ms
129,016 KB
testcase_13 AC 60 ms
67,584 KB
testcase_14 AC 686 ms
304,156 KB
testcase_15 AC 210 ms
122,784 KB
testcase_16 AC 511 ms
253,084 KB
testcase_17 AC 670 ms
279,572 KB
testcase_18 AC 452 ms
219,088 KB
testcase_19 AC 397 ms
200,516 KB
testcase_20 AC 746 ms
294,528 KB
testcase_21 AC 795 ms
275,456 KB
testcase_22 AC 305 ms
150,520 KB
testcase_23 AC 39 ms
52,608 KB
testcase_24 AC 39 ms
52,608 KB
testcase_25 AC 39 ms
52,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

class BinomialCoefficient:
	def __init__(self, mod):
		self.mod = mod
		self.prime = self.prime_factorize(mod)
		self.facs = []
		self.invs = []
		self.pows = []
		self.mods = {}
		for p, c in self.prime:
			pc = pow(p, c)
			fac = [1] * pc
			inv = [1] * pc
			for i in range(1, pc):
				k = i
				if(i % p == 0):
					k = 1
				fac[i] = fac[i - 1] * k % pc
			inv[-1] = fac[-1]
			for i in range(pc - 1, 0, -1):
				k = i
				if(i % p == 0):
					k = 1
				inv[i - 1] = inv[i] * k % pc
			self.facs.append(fac)
			self.invs.append(inv)
			pw = [1]
			while(pw[-1] * p != pc):
				pw.append(pw[-1] * p)
			self.pows.append(pw)
		r = [0] * len(self.prime)
		pcs = [pow(p, c) for p, c in self.prime]
		self.mods[tuple(r)] = 0
		for i in range(1, mod):
			for j, pc in enumerate(pcs):
				r[j] += 1
				if(r[j] >= pc):
					r[j] -= pc
			self.mods[tuple(r)] = i

	def prime_factorize(self, n):
		prime = []
		f = 2
		while(f * f <= n):
			if(n % f == 0):
				n //= f
				cnt = 1
				while(n % f == 0):
					n //= f
					cnt += 1
				prime.append((f, cnt))
			f += 1
		if(n != 1):
			prime.append((n, 1))
		return prime

	def inv_gcd(self, n, m):
		n %= m
		if(n == 0):
			return m, 0
		s, t, m0, m1 = m, n, 0, 1
		while(t):
			u = s // t
			s -= t * u
			m0 -= m1 * u
			m0, m1, s, t = m1, m0, t, s
		if(m0 < 0):
			m0 += m // s
		return s, m0

	def inv_mod(self, n, m):
		g, im = self.inv_gcd(n, m)
		return im

	def calc_e(self, n, k, r, p):
		e = 0
		while(n):
			n //= p
			e += n
		while(k):
			k //= p
			e -= k
		while(r):
			r //= p
			e -= r
		return e

	def lucas(self, n, k, p, c, i):
		pw = self.pows[i]
		fac = self.facs[i]
		inv = self.invs[i]
		r = n - k
		pc = pow(p, c)
		e = self.calc_e(n, k, r, p)
		if(e >= len(pw)):
			return 0
		ret = pw[e]
		if((p != 2 or c < 3) and (self.calc_e(n // pw[-1], k // pw[-1], r // pw[-1], p) & 1)):
			ret *= -1
		while(n):
			ret *= fac[n % pc] * inv[k % pc] * inv[r % pc]
			ret %= pc
			n //= p
			k //= p
			r //= p
		return ret

	def __call__(self, n, k):
		if(k < 0 or k > n):
			return 0
		if(k == 0 or k == n):
			return 1
		r = [self.lucas(n, k, p, c, i) for i, (p, c) in enumerate(self.prime)]
		return self.mods[tuple(r)]


"""
Main Code
"""

l, r, m = map(int, input().split())

nCk = BinomialCoefficient(m)
ans = 0
for i in range(l, r + 1):
	ans += nCk(2 * i, i) + m - 2
	ans %= m
print(ans)
0