結果
問題 | No.2487 Multiple of M |
ユーザー |
![]() |
提出日時 | 2023-09-29 23:10:57 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 41 ms / 2,000 ms |
コード長 | 751 bytes |
コンパイル時間 | 216 ms |
コンパイル使用メモリ | 82,240 KB |
実行使用メモリ | 54,068 KB |
最終ジャッジ日時 | 2024-07-23 06:50:44 |
合計ジャッジ時間 | 3,905 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 53 |
ソースコード
from math import gcdimport sysinput = sys.stdin.readlinemod = 998244353N, M, K = map(int, input().split())def powsum(a, n):# sum(a**i for i in range(n)) % modif n == 1:return 1x = powsum(a**2 % mod, n//2)res = x + a * xif n % 2 == 1:res += pow(a, n-1, mod)return res % moddef f(n, i):# use n terms to make a multiple of M/gcd(M,K**i)g = gcd(M, K**i)if n == 0:return 1if n == 1:return g-1if i > 30:g = gcd(M, K**30)if n % 2 == 0:return (-g * powsum(1-M, n) + 1) % modelse:return (-g * (M-1) * powsum(1-M, n-1) + g - 1) % modreturn (pow(M-1, n-1, mod)*g - f(n-1, i+1)) % modprint(f(N, 0))