luogu#P12397. 「FAOI-R9」函数大师

    ID: 8587 远端评测题 1500ms 512MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>数论洛谷原创O2优化枚举洛谷月赛

「FAOI-R9」函数大师

Background

As a computer technology master Mingyue likes using Geometer's Sketchpad to draw the graphs of various bizarre functions, like y=xxsinx y=\frac{x^x}{\sin x} , y=xtanx y=\lfloor x^{\tan x} \rfloor , and y=x+x3+x5+x71+x2+x4+x6 y=\frac{x+x^3+x^5+x^7}{1+x^2+x^4+x^6}.

Some of them are continuous, some are discrete, and some have very strange shapes. However, as a math master who scored 99/100 in the high school entrance examination in math, he is confident that he can grasp the laws of many functions.

So, Qingfeng give him such a function.

Problem Description

Qingfeng denotes a function s(x)  (xN)s(x)\;(x \in \mathbb N^*) as the sum of each digit of xx in decimal. Formally, s(x)s(x) is defined as:

$$s(x) = \sum_{i=0} \left( \lfloor \frac{x}{10^i} \rfloor \bmod 10 \right).$$

Consequently, he defines Sk(x)  (xN,kN)S_k(x)\;(x \in \mathbb N^*, k \in \mathbb N) as follows:

$$S_k(x) = \begin{cases} x, & k = 0, \\ s(S_{k-1}(x)), & k > 0. \end{cases}$$

Moreover, Qingfeng defines fk(x)  (xN,kN)f_k(x)\;(x \in \mathbb N^*, k \in \mathbb N) to be i=0kSi(x)\sum_{i=0}^k S_i(x). After giving such a function to Mingyue, who confidently inputs the formula into the Geometer's Sketchpad and is dazzled by the graphics, Mingyue decides to ask you to find out the properties of the function.

To be specific: you are given a constant integer kk. There are multiple queries, in each of them you are given another integer mm, and you have to find out the number of intersection points of the graphics of the functions y=fk(x)y = f_k(x) and y=my = m. It can be proven that the result is always finite under the given constraints.

Input Format

The first line contains two integers TT and kk, denoting the number of queries, and the given constant, respectively.

Then TT lines follow. Each line contains a single integer mm.

Output Format

For each query, output a single line containing an integer: the answer to the corresponding query.

4 3
21
20
19
50
1
1
0
1

Hint

Sample Description

For the first test case: the sets of the x - coordinates of all the intersection points corresponding to each group of data are {12}\{12\}, {5}\{5\}, \varnothing, and {26}\{26\}, respectively.

Constraints

Subtasks are used in this problem.

  • Subtask 1(5 pts): k=0 k=0 .
  • Subtask 2(20 pts): T10 T \le 10 , m105 m \le 10^5 , k10 k \le 10 .
  • Subtask 3(25 pts): T10 T \le 10 , m106 m \le 10^6 , k104 k \le 10^4 .
  • Subtask 4(25 pts): k1 k \le 1 .
  • Subtask 5(25 pts): No additional constraints.

For all test cases, it is guaranteed that 1T1051\le T\le 10^5, 0k1090\le k\le 10^9, 0m10180\le m\le 10^{18}.