結果

問題 No.1252 数字根D
ユーザー proribone
提出日時 2020-10-09 23:09:42
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 142 ms / 2,000 ms
コード長 644 bytes
コンパイル時間 429 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 77,056 KB
最終ジャッジ日時 2024-07-20 14:03:42
合計ジャッジ時間 2,506 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 14
権限があれば一括ダウンロードができます

ソースコード

diff #


def solve():
    def digit(x):
        res=0
        while(x>0):
            res+=x%d
            x//=d
        return res
    
    def f(m,N):
        if m==0:
            return N
        return digit(f(m-1,N))
    
    d,A,B=map(int,input().split())
    if A==0:
        A+=1
    if B==0:
        return 0
    
    Ares,Bres=f(30,A),f(30,B)
    if (B-A+1)<=(d-1) and Ares<Bres:
        return (Ares+Bres)*(Bres-Ares+1)//2
    
    ans=(Ares+d-1)*(d-Ares)//2
    A+=d-Ares
    dif=(B-A)
    roop=dif//(d-1)
    ans+=roop*(d*(d-1)//2)
    ans+=(1+Bres)*(Bres)//2
    return ans
    
    

t=int(input())
for _ in range(t):
    print(solve())
0