#P10049. [CCPC 2023 北京市赛] 报数 IV

[CCPC 2023 北京市赛] 报数 IV

Background

This is the fourth time that Little R and Little Z have played the counting-off game, so they decided not to write a long problem statement anymore. You only need to know that they came up with another strange counting rule, and they want to figure out which numbers can be called out.

Problem Description

For any positive integer nn, define the function f(n)f(n) as the sum of all digits of nn in decimal. For example, f(114514)=1+1+4+5+1+4=16f(114514)=1+1+4+5+1+4=16. Clearly, f(n)f(n) is also a positive integer, so we can consider nested applications such as f(f(n))f(f(n)), f(f(f(n)))f(f(f(n))), and so on. Furthermore, for positive integers n,kn,k, define g(n,k)=f(f(...f(n)))g(n,k)=f(f(...f(n))) (with a total of kk layers of ff).

To make the counting-off game different each round, Little R and Little Z decide to set two positive integers k,mk,m for each round, and specify that: in this round, all positive integers nn satisfying g(n,k)=mg(n,k)=m cannot be called out.

Since both of them are skilled players, to prevent the game from going on forever, each round also gives a positive integer NN as the upper bound of counting off. They want to know: among the positive integers not exceeding NN, how many cannot be called out under this rule.

Input Format

The first line contains a positive integer TT, indicating the number of game rounds played by Little R and Little Z. It is guaranteed that 1T51 \le T \le 5.

The next TT lines each contain three positive integers N,k,mN,k,m, describing one round of the game as stated above. It is guaranteed that 1N1010001 \le N \le 10^{1000}, 1k,m1091 \le k,m \le 10^9.

Output Format

Output TT lines. Each line contains one integer, representing the number of integers that cannot be called out in this round, modulo 109+710^9+7.

2
114 1 5
514 2 10
8
10

Hint

In the first round, the numbers that cannot be called out are 5,14,23,32,41,50,104,1135,14,23,32,41,50,104,113.

Translated by ChatGPT 5