sgu108. Self-numbers 2 滚动数组 打表 难度:1
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
思路:把询问排序,对询问打表,使用滚动数组不然会MLE
滚动数组...以后要计算是否会mle 原本是开了1e7数组,+5000答案数组,所占为20000kb左右
注意可能有相同的询问
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; bool isnt[2][100001]; int len; struct node { int num,ind,ans; }; node q[5050]; int qlen; bool cmp1(node n1,node n2){ return n1.num<n2.num; } bool cmp2(node n1,node n2){ return n1.ind<n2.ind; } int getsum(int i){ int sum=0; while(i>=10){ sum+=i%10;i/=10; } sum+=i; return sum; } int n,k; int main(){ while(scanf("%d%d",&n,&k)==2){ for(int i=0;i<k;i++){ scanf("%d",&(q[i].num)); q[i].ind=i; } sort(q,q+k,cmp1); int l=n/100000; for(int i=0;i<=l;i++){ memset(isnt[1-i&1],0,sizeof(isnt[0])); for(int j=100000*i;j<100000*(i+1)&&j<=n;j++){ if(j==0)continue; int tt=j+getsum(j); if(tt<=n){ if(tt<100000*(i+1)){ isnt[i&1][tt%100000]=true; } else isnt[1-i&1][tt%100000]=true; } if(!isnt[i&1][j%100000]){ // printf("%d ",j); len++; while(len==q[qlen].num&&qlen<k){ q[qlen].ans=j; qlen++; } } } } sort(q,q+k,cmp2); printf("%d\n",len); for(int i=0;i<k;i++){ printf("%d%c",q[i].ans,i==k-1?'\n':' '); } } return 0; }