108. Self-numbers 2

time limit per test: 0.25 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 \le N \le 10^7, 1 \le K \le 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[s:sub:1]..a[s:sub:k], delimited by spaces. It`s a gaurantee, that all self-numbers a[s:sub:1]..a[s:sub:k] are in interval [1..N]. (for example if N = 100, s:sub:k can be 1..13 and cannot be 14, because 14-th self-number a[14] = 108, 108 > 100)

Example(s)

Sample Input Sample Output
100 10
1 2 3 4 5 6 7 11 12 13
13
1 3 5 7 9 20 31 75 86 97
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>

#define inf 0x3f3f3f3f
#define Inf 0x3FFFFFFFFFFFFFFFLL
#define maxn 5050
using namespace std;
int mark[340000];
int res[maxn];
int n, k;
struct node{
  int id, v;
  friend bool operator<(node a, node b){
    return a.v < b.v;
  }
}s[maxn];
int cnt;
int sum(int n){
  return n == 0 ? 0 : n % 10 + sum(n / 10);
}
void init(int n){
  memset(mark, 0, sizeof(mark));
  int t, p, q, tp, tq;
  int x = 0;
  cnt = 1;
  for(int i=1;i<=n;i++){
    p = i / 30;
    q = i % 30;
    if(!(mark[p] & (1 << q))){
      while(x < k && cnt == s[x].v){
        res[ s[x].id ] = i;
        x++;
      }
      cnt++;
    }
    t = i + sum(i);
    tp = t / 30;
    tq = t % 30;
    if(t <= 10000000) mark[tp] |= (1 << tq);
  }
}
int main(){
  while(~scanf("%d%d", &n, &k)){
    for(int i=0;i<k;i++){
      scanf("%d", &s[i].v);
      s[i].id = i;
    }
    sort(s, s + k);
    init(n);
    printf("%d\n", cnt - 1);
    for(int i=0;i<k;i++){
      if(i == k - 1) printf("%d\n", res[i]);
      else printf("%d ", res[i]);
    }
  }
  return 0;
}