<|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.
b. Use a bitset `dp` over [0..len), where len is the smallest power of two exceeding N, chosen at compile time via the `solve_fixed_len` template recursion so the bitset is sized statically. `dp[i]` flags that i has a generator.
c. For x from 1 to N, compute y = x + sum_of_digits(x); if y < len, set dp[y] = true.
d. Mark `useful_indices[s]` for every requested rank s.
e. Scan i from 1 to N, maintaining a counter cnt of self-numbers seen so far. If dp[i] is false, increment cnt; if `useful_indices[cnt]` is set, append i to the answer list.
f. After the scan, cnt is the total number of self-numbers ≤N. Print cnt; then for each query, look up its answer through the coordinate-compressed list of distinct requested ranks and print it 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
```cpp
#include <bits/stdc++.h>

using namespace std;

template<typename T1, typename T2>
ostream& operator<<(ostream& out, const pair<T1, T2>& x) {
    return out << x.first << ' ' << x.second;
}

template<typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& x) {
    return in >> x.first >> x.second;
}

template<typename T>
istream& operator>>(istream& in, vector<T>& a) {
    for(auto& x: a) {
        in >> x;
    }
    return in;
};

template<typename T>
ostream& operator<<(ostream& out, const vector<T>& a) {
    for(auto x: a) {
        out << x << ' ';
    }
    return out;
};

int n, k;
vector<int> a;

void read() {
    cin >> n >> k;
    a.resize(k);
    cin >> a;
}

const int MAXLEN = (int)1e7 + 42;

template<int len = 1>
void solve_fixed_len() {
    if(len <= n) {
        solve_fixed_len<min(len * 2, MAXLEN)>();
        return;
    }

    function<int(int)> nxt = [&](int x) {
        int res = x;
        while(x) {
            res += x % 10;
            x /= 10;
        }
        return res;
    };

    bitset<len> dp;
    bitset<len> useful_indices;
    for(int i: a) {
        useful_indices[i] = true;
    }

    for(int i = 1; i <= n; i++) {
        int nxt_i = nxt(i);
        if(nxt_i >= len) {
            continue;
        }
        dp[nxt_i] = true;
    }

    vector<int> ans;
    vector<int> compressed = a;
    sort(compressed.begin(), compressed.end());
    compressed.erase(
        unique(compressed.begin(), compressed.end()), compressed.end()
    );

    int cnt = 0;
    for(int i = 1; i <= n; i++) {
        if(!dp[i]) {
            cnt++;
            if(useful_indices[cnt]) {
                ans.push_back(i);
            }
        }
    }

    cout << cnt << '\n';
    for(int i: a) {
        int real_i = lower_bound(compressed.begin(), compressed.end(), i) -
                     compressed.begin();
        cout << ans[real_i] << ' ';
    }
    cout << '\n';
}

void solve() {
    // A number m is a self-number if no smaller k satisfies k + digitsum(k) ==
    // m. Mark every value d(i) = i + digitsum(i) as "has a generator", then the
    // unmarked values in [1, n] are exactly the self-numbers, in order.
    //
    // dp is a bitset over [0, len) flagging generated numbers; len is chosen at
    // compile time as the smallest power of two exceeding n (via the
    // solve_fixed_len template recursion) so the bitset is sized statically.
    // We sweep 1..n, count self-numbers, and for the queried 1-based indices
    // s_i collect the corresponding self-numbers. useful_indices marks which
    // ranks were requested; the queries are then answered through the
    // coordinate-compressed list of distinct requested ranks.

    solve_fixed_len();
}

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

    int T = 1;
    // cin >> T;
    for(int test = 1; test <= T; test++) {
        read();
        // cout << "Case #" << test << ": ";
        solve();
    }

    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 `dp` 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 tracking which ranks were requested and recording the matching self-numbers as we enumerate them in increasing order. This way we use O(N) time and O(N+K) memory.
