結果
問題 |
No.181 A↑↑N mod M
|
ユーザー |
![]() |
提出日時 | 2022-04-09 09:37:52 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,136 bytes |
コンパイル時間 | 453 ms |
コンパイル使用メモリ | 82,688 KB |
実行使用メモリ | 53,120 KB |
最終ジャッジ日時 | 2024-11-29 06:33:57 |
合計ジャッジ時間 | 3,137 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 6 |
other | AC * 28 WA * 9 |
ソースコード
from math import gcd def xgcd(a,b): prevx, nextx = 1, 0 prevy, nexty = 0, 1 while b: quotient = a//b nextx, prevx = prevx - quotient * nextx, nextx nexty, prevy = prevy - quotient * nexty, nexty a, b = b, a % b #print(a,b) return prevx, prevy,a def pfact(m): pf = {} for i in range(2,int(m**0.5)+1): while m%i == 0: pf[i] = pf.get(i,0) + 1 m //= i if m>1 : pf[m]=1 return pf def euler(m): tmp = pfact(m) ans = 1 for i, j in tmp.items(): ans = ans * (i**(j-1)) * (i-1) return ans def perfectdecompose(d, u): for i in range(2,int(d**0.5)+1): while d%i == 0: d//=i while u%i == 0: u//=i if d>1: while u%d == 0: u//=d return u def tetinf(n, u): if u == 1: return 0 d = gcd(n, u) pf = perfectdecompose(d, u) return pow(n, tetinf(n, euler(pf)), pf) * (u//pf) * xgcd(u//pf, pf)[0] % u n,k,m = map(int,input().split()) if k >= 4 and n >= 3: print(tetinf(n, m)) elif n == 2 and k == 4: print(65536 % m) elif k == 3: if n % m == 0 and pow(n, n, m) == 0: print(0) else: print(pow(n, pow(n, n, m), m)) elif k == 2: print(pow(n, n, m)) elif k == 1: print(n % m) else: print(1)