結果
| 問題 | 
                            No.3118 Increment or Multiply
                             | 
                    
| コンテスト | |
| ユーザー | 
                             | 
                    
| 提出日時 | 2025-04-19 08:12:53 | 
| 言語 | PyPy3  (7.3.15)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 139 ms / 2,000 ms | 
| コード長 | 355 bytes | 
| コンパイル時間 | 721 ms | 
| コンパイル使用メモリ | 82,332 KB | 
| 実行使用メモリ | 77,440 KB | 
| 最終ジャッジ日時 | 2025-04-19 08:13:00 | 
| 合計ジャッジ時間 | 6,378 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge1 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 1 | 
| other | AC * 35 | 
ソースコード
MOD=998244353
T=int(input())
for _ in range(T):
    N,A=map(int,input().split())
    if A==1:
        print((N*(N-1)//2)%MOD)
        continue
    ans=0
    now=0
    pos=N
    while True:
        lw=pos//A
        rng=pos-lw
        ans+=rng*now+rng*(rng-1)//2
        now+=1+(pos%A)
        pos=lw
        if pos==0:
            break
    print(ans%MOD)