結果

問題 No.129 お年玉(2)
ユーザー 👑 rin204rin204
提出日時 2022-07-04 13:58:33
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 962 ms / 5,000 ms
コード長 4,010 bytes
コンパイル時間 444 ms
コンパイル使用メモリ 86,760 KB
実行使用メモリ 107,300 KB
最終ジャッジ日時 2023-08-20 20:48:10
合計ジャッジ時間 43,111 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 858 ms
107,008 KB
testcase_01 AC 837 ms
106,872 KB
testcase_02 AC 855 ms
107,060 KB
testcase_03 AC 828 ms
107,112 KB
testcase_04 AC 871 ms
107,036 KB
testcase_05 AC 890 ms
107,112 KB
testcase_06 AC 886 ms
107,112 KB
testcase_07 AC 888 ms
107,132 KB
testcase_08 AC 885 ms
107,220 KB
testcase_09 AC 786 ms
107,300 KB
testcase_10 AC 786 ms
107,056 KB
testcase_11 AC 818 ms
107,228 KB
testcase_12 AC 818 ms
107,044 KB
testcase_13 AC 802 ms
107,060 KB
testcase_14 AC 814 ms
107,164 KB
testcase_15 AC 809 ms
107,056 KB
testcase_16 AC 790 ms
107,220 KB
testcase_17 AC 797 ms
107,036 KB
testcase_18 AC 781 ms
107,172 KB
testcase_19 AC 810 ms
107,064 KB
testcase_20 AC 878 ms
107,152 KB
testcase_21 AC 881 ms
106,868 KB
testcase_22 AC 812 ms
107,220 KB
testcase_23 AC 824 ms
106,932 KB
testcase_24 AC 802 ms
107,132 KB
testcase_25 AC 752 ms
107,116 KB
testcase_26 AC 752 ms
107,024 KB
testcase_27 AC 735 ms
107,044 KB
testcase_28 AC 734 ms
107,224 KB
testcase_29 AC 741 ms
107,220 KB
testcase_30 AC 736 ms
107,116 KB
testcase_31 AC 740 ms
107,004 KB
testcase_32 AC 962 ms
107,224 KB
testcase_33 AC 739 ms
107,208 KB
testcase_34 AC 742 ms
107,052 KB
testcase_35 AC 732 ms
107,012 KB
testcase_36 AC 735 ms
107,056 KB
testcase_37 AC 734 ms
107,008 KB
testcase_38 AC 730 ms
107,056 KB
testcase_39 AC 731 ms
107,184 KB
testcase_40 AC 733 ms
107,012 KB
testcase_41 AC 732 ms
107,056 KB
testcase_42 AC 734 ms
107,132 KB
testcase_43 AC 739 ms
107,004 KB
testcase_44 AC 739 ms
107,168 KB
testcase_45 AC 726 ms
107,036 KB
testcase_46 AC 733 ms
107,100 KB
testcase_47 AC 734 ms
107,112 KB
testcase_48 AC 743 ms
107,144 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

n = int(input())
m = int(input())

n //= 1000
n %= m

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_Arbitary_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_Arbitary:
    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_Arbitary_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)

Comb = Combination_Arbitary(10 ** 9)
print(Comb.nCk(m, n))
0