結果

問題 No.1252 数字根D
ユーザー uni_pythonuni_python
提出日時 2020-10-09 23:07:13
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 127 ms / 2,000 ms
コード長 1,166 bytes
コンパイル時間 285 ms
コンパイル使用メモリ 87,056 KB
実行使用メモリ 77,700 KB
最終ジャッジ日時 2023-09-27 19:31:04
合計ジャッジ時間 2,992 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
71,100 KB
testcase_01 AC 71 ms
71,112 KB
testcase_02 AC 72 ms
71,200 KB
testcase_03 AC 127 ms
75,752 KB
testcase_04 AC 108 ms
77,376 KB
testcase_05 AC 105 ms
77,356 KB
testcase_06 AC 104 ms
77,412 KB
testcase_07 AC 105 ms
77,560 KB
testcase_08 AC 102 ms
77,420 KB
testcase_09 AC 106 ms
77,700 KB
testcase_10 AC 106 ms
77,540 KB
testcase_11 AC 105 ms
77,500 KB
testcase_12 AC 108 ms
77,700 KB
testcase_13 AC 105 ms
77,676 KB
testcase_14 AC 72 ms
71,144 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input=sys.stdin.readline
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    T=I()
    
    def calc_d(x,d):#xのd進数字和
        temp=0
        while x!=0:
            temp+=x%d
            x=x//d
        return temp
    
    def calc(x,d):
        dif=1
        while dif!=0:
           nxt=calc_d(x,d)
           dif=x-nxt
           x=nxt
        return nxt 
    
    for _ in range(T):
        d,A,B=MI()
        a=calc(A,d)
        n=B-A+1#個数
        
        #桁和は0にならない
        S=((d-1)*d)//2#1周期分
        
        rem=d-a#周期を合わせるための最初のずれのこすう
        if n<=rem:
            ans=(((a+n-1)+a)*n)//2
            print(ans)
        else:
            aaa=(((a+rem-1)+a)*rem)//2#最初のずれ分の和
            n=n-rem
            loopn=n//(d-1)
            rem=n%(d-1)
            
            bbb=loopn*S#ループ分
            ccc=((rem+1)*rem)//2#残り
            
            ans=aaa+bbb+ccc
            print(ans)
            
            # print(aaa,bbb,ccc)


main()
0