結果
問題 | No.109 N! mod M |
ユーザー | vwxyz |
提出日時 | 2021-08-03 03:05:12 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 296 ms / 5,000 ms |
コード長 | 5,179 bytes |
コンパイル時間 | 262 ms |
コンパイル使用メモリ | 82,048 KB |
実行使用メモリ | 90,368 KB |
最終ジャッジ日時 | 2024-09-16 14:49:50 |
合計ジャッジ時間 | 2,823 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 174 ms
89,856 KB |
testcase_01 | AC | 180 ms
89,984 KB |
testcase_02 | AC | 178 ms
89,984 KB |
testcase_03 | AC | 182 ms
89,728 KB |
testcase_04 | AC | 185 ms
90,112 KB |
testcase_05 | AC | 296 ms
90,368 KB |
testcase_06 | AC | 186 ms
89,600 KB |
testcase_07 | AC | 190 ms
89,984 KB |
testcase_08 | AC | 180 ms
89,984 KB |
ソースコード
import bisect import copy import decimal import fractions import heapq import itertools import math import random import sys from collections import Counter,deque,defaultdict from functools import lru_cache,reduce from heapq import heappush,heappop,heapify,heappushpop,_heappop_max,_heapify_max def _heappush_max(heap,item): heap.append(item) heapq._siftdown_max(heap, 0, len(heap)-1) def _heappushpop_max(heap, item): if heap and item < heap[0]: item, heap[0] = heap[0], item heapq._siftup_max(heap, 0) return item from math import gcd as GCD, inf, modf read=sys.stdin.read readline=sys.stdin.readline readlines=sys.stdin.readlines def Extended_Euclid(n,m): stack=[] while m: stack.append((n,m)) n,m=m,n%m if n>=0: x,y=1,0 else: x,y=-1,0 for i in range(len(stack)-1,-1,-1): n,m=stack[i] x,y=y,x-(n//m)*y return x,y class MOD: def __init__(self,mod): self.mod=mod def Pow(self,a,n): a%=self.mod if n>=0: return pow(a,n,self.mod) else: assert math.gcd(a,self.mod)==1 x=Extended_Euclid(a,self.mod)[0] return pow(x,-n,self.mod) def Build_Fact(self,N): assert N>=0 self.factorial=[1] for i in range(1,N+1): self.factorial.append((self.factorial[-1]*i)%self.mod) self.factorial_inv=[None]*(N+1) self.factorial_inv[-1]=self.Pow(self.factorial[-1],-1) for i in range(N-1,-1,-1): self.factorial_inv[i]=(self.factorial_inv[i+1]*(i+1))%self.mod return self.factorial_inv def Fact(self,N): return self.factorial[N] def Fact_Inv(self,N): return self.factorial_inv[N] def Comb(self,N,K): if K<0 or K>N: return 0 s=self.factorial[N] s=(s*self.factorial_inv[K])%self.mod s=(s*self.factorial_inv[N-K])%self.mod return s class Prime: def __init__(self,N): self.smallest_prime_factor=[None]*(N+1) for i in range(2,N+1,2): self.smallest_prime_factor[i]=2 n=int(N**.5)+1 for p in range(3,n,2): if self.smallest_prime_factor[p]==None: self.smallest_prime_factor[p]=p for i in range(p**2,N+1,2*p): if self.smallest_prime_factor[i]==None: self.smallest_prime_factor[i]=p for p in range(n,N+1): if self.smallest_prime_factor[p]==None: self.smallest_prime_factor[p]=p self.primes=[p for p in range(N+1) if p==self.smallest_prime_factor[p]] def Factorize(self,N): assert N>=1 factorize=defaultdict(int) if N<=len(self.smallest_prime_factor)-1: while N!=1: factorize[self.smallest_prime_factor[N]]+=1 N//=self.smallest_prime_factor[N] else: for p in self.primes: while N%p==0: N//=p factorize[p]+=1 if N<p*p: if N!=1: factorize[N]+=1 break if N<=len(self.smallest_prime_factor)-1: while N!=1: factorize[self.smallest_prime_factor[N]]+=1 N//=self.smallest_prime_factor[N] break else: if N!=1: factorize[N]+=1 return factorize def Divisors(self,N): assert N>0 divisors=[1] for p,e in self.Factorize(N).items(): A=[1] for _ in range(e): A.append(A[-1]*p) divisors=[i*j for i in divisors for j in A] return divisors def Is_Prime(self,N): return N==self.smallest_prime_factor[N] def Totient(self,N): for p in self.Factorize(N).keys(): N*=p-1 N//=p return N def CRT(lst_r,lst_m): r,m=lst_r[0],lst_m[0] for r0,m0 in zip(lst_r[1:],lst_m[1:]): if (r0,m0)==(-1,0): r,m=-1,0 break r0%=m0 g=math.gcd(m,m0) l=LCM(m,m0) if r%g!=r0%g: r,m=-1,0 break r,m=(r0+m0*(((r-r0)//g)*Extended_Euclid(m0//g,m//g)[0]%(m//g)))%l,l return r,m def LCM(n,m): if n or m: return abs(n)*abs(m)//math.gcd(n,m) return 0 T=int(readline()) P=Prime(10**5) for _ in range(T): N,M=map(int,readline().split()) lst_r,lst_m=[],[] if M==1: ans=0 else: for p,e in P.Factorize(M).items(): m=p**e if m<=N: r=0 elif e==1: r=1 for i in range(N+1,p): r*=i r%=p r=(-1)*MOD(p).Pow(r,-1)%m else: r=1 for i in range(1,N+1): r*=i r%=m if r==0: break lst_r.append(r) lst_m.append(m) ans,_=CRT(lst_r,lst_m) print(ans)