結果

問題 No.2120 場合の数の下8桁
ユーザー とりゐとりゐ
提出日時 2022-11-04 21:36:15
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 110 ms / 2,000 ms
コード長 2,731 bytes
コンパイル時間 742 ms
コンパイル使用メモリ 86,800 KB
実行使用メモリ 81,924 KB
最終ジャッジ日時 2023-09-25 23:51:46
合計ジャッジ時間 3,613 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 105 ms
81,924 KB
testcase_01 AC 106 ms
81,332 KB
testcase_02 AC 106 ms
81,816 KB
testcase_03 AC 106 ms
81,600 KB
testcase_04 AC 105 ms
81,452 KB
testcase_05 AC 106 ms
81,344 KB
testcase_06 AC 106 ms
81,500 KB
testcase_07 AC 104 ms
81,456 KB
testcase_08 AC 108 ms
81,580 KB
testcase_09 AC 106 ms
81,592 KB
testcase_10 AC 107 ms
81,604 KB
testcase_11 AC 110 ms
81,592 KB
testcase_12 AC 109 ms
81,520 KB
testcase_13 AC 108 ms
81,856 KB
testcase_14 AC 105 ms
81,924 KB
testcase_15 AC 108 ms
81,812 KB
testcase_16 AC 103 ms
81,636 KB
testcase_17 AC 106 ms
81,304 KB
testcase_18 AC 105 ms
81,768 KB
testcase_19 AC 110 ms
81,452 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class BinomialCoefficient:
    def __init__(self, m):
        self.MOD = m
        self.factorization = self._factorize(m)
        self.facs = []
        self.invs = []
        self.coeffs = []
        self.pows = []
        for p, pe in self.factorization:
            fac = [1]*pe
            for i in range(1, pe):
                fac[i] = fac[i-1]*(i if i % p else 1) % pe
            inv = [1]*pe
            inv[-1] = fac[-1]
            for i in range(1, pe)[::-1]:
                inv[i-1] = inv[i]*(i if i % p else 1) % pe
            self.facs.append(fac)
            self.invs.append(inv)
            # coeffs
            c = self._modinv(m // pe, pe)
            self.coeffs.append(m//pe*c % m)
            # pows
            powp = [1]
            while powp[-1]*p != pe:
                powp.append(powp[-1]*p)
            self.pows.append(powp)

    def __call__(self, n, k):
        if k < 0 or k > n:
            return 0
        if k == 0 or k == n:
            return 1 % self.MOD
        res = 0
        for i, (p, pe) in enumerate(self.factorization):
            res += self._choose_pe(n, k, p, pe,
                                   self.facs[i], self.invs[i], self.pows[i]) * self.coeffs[i]
            res %= self.MOD
        return res

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

    def _choose_pe(self, n, k, p, pe, fac, inv, powp):
        r = n-k
        e0 = self._E(n, k, r, p)
        if e0 >= len(powp):
            return 0
        res = powp[e0]
        if (p != 2 or pe == 4) and self._E(n//(pe//p), k//(pe//p), r//(pe//p), p) % 2:
            res = pe-res
        while n:
            res = res * fac[n % pe] % pe * inv[k % pe] % pe * inv[r % pe] % pe
            n //= p
            k //= p
            r //= p
        return res

    def _factorize(self, N):
        factorization = []
        for i in range(2, N+1):
            if i*i > N:
                break
            if N % i:
                continue
            c = 0
            while N % i == 0:
                N //= i
                c += 1
            factorization.append((i, i**c))
        if N != 1:
            factorization.append((N, N))
        return factorization

    def _modinv(self, a, MOD):
        r0, r1, s0, s1 = a, MOD, 1, 0
        while r1:
            r0, r1, s0, s1 = r1, r0 % r1, s1, s0-r0//r1*s1
        return s0 % MOD

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

P=5**8
c=BinomialCoefficient(P)
ansP=c(m,n)

Q=2**8
c=BinomialCoefficient(Q)
ansQ=c(m,n)

ans=ansP
while ans%Q!=ansQ:
  ans+=5**8
  ans%=10**8

if ans==0:
  print('0'*8)
else:
  ans=str(ans)
  print('0'*(8-len(ans))+ans)
0