<|instruction|>
Solve the below problem. The solution should start with an abridged problem statement. Then key observations. Then full solution based on the observations. Then C++ and Python implementations with comments.

108. Self-numbers 2

time limit per test: 0.5 sec.
memory limit per test: 4096 KB


In 1949 the Indian mathematician D.R. Kaprekar discovered a class of numbers called self-numbers. For any positive integer n, define d(n) to be n plus the sum of the digits of n. (The d stands for digitadition, a term coined by Kaprekar.) For example, d(75) = 75 + 7 + 5 = 87. Given any positive integer n as a starting point, you can construct the infinite increasing sequence of integers n, d(n), d(d(n)), d(d(d(n))), .... For example, if you start with 33, the next number is 33 + 3 + 3 = 39, the next is 39 + 3 + 9 = 51, the next is 51 + 5 + 1 = 57, and so you generate the sequence 33, 39, 51, 57, 69, 84, 96, 111, 114, 120, 123, 129, 141, ... The number n is called a generator of d(n). In the sequence above, 33 is a generator of 39, 39 is a generator of 51, 51 is a generator of 57, and so on. Some numbers have more than one generator: for example, 101 has two generators, 91 and 100. A number with no generators is a self-number. Let the a[i] will be i-th self-number. There are thirteen self-numbers a[1]..a[13] less than 100: 1, 3, 5, 7, 9, 20, 31, 42, 53, 64, 75, 86, and 97. (the first self-number is a[1]=1, the second is a[2] = 3, :, the thirteen is a[13]=97);


Input

Input contains integer numbers N, K, s1...sk. (1<=N<=107, 1<=K<=5000) delimited by spaces and line breaks.


Output

At first line you must output one number - the quantity of self-numbers in interval [1..N]. Second line must contain K numbers - a[s1]..a[sk], delimited by spaces. It`s a gaurantee, that all self-numbers a[s1]..a[sk] are in interval [1..N]. (for example if N = 100, sk can be 1..13 and cannot be 14, because 14-th self-number a[14] = 108, 108 > 100)


Sample Input

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

13
1 3 5 7 9 20 31 75 86 97

<|response|>
1. Abridged problem statement  
Given an integer N (1 ≤ N ≤ 10^7) and K query positions s₁,…,sₖ (1 ≤ K ≤ 5000), we define d(x)=x+sum_of_digits(x). A number y is a self-number if there is no x such that d(x)=y. Let a[i] be the i-th self-number in increasing order.  
Output:  
• First line: the total count of self-numbers in [1..N].  
• Second line: the values a[s₁], a[s₂], …, a[sₖ]. It is guaranteed that each requested a[sᵢ]≤N.

2. Key observations  
- For every x in [1..N], we can compute y=d(x). If y≤N, then y is not a self-number.  
- After marking all such y up to N, the unmarked numbers are exactly the self-numbers.  
- We only need to answer up to K=5000 queries for positions in the self-number sequence; we do not have to store all self-numbers, just record those whose rank matches one of the sᵢ.

3. Full solution approach  
a. Read N, K and the list of query positions s[0..K-1].  
b. Build an array has_gen[1..N], initialized to false.  
c. For x from 1 to N:  
   • Compute sum of digits of x, call it S; let y = x + S.  
   • If y ≤ N, set has_gen[y] = true.  
d. Prepare to answer queries:  
   • Create a vector of pairs queries = {(s[i], i)} and sort it by the first component (the desired rank).  
   • Allocate answer[K].  
e. Scan i from 1 to N, maintain a counter cnt of self-numbers seen so far.  
   If has_gen[i] is false, increment cnt; while the next query in the sorted list has first==cnt, record answer[ its original index ] = i and advance the query pointer.  
f. After the scan, cnt is the total number of self-numbers ≤N. Print cnt and then the answers in the original query order.

Time complexity:  
- O(N · digit_count(N)) ≃ O(N·7) to mark all generated numbers.  
- O(N + K log K) to scan for self-numbers and answer queries.  
Overall O(N).

4. C++ implementation with detailed comments  
```cpp
#include <bits/stdc++.h>
using namespace std;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N, K;
    cin >> N >> K;
    vector<int> s(K);
    for(int i = 0; i < K; i++){
        cin >> s[i];
    }

    // Step 1: mark generated numbers
    // has_gen[i] == true means i = x + sum_digits(x) for some x
    vector<char> has_gen(N+1, 0);

    auto sum_digits = [&](int x){
        int s = 0;
        while(x){
            s += x % 10;
            x /= 10;
        }
        return s;
    };

    for(int x = 1; x <= N; x++){
        int y = x + sum_digits(x);
        if(y <= N){
            has_gen[y] = 1;
        }
    }

    // Step 2: prepare queries sorted by desired rank
    vector<pair<int,int>> queries;
    queries.reserve(K);
    for(int i = 0; i < K; i++){
        queries.emplace_back(s[i], i);
    }
    sort(queries.begin(), queries.end());

    vector<int> answer(K);
    int qptr = 0;      // pointer into queries
    int cnt = 0;       // count of self-numbers found

    // Step 3: scan numbers 1..N
    for(int i = 1; i <= N && qptr < K; i++){
        if(!has_gen[i]){
            cnt++;
            // check if this rank matches any query
            while(qptr < K && queries[qptr].first == cnt){
                int orig_idx = queries[qptr].second;
                answer[orig_idx] = i;
                qptr++;
            }
        }
    }

    // Count all self-numbers even if we've answered all queries
    // (to get the correct total count)
    for(int i = cnt+1; i <= N; i++){
        if(!has_gen[i]) cnt++;
    }

    // Step 4: output results
    cout << cnt << "\n";
    for(int i = 0; i < K; i++){
        cout << answer[i] << (i+1<K ? ' ' : '\n');
    }
    return 0;
}
```

5. Python implementation with detailed comments  
```python
import sys

def main():
    data = sys.stdin.read().split()
    N, K = map(int, data[:2])
    s = list(map(int, data[2:]))

    # Step 1: mark generated numbers
    has_gen = bytearray(N+1)  # 0 = self-number candidate, 1 = generated

    def sum_digits(x):
        tot = 0
        while x:
            tot += x % 10
            x //= 10
        return tot

    for x in range(1, N+1):
        y = x + sum_digits(x)
        if y <= N:
            has_gen[y] = 1

    # Step 2: prepare sorted queries by desired rank
    queries = sorted((s[i], i) for i in range(K))
    answer = [0]*K
    qptr = 0
    cnt = 0

    # Step 3: scan 1..N, count self-numbers and answer queries
    for i in range(1, N+1):
        if has_gen[i] == 0:
            cnt += 1
            # while current query wants this rank, record answer
            while qptr < K and queries[qptr][0] == cnt:
                _, orig_idx = queries[qptr]
                answer[orig_idx] = i
                qptr += 1

    # Step 4: output
    out = []
    out.append(str(cnt))
    out.append(" ".join(str(answer[i]) for i in range(K)))
    sys.stdout.write("\n".join(out))

if __name__ == "__main__":
    main()
```

Explanation of core steps:  
- We build a simple sieve-like array `has_gen` where we mark y=d(x) for all x.  
- Any unmarked i in [1..N] is a self-number.  
- We only store the answer for the requested positions by sorting the queries by rank and advancing a pointer as we enumerate self-numbers in increasing order. This way we use O(N) time and O(N+K) memory.