結果

問題 No.3118 Increment or Multiply
ユーザー detteiuu
提出日時 2025-10-10 22:23:58
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 180 ms / 2,000 ms
コード長 750 bytes
コンパイル時間 419 ms
コンパイル使用メモリ 82,712 KB
実行使用メモリ 77,916 KB
最終ジャッジ日時 2025-10-10 22:24:06
合計ジャッジ時間 7,037 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #

def inverse(n, d):
    return n * pow(d, -1, MOD) % MOD

def rangeSUM(l, r, c):
    return (l+r)%MOD*c%MOD*half%MOD

MOD = 998244353
half = inverse(1, 2)

for _ in range(int(input())):
    N, A = map(int, input().split())

    if A == 1:
        if 2 <= N:
            print(rangeSUM(1, N-1, N-1))
        else:
            print(0)
        continue

    B = []
    n = N
    while n:
        B.append(n%A)
        n //= A
    B = B[::-1]
    
    ans = 0
    pre = 0
    SUM = sum(B)
    now = 0
    for i, a in enumerate(B):
        now = now*A+a
        cnt = now-pre
        SUM -= a
        ans += rangeSUM(1, cnt-1, cnt-1)
        ans %= MOD
        ans += (SUM+(len(B)-1-i))%MOD*cnt%MOD
        ans %= MOD
        pre = now
    
    print(ans)
0