結果

問題 No.109 N! mod M
ユーザー shotoyooshotoyoo
提出日時 2021-06-14 23:38:00
言語 PyPy3
(7.3.13)
結果
WA  
実行時間 -
コード長 1,184 bytes
コンパイル時間 810 ms
コンパイル使用メモリ 87,208 KB
実行使用メモリ 76,532 KB
最終ジャッジ日時 2023-08-26 13:19:50
合計ジャッジ時間 5,905 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 68 ms
75,548 KB
testcase_01 AC 267 ms
75,568 KB
testcase_02 AC 171 ms
75,484 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = lambda : sys.stdin.readline().rstrip()

sys.setrecursionlimit(2*10**5+10)
write = lambda x: sys.stdout.write(x+"\n")
debug = lambda x: sys.stderr.write(x+"\n")
writef = lambda x: print("{:.12f}".format(x))

def factor(n, m=None):
    # mを与えると、高々その素因数まで見て、残りは分解せずにそのまま出力する
    f = {}
    tmp = n
    M = int(-(-n**0.5//1))+1
    if m is not None:
        M = min(m+1, M)
    for i in range(2, M+1):
        if tmp<i:
            break
        if tmp%i==0:
            cnt=0
            while tmp%i==0:
                cnt+=1
                tmp //= i
            f[i] = cnt
    if tmp!=1:
        f[tmp] = 1
    if not f:
        f[n] = 1
    return f
def sub(n,m):
    if n<2*10**6:
        ans = 1
        for i in range(1,n+1):
            ans *= i
            ans %= m
    else:
        f = factor(m)
        if len(f)>=2:
            ans = 0
        else:
            ans = -1
            for i in range(n,m-1)[::-1]:
                ans *= pow(i,m-2,m)
                ans %= m
    return ans%m
t = int(input())
for i in range(t):
    n,m = list(map(int, input().split()))
    print(sub(n,m))
0