結果

問題 No.109 N! mod M
ユーザー ayaoniayaoni
提出日時 2020-12-14 21:04:20
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 981 bytes
コンパイル時間 307 ms
コンパイル使用メモリ 81,692 KB
実行使用メモリ 62,164 KB
最終ジャッジ日時 2023-10-20 05:13:17
合計ジャッジ時間 5,302 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
59,580 KB
testcase_01 AC 139 ms
59,580 KB
testcase_02 AC 165 ms
59,584 KB
testcase_03 AC 40 ms
53,416 KB
testcase_04 AC 94 ms
59,952 KB
testcase_05 AC 3,509 ms
62,164 KB
testcase_06 AC 63 ms
59,760 KB
testcase_07 WA -
testcase_08 AC 43 ms
59,732 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def LS2(): return list(sys.stdin.readline().rstrip())


for _ in range(I()):
    N,M = MI()
    if N >= M:
        print(0)
    elif N <= 2*10**5:
        ans = 1
        for i in range(1,N+1):
            ans *= i
            ans %= M
        print(ans)
    else:
        r = 1
        for i in range(2,int(M**.5)+1):
            if M % i == 0:
                r = 0
                break
        if r == 1:
            ans = M-1
            for i in range(N+1,M):
                ans *= pow(i,M-2,M)
                ans %= M
            print(ans)
        else:
            print(0)
0