結果

問題 No.109 N! mod M
ユーザー titiatitia
提出日時 2022-01-26 01:31:47
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 516 bytes
コンパイル時間 117 ms
コンパイル使用メモリ 10,964 KB
実行使用メモリ 12,836 KB
最終ジャッジ日時 2023-08-22 21:35:49
合計ジャッジ時間 7,738 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
7,912 KB
testcase_01 AC 296 ms
7,852 KB
testcase_02 AC 346 ms
7,788 KB
testcase_03 AC 16 ms
7,792 KB
testcase_04 AC 244 ms
7,860 KB
testcase_05 TLE -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

# 解説を見た。ウィルソンの定理は知っておくべきか。

def calc(N,M):
    if M<=N:
        return 0
    if M<=2*10**5:
        ANS=1
        for i in range(1,N+1):
            ANS=ANS*i%M
        return ANS%M

    x=int(M**(1/2))

    for j in range(2,x+1):
        if M%j==0:
            return 0

    ANS=-1
    for j in range(N+1,M):
        ANS=ANS*pow(j,M-2,M)%M
    return ANS%M
    
    
    


T=int(input())
for tests in range(T):
    N,M=map(int,input().split())

    print(calc(N,M))
0