結果

問題 No.3118 Increment or Multiply
ユーザー titia
提出日時 2025-04-20 03:16:19
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 125 ms / 2,000 ms
コード長 968 bytes
コンパイル時間 318 ms
コンパイル使用メモリ 82,296 KB
実行使用メモリ 77,228 KB
最終ジャッジ日時 2025-04-20 03:16:24
合計ジャッジ時間 4,434 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

mod=998244353

def calc(N,A):
    DP=[1<<60]*(N+1)

    DP[N]=0

    for i in range(N,-1,-1):
        if i%A==0:
            DP[i//A]=min(DP[i//A],DP[i]+1)
        if i-1>=0:
            DP[i-1]=min(DP[i-1],DP[i]+1)

    return DP

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

    if A==1:
        print((N*(N-1)//2)%mod)
        continue

    LIST=[(N,0)]

    now=N
    kai=0
    while now>1:
        k=now%A

        LIST.append(((now-k)//A,kai+k+1))
        now=(now-k)//A
        kai+=k+1
        
        
    ANS=0

    #print(LIST)

    for i in range(len(LIST)):
        if i<len(LIST)-1:
            a,b=LIST[i]
            c,d=LIST[i+1]

            ANS+=(b+(b+(a-c-1)))*(a-c)//2
            ANS%=mod

            #print(ANS)
        else:
            a,b=LIST[i]

            if a==1:
                ANS+=b
            else:
                pass

    print(ANS%mod)
        
    
        
0