結果

問題 No.3118 Increment or Multiply
ユーザー detteiuu
提出日時 2025-10-10 22:20:55
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 734 bytes
コンパイル時間 412 ms
コンパイル使用メモリ 82,228 KB
実行使用メモリ 78,272 KB
最終ジャッジ日時 2025-10-10 22:21:02
合計ジャッジ時間 6,631 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 5 WA * 30
権限があれば一括ダウンロードができます

ソースコード

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
    
    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