結果

問題 No.2487 Multiple of M
ユーザー sotanishysotanishy
提出日時 2023-09-29 23:10:57
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 78 ms / 2,000 ms
コード長 751 bytes
コンパイル時間 676 ms
コンパイル使用メモリ 86,900 KB
実行使用メモリ 71,704 KB
最終ジャッジ日時 2023-09-30 12:50:14
合計ジャッジ時間 6,434 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
71,320 KB
testcase_01 AC 75 ms
71,680 KB
testcase_02 AC 75 ms
71,408 KB
testcase_03 AC 74 ms
71,212 KB
testcase_04 AC 76 ms
71,212 KB
testcase_05 AC 74 ms
71,444 KB
testcase_06 AC 74 ms
71,316 KB
testcase_07 AC 75 ms
71,672 KB
testcase_08 AC 75 ms
71,416 KB
testcase_09 AC 77 ms
71,136 KB
testcase_10 AC 76 ms
71,524 KB
testcase_11 AC 76 ms
71,424 KB
testcase_12 AC 75 ms
71,644 KB
testcase_13 AC 75 ms
71,540 KB
testcase_14 AC 74 ms
71,376 KB
testcase_15 AC 76 ms
71,356 KB
testcase_16 AC 74 ms
71,704 KB
testcase_17 AC 75 ms
71,424 KB
testcase_18 AC 73 ms
71,640 KB
testcase_19 AC 75 ms
71,420 KB
testcase_20 AC 74 ms
71,584 KB
testcase_21 AC 75 ms
71,392 KB
testcase_22 AC 75 ms
71,648 KB
testcase_23 AC 76 ms
71,360 KB
testcase_24 AC 75 ms
71,336 KB
testcase_25 AC 74 ms
71,344 KB
testcase_26 AC 76 ms
71,468 KB
testcase_27 AC 74 ms
71,620 KB
testcase_28 AC 77 ms
71,212 KB
testcase_29 AC 77 ms
71,384 KB
testcase_30 AC 77 ms
71,180 KB
testcase_31 AC 76 ms
71,460 KB
testcase_32 AC 76 ms
71,584 KB
testcase_33 AC 78 ms
71,264 KB
testcase_34 AC 76 ms
71,360 KB
testcase_35 AC 75 ms
71,636 KB
testcase_36 AC 76 ms
71,212 KB
testcase_37 AC 76 ms
71,704 KB
testcase_38 AC 76 ms
71,388 KB
testcase_39 AC 76 ms
71,140 KB
testcase_40 AC 76 ms
71,328 KB
testcase_41 AC 76 ms
71,392 KB
testcase_42 AC 75 ms
71,652 KB
testcase_43 AC 76 ms
71,424 KB
testcase_44 AC 76 ms
71,276 KB
testcase_45 AC 76 ms
71,528 KB
testcase_46 AC 74 ms
71,252 KB
testcase_47 AC 76 ms
71,640 KB
testcase_48 AC 76 ms
71,668 KB
testcase_49 AC 76 ms
71,516 KB
testcase_50 AC 75 ms
71,272 KB
testcase_51 AC 73 ms
71,316 KB
testcase_52 AC 76 ms
71,680 KB
testcase_53 AC 77 ms
71,464 KB
testcase_54 AC 76 ms
71,336 KB
testcase_55 AC 75 ms
71,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from math import gcd
import sys
input = sys.stdin.readline

mod = 998244353
N, M, K = map(int, input().split())

def powsum(a, n):
    # sum(a**i for i in range(n)) % mod
    if n == 1:
        return 1
    x = powsum(a**2 % mod, n//2)
    res = x + a * x
    if n % 2 == 1:
        res += pow(a, n-1, mod)
    return res % mod


def 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 1
    if n == 1:
        return g-1

    if i > 30:
        g = gcd(M, K**30)
        if n % 2 == 0:
            return (-g * powsum(1-M, n) + 1) % mod
        else:
            return (-g * (M-1) * powsum(1-M, n-1) + g - 1) % mod

    return (pow(M-1, n-1, mod)*g - f(n-1, i+1)) % mod


print(f(N, 0))
0