結果
| 問題 | No.1252 数字根D | 
| コンテスト | |
| ユーザー |  ygd. | 
| 提出日時 | 2020-10-18 11:06:56 | 
| 言語 | PyPy3 (7.3.15) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 114 ms / 2,000 ms | 
| コード長 | 604 bytes | 
| コンパイル時間 | 423 ms | 
| コンパイル使用メモリ | 82,248 KB | 
| 実行使用メモリ | 77,024 KB | 
| 最終ジャッジ日時 | 2024-07-21 03:20:37 | 
| 合計ジャッジ時間 | 2,660 ms | 
| ジャッジサーバーID (参考情報) | judge1 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 1 | 
| other | AC * 14 | 
ソースコード
T = int(input())
for case in range(T):
  d,A,B = map(int,input().split())
  if A == B == 0:
    print(0);continue
  if A == 0:
    A += 1
  oriA = A+0
  a = 0
  while A > 0:
    a += A%d
    A = A//d
    if A == 0 and a >= d:
      A = a
      a = 0
  #print(a)
  num = B-oriA+1
  loop_num = num//(d-1)
  amari = num%(d-1)
  loop_wa = (1+d-1)*(d-1)//2
  if a + amari-1 >= d: #先頭に戻る
    amari_wa = loop_wa - ((a-1) + (a-(d-1-amari)))*(d-1-amari)//2
  else:
    amari_wa = (a + a + amari-1)*amari//2
  ans = loop_num*loop_wa + amari_wa
  #print(loop_num,amari,loop_wa,amari_wa)
  print(ans)
    
            
            
            
        