No.3118 Increment or Multiply
Problem Statement
You are given positive integers $N,A$. For a positive integer $K$ less than or equal to $N$, consider the following "problem" and let $f(K)$ be its answer.
Problem
There is a variable $C$, initially $C = K$. In $1$ operation, you can either add $1$ to $C$ or multiply it by $A$.
Find the minimum number of operations required to make $C = N$.
Find the sum of $f(K)$ over all positive integers $K$ less than or equal to $N$, that is, the value of $\displaystyle \sum_{i = 1}^{N}f(i)$, modulo $998244353$.
There are $T$ test cases in total.
Constraints
- $1 \leq T \leq 10^{4}$
- $1 \leq N \leq 10^{18}$
- $1 \leq A \leq 10^{18}$
Input
The input is given from standard input in the following format.$T$
$\text{case}_{1}$
$\text{case}_2$
$\ldots$
$\text{case}_{T}$
Here, $\text{case}_i$ denotes the $i$-th test case.
Each test case is given in the following format.
$N\ A$
Output
Print $T$ lines. On the $j (1 \leq j \leq T)$-th line, print the value of $\displaystyle \sum _{i = 1}^{N}f(i)$ for $\text{case}_j$ modulo $998244353$, followed by a newline.
Sample
Sample 1
Input
3 5 2 1 1 271828459045235602 314159265358979323
Output
8 0 487598590
This input contains $3$ test cases. We explain the $1$-st test case.
We have $f(1) = 3$. This is because multiplying $1$ by $2$ $2$ times and then adding $1$ turns $1$ into $5$ in $3$ operations, and it can be proven that $1$ cannot be turned into $5$ in $2$ or fewer operations.
By similar reasoning, we have $f(2) = 2$,$f(3) = 2$,$f(4) = 1$,$f(5) = 0$.
Their sum is $3+2+2+1+0 = 8$, so the answer to the 1st test case is $8$. Therefore, print $8$.
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。