#P1900. 自我数

自我数

Description

In 1949, Indian mathematician D. R. Daprekar discovered a class of numbers called Self-Numbers. For every positive integer nn, we define d(n)d(n) as nn plus the sum of its digits. For example, d(75)=75+7+5=87d(75) = 75 + 7 + 5 = 87. Given any positive integer nn as a starting point, we can construct an infinite increasing sequence: n,d(n),d(d(n)),d(d(d(n))),n, d(n), d(d(n)), d(d(d(n))), \ldots For example, if you start from 3333, the next number is 33+3+3=3933 + 3 + 3 = 39, the next is 39+3+9=5139 + 3 + 9 = 51, and the one after that is 51+5+1=5751 + 5 + 1 = 57, so the sequence you generate looks like this: $33, 39, 51, 57, 69, 84, 96, 111, 114, 120, 123, 129, 141, \ldots$ The number nn is called a generator of d(n)d(n). In the sequence above, 3333 is a generator of 3939, 3939 is a generator of 5151, 5151 is a generator of 5757, and so on. Some numbers have more than one generator, such as 101101, whose generators can be 9191 and 100100. A number with no generator is called a Self-Number. The first 1313 Self-Numbers are 1,3,5,7,9,20,31,42,53,64,75,86,971, 3, 5, 7, 9, 20, 31, 42, 53, 64, 75, 86, 97. We denote the ii-th Self-Number by aia_i, so a1=1,a2=3,a3=5,a_1 = 1, a_2 = 3, a_3 = 5, \ldots.

Input Format

The input contains integers N,K,s1,,sKN, K, s_1, \ldots, s_K, where 1N1071 \le N \le {10}^7 and 1K50001 \le K \le 5000, separated by spaces and newlines.

Output Format

On the first line, output a single number: the count of Self-Numbers in the closed interval [1,N][1, N]. The second line must contain KK numbers separated by spaces, namely as1,,asKa_{s_1}, \ldots, a_{s_K}. It is guaranteed that all of as1,,asKa_{s_1}, \ldots, a_{s_K} are less than NN. (For example, if N=100N = 100, sks_k can be from 11 to 1313, but not 1414, because a14=108>100a_{14} = 108 > 100.)

100 10
1 2 3 4 5 6 7 11 12 13 

13
1 3 5 7 9 20 31 75 86 97

Hint

For 100%100\% of the testdata, 1N1071 \le N \le {10}^7 and 1K50001 \le K \le 5000.

Translated by ChatGPT 5