結果
問題 | No.181 A↑↑N mod M |
ユーザー |
![]() |
提出日時 | 2024-05-03 16:08:48 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 43 ms / 5,000 ms |
コード長 | 1,510 bytes |
コンパイル時間 | 176 ms |
コンパイル使用メモリ | 82,124 KB |
実行使用メモリ | 61,052 KB |
最終ジャッジ日時 | 2024-11-24 18:29:35 |
合計ジャッジ時間 | 3,087 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 6 |
other | AC * 37 |
ソースコード
import sys readline=sys.stdin.readline from collections import defaultdict import math def LCM(n,m): if n or m: return abs(n)*abs(m)//math.gcd(n,m) return 0 def Totient(N,mod): retu=N primes=list(Factorize(mod).keys()) l=len(primes) dp_popcount=[0]*(1<<l) dp_count=[N]*(1<<l) for bit in range(1,1<<l): i=(bit&-bit).bit_length()-1 dp_popcount[bit]=dp_popcount[bit^1<<i]+1 dp_count[bit]=dp_count[bit^1<<i]//primes[i] if dp_popcount[bit]%2==0: retu+=dp_count[bit] else: retu-=dp_count[bit] return retu def Factorize(N): assert N>=1 factors=defaultdict(int) for p in range(2,N): if p**2>N: break while N%p==0: factors[p]+=1 N//=p if N!=1: factors[N]+=1 return factors def Permutation_Product(x,n,f): lst=[] dct={} idx=0 cur=x while True: if cur in dct.keys(): loop_begin=dct[cur] loop_end=idx break else: lst.append(cur) dct[cur]=idx cur=f(cur) idx+=1 if loop_end-1>=n: return lst[n] n-=loop_begin loop_length=loop_end-loop_begin lst=lst[loop_begin:] return lst[n%(loop_length)] A,N,M=map(int,readline().split()) mod=1 MM=M*M while MM>=2: mod=LCM(mod,MM) for p in Factorize(MM): MM=MM*(p-1)//p ans=Permutation_Product(1,N,lambda x:pow(A,x,mod))%M print(ans)