問題一覧 > 通常問題

No.1252 Digital Root D

レベル : / AI推定難易度 : 1125 / 実行時間制限 : 1ケース 2.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (解説公開後に AC するまで非表示) / 解いたユーザー数 137
作問者 : 👑 Kazun / テスター : maspy
お気に入りにしたユーザー ProblemId : 4692 / 自分の提出
問題文最終更新日: 2020-10-09 20:06:06
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
yukicoder contest 269 (MATHEMATICS 1) (順位表) の他の問題:

Note

In this problem, unless explicitly stated otherwise, all numbers are written in decimal notation, and the input is also given in decimal notation. Also, print the output in decimal notation regardless of the value of $d$ in each test case.

Problem Statement

The base-$d$ digit sum $f(N)$ of a non-negative integer $N$ is defined as $\displaystyle f(N)=\sum_{j=0}^M a_j$, using a non-negative integer $M$ and $M+1$ integers $a_0,\dots,a_M$ between $0$ and $d-1$, inclusive, that satisfy $\displaystyle N=\sum_{j=0}^M a_j d^j$.

Next, for non-negative integers $m,N$, define $f^m(N)$ as follows: $\quad f^m(N)=\begin{cases} f(f^{m-1}(N)) & (m \geq 1) \\ N & (m=0) \end{cases}$

Also, the base-$d$ digital root $N^*$ of a non-negative integer $N$ is defined as $\displaystyle N^*=\lim_{m \to \infty} f^m(N)$. Now, for non-negative integers $A,B~(A \leq B)$, find $\displaystyle \sum_{k=A}^B k^*$.

It can be shown that the digital root is uniquely determined for every non-negative integer.

Solve $T$ test cases.

Example

Let us find the base-$10$ digital root of $N=20201009$.
  • $f^0(N)=N=20201009$
  • $f^1(N)=f(f^0(N))=f(N)=2+0+2+0+1+0+0+9=14$
  • $f^2(N)=f(f^1(N))=f(f(N))=f(14)=1+4=5$
  • $f^3(N)=f(f^2(N))=f(5)=5$
  • $\vdots$
Thus, we can see that $N^*=5$.

Constraints

  • $1 \leq T \leq 2000$
  • $2 \leq d \leq 10^9$
  • $0 \leq A \leq B \leq 10^9$
  • $T,d,A,B$ are all integers.

Input

The input is given from standard input in the following format. The first line is as follows.
$T$
The following $T$ lines then represent the $T$ test cases. Each is a line in the following format.
$d~A~B$

Output

Print a newline after the output of each test case.

Sample

Sample 1
Input
3
10 99 101
9 9 18
16 12345 67890
Output
12
39
444375

[About the first test case]
Since $99^*=9,100^*=1,101^*=2$, the answer is $9+1+2=12$.

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。