結果

問題 No.502 階乗を計算するだけ
ユーザー hinamimihinamimi
提出日時 2020-07-24 16:48:11
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 2,773 bytes
コンパイル時間 199 ms
コンパイル使用メモリ 11,128 KB
実行使用メモリ 463,508 KB
最終ジャッジ日時 2023-09-07 19:35:24
合計ジャッジ時間 5,213 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
12,772 KB
testcase_01 AC 17 ms
8,460 KB
testcase_02 AC 17 ms
8,480 KB
testcase_03 AC 16 ms
8,456 KB
testcase_04 AC 17 ms
8,460 KB
testcase_05 AC 16 ms
8,456 KB
testcase_06 AC 16 ms
8,356 KB
testcase_07 AC 16 ms
8,404 KB
testcase_08 AC 17 ms
8,256 KB
testcase_09 AC 17 ms
8,388 KB
testcase_10 AC 17 ms
8,500 KB
testcase_11 AC 16 ms
8,364 KB
testcase_12 AC 17 ms
8,448 KB
testcase_13 AC 16 ms
8,452 KB
testcase_14 AC 16 ms
8,460 KB
testcase_15 AC 16 ms
8,308 KB
testcase_16 AC 16 ms
8,448 KB
testcase_17 AC 16 ms
8,352 KB
testcase_18 AC 16 ms
8,452 KB
testcase_19 AC 17 ms
8,344 KB
testcase_20 AC 16 ms
8,380 KB
testcase_21 AC 16 ms
8,412 KB
testcase_22 AC 189 ms
45,252 KB
testcase_23 AC 64 ms
18,296 KB
testcase_24 AC 132 ms
32,760 KB
testcase_25 AC 30 ms
11,408 KB
testcase_26 AC 82 ms
22,112 KB
testcase_27 AC 56 ms
16,636 KB
testcase_28 AC 71 ms
19,892 KB
testcase_29 AC 41 ms
13,312 KB
testcase_30 AC 179 ms
42,584 KB
testcase_31 AC 95 ms
24,676 KB
testcase_32 TLE -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

class Factorial():
    def __init__(self, mod=10**9 + 7):
        self.mod = mod
        self._factorial = [1]
        self._size = 1
        self._factorial_inv = [1]
        self._size_inv = 1

    def __call__(self, n):
        return self.fact(n)

    def fact(self, n):
        ''' n! % mod '''
        if n >= self.mod:
            return 0
        self._make(n)
        return self._factorial[n]
    
    def _make(self, n):
        if n >= self.mod:
            n = self.mod
        if self._size < n+1:
            for i in range(self._size, n+1):
                self._factorial.append(self._factorial[i-1]*i % self.mod)
            self._size = n+1

    def fact_inv(self, n):
        ''' n!^-1 % mod '''
        if n >= self.mod:
            raise ValueError('Modinv is not exist! arg={}'.format(n))
        if self._size_inv < n+1:
            self._factorial_inv += [-1] * (n+1-self._size_inv)
            self._size_inv = n+1
        if self._factorial_inv[n] == -1:
            self._factorial_inv[n] = self.modinv(self.fact(n))
        return self._factorial_inv[n]
    
    def _make_inv(self, n, r=2):
        if n >= self.mod:
            n = self.mod - 1
        if self._size_inv < n+1:
            self._factorial_inv += [-1] * (n+1-self._size_inv)
            self._size_inv = n+1
        self._factorial_inv[n] = self.modinv(self.fact(n))
        for i in range(n, r, -1):
            self._factorial_inv[i-1] = self._factorial_inv[i]*i % self.mod
    
    @staticmethod
    def xgcd(a, b):
        '''
        Return (gcd(a, b), x, y) such that a*x + b*y = gcd(a, b)
        '''
        x0, x1, y0, y1 = 0, 1, 1, 0
        while a != 0:
            (q, a), b = divmod(b, a), a
            y0, y1 = y1, y0 - q * y1
            x0, x1 = x1, x0 - q * x1
        return b, x0, y0

    def modinv(self, n):
        g, x, _ = self.xgcd(n, self.mod)
        if g != 1:
            raise ValueError('Modinv is not exist! arg={}'.format(n))
        return x % self.mod

    def comb(self, n, r):
        ''' nCr % mod '''
        if r > n:
            return 0
        t = self(n)*self.fact_inv(n-r) % self.mod
        return t*self.fact_inv(r) % self.mod
    
    def comb_(self, n, r):
        '''
        nCr % mod
        when r is not large and n is too large
        '''
        c = 1
        for i in range(1, r+1):
            c *= (n-i+1) * self.fact_inv(i)
            c %= self.mod
        return c

    def comb_with_repetition(self, n, r):
        ''' nHr % mod '''
        t = self(n+r-1)*self.fact_inv(n-1) % self.mod
        return t*self.fact_inv(r) % self.mod

    def perm(self, n, r):
        ''' nPr % mod '''
        if r > n:
            return 0
        return self(n)*self.fact_inv(n-r) % self.mod

print(Factorial()(int(input())))
0