結果

問題 No.2181 LRM Question 2
ユーザー 👑 rin204rin204
提出日時 2023-01-06 23:52:06
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 4,019 bytes
コンパイル時間 442 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 82,312 KB
最終ジャッジ日時 2024-05-07 23:51:27
合計ジャッジ時間 9,453 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
59,116 KB
testcase_01 AC 35 ms
53,872 KB
testcase_02 AC 1,933 ms
78,128 KB
testcase_03 AC 36 ms
53,248 KB
testcase_04 AC 45 ms
63,744 KB
testcase_05 AC 40 ms
59,392 KB
testcase_06 AC 52 ms
63,360 KB
testcase_07 AC 38 ms
53,504 KB
testcase_08 TLE -
testcase_09 AC 615 ms
77,440 KB
testcase_10 TLE -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

from math import gcd


def isprime(n):
    if n <= 1:
        return False
    elif n == 2:
        return True
    elif n % 2 == 0:
        return False

    A = [2, 325, 9375, 28178, 450775, 9780504, 1795265022]
    s = 0
    d = n - 1
    while d % 2 == 0:
        s += 1
        d >>= 1

    for a in A:
        if a % n == 0:
            return True
        x = pow(a, d, n)
        if x != 1:
            for t in range(s):
                if x == n - 1:
                    break
                x = x * x % n
            else:
                return False
    return True


def pollard(n):
    if n % 2 == 0:
        return 2
    if isprime(n):
        return n

    f = lambda x: (x * x + 1) % n

    step = 0
    while 1:
        step += 1
        x = step
        y = f(x)
        while 1:
            p = gcd(y - x + n, n)
            if p == 0 or p == n:
                break
            if p != 1:
                return p
            x = f(x)
            y = f(f(y))


def primefact(n):
    if n == 1:
        return []
    p = pollard(n)
    if p == n:
        return [p]
    left = primefact(p)
    right = primefact(n // p)
    left += right
    return sorted(left)


def modinv(a, MOD):
    b = MOD
    u = 1
    v = 0
    while b:
        t = a // b
        a -= t * b
        u -= t * v
        a, b = b, a
        u, v = v, u
    u %= MOD
    return u


def Garner(M, R):
    if not M:
        return 0
    m_prod = M[0]
    C = R[0]
    for m, r in zip(M[1:], R[1:]):
        t = (r - C) * modinv(m_prod, m) % m
        C += t * m_prod
        m_prod *= m
    return C


class Combination_Arbitrary_sub:
    def __init__(self, p, pq):
        self.fact = [0] * (pq + 1)
        self.invfact = [0] * (pq + 1)
        self.fact[0] = 1
        self.invfact[0] = 1
        for i in range(1, pq + 1):
            if i % p == 0:
                self.fact[i] = self.fact[i - 1]
            else:
                self.fact[i] = self.fact[i - 1] * i % pq
            self.invfact[i] = modinv(self.fact[i], pq)


class Combination_Arbitrary:
    def __init__(self, MOD):
        self.MOD = MOD
        primes = primefact(MOD)
        self.le = len(set(primes))
        self.p = sorted(set(primes))
        self.q = [0] * self.le
        self.pq = [1] * self.le
        ind = -1
        bef = -1
        for p in primes:
            if p != bef:
                bef = p
                ind += 1
            self.q[ind] += 1
            self.pq[ind] *= p
        self.fac = [None] * self.le
        for i, (p_, pq_) in enumerate(zip(self.p, self.pq)):
            self.fac[i] = Combination_Arbitrary_sub(p_, pq_)

    def C(self, n, k, p, q, pq, fac):
        z = n - k

        e0 = 0
        u = n // p
        while u > 0:
            e0 += u
            u //= p
        u = k // p
        while u > 0:
            e0 -= u
            u //= p
        u = z // p
        while u > 0:
            e0 -= u
            u //= p

        em = 0
        u = n // pq
        while u > 0:
            em += u
            u //= p
        u = k // pq
        while u > 0:
            em -= u
            u //= p
        u = z // pq
        while u > 0:
            em -= u
            u //= p

        ret = 1
        while n > 0:
            ret *= fac.fact[n % pq]
            ret %= pq
            ret *= fac.invfact[k % pq]
            ret %= pq
            ret *= fac.invfact[z % pq]
            ret %= pq
            n //= p
            k //= p
            z //= p
        ret *= pow(p, e0, pq)
        ret %= pq
        if not (p == 2 and q >= 3) and em & 1:
            ret = -ret % pq
        return ret

    def nCk(self, n, k):
        if n < k or k < 0:
            return 0

        R = [0] * self.le
        for i in range(self.le):
            R[i] = self.C(n, k, self.p[i], self.q[i], self.pq[i], self.fac[i])
        return Garner(self.pq, R)

l, r, m = map(int, input().split())
C = Combination_Arbitrary(m)
ans = 0
for i in range(l, r + 1):
    ans += C.nCk(2 * i, i) - 2
    ans %= m

print(ans)
0