結果

問題 No.502 階乗を計算するだけ
ユーザー srup٩(๑`н´๑)۶srup٩(๑`н´๑)۶
提出日時 2017-04-07 23:34:55
言語 Python2
(2.7.18)
結果
MLE  
実行時間 -
コード長 788 bytes
コンパイル時間 594 ms
コンパイル使用メモリ 6,564 KB
実行使用メモリ 814,624 KB
最終ジャッジ日時 2023-09-23 02:53:00
合計ジャッジ時間 3,719 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
5,960 KB
testcase_01 AC 11 ms
5,948 KB
testcase_02 AC 11 ms
5,888 KB
testcase_03 AC 11 ms
5,940 KB
testcase_04 AC 11 ms
5,888 KB
testcase_05 AC 11 ms
5,916 KB
testcase_06 AC 11 ms
5,948 KB
testcase_07 AC 11 ms
5,840 KB
testcase_08 AC 11 ms
5,968 KB
testcase_09 AC 11 ms
5,844 KB
testcase_10 AC 12 ms
5,832 KB
testcase_11 AC 12 ms
5,956 KB
testcase_12 AC 11 ms
5,916 KB
testcase_13 AC 11 ms
5,848 KB
testcase_14 AC 11 ms
5,832 KB
testcase_15 AC 11 ms
5,828 KB
testcase_16 AC 11 ms
5,956 KB
testcase_17 AC 11 ms
5,940 KB
testcase_18 AC 11 ms
5,948 KB
testcase_19 AC 11 ms
5,864 KB
testcase_20 AC 12 ms
6,040 KB
testcase_21 AC 12 ms
5,896 KB
testcase_22 AC 104 ms
35,884 KB
testcase_23 AC 37 ms
13,888 KB
testcase_24 AC 73 ms
25,408 KB
testcase_25 AC 19 ms
8,124 KB
testcase_26 AC 47 ms
16,792 KB
testcase_27 AC 33 ms
12,224 KB
testcase_28 AC 40 ms
14,912 KB
testcase_29 AC 25 ms
9,616 KB
testcase_30 AC 96 ms
33,568 KB
testcase_31 AC 56 ms
18,956 KB
testcase_32 MLE -
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 #

def egcd(a, b):
    if a == 0:
        return (b, 0, 1)
    else:
        g, y, x = egcd(b % a, a)
        return (g, x - (b // a) * y, y)

def modinv(a, m):
    g, x, y = egcd(a, m)
    if g != 1:
        raise Exception('modular inverse does not exist')
    else:
        return x % m

def factorialMod(n, modulus):
    ans=1
    if n <= modulus//2:
        #calculate the factorial normally (right argument of range() is exclusive)
        for i in range(1,n+1):
            ans = (ans * i) % modulus   
    else:
        #Fancypants method for large n
        for i in range(n+1,modulus):
            ans = (ans * i) % modulus
        ans = modinv(ans, modulus)
        ans = -1*ans + modulus
    return ans % modulus

mod = 1000000007;
k = raw_input()
print factorialMod(int(k), mod)
0