結果
問題 |
No.502 階乗を計算するだけ
|
ユーザー |
![]() |
提出日時 | 2017-04-07 23:34:55 |
言語 | Python2 (2.7.18) |
結果 |
MLE
|
実行時間 | - |
コード長 | 788 bytes |
コンパイル時間 | 219 ms |
コンパイル使用メモリ | 6,944 KB |
実行使用メモリ | 814,968 KB |
最終ジャッジ日時 | 2024-07-16 03:13:35 |
合計ジャッジ時間 | 2,716 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 32 MLE * 1 -- * 19 |
ソースコード
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)