SGU 108 解题报告

http://acm.sgu.ru/problem.php?contest=0&problem=108

108. Self-numbers 2

time limit per test: 2.50 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);

1949年,印度数学家D.R. Kaprekar 发现了一类数,称为自我数.对于任何一个正整数 n ,定义 d(n) 的值为 n 与 n的每一位数字之和.(d 叫做digitadition, 这个单词是Kaprekar创造的.) 例如,d(75) = 75 + 7 + 5 = 87. 给你任意正整数 n 作为起点,您可以构建无限增加的整数 n 序列,n, d(n), d(d(n)), d(d(d(n))),.... 例如,起点是33,则下一个数是 33 + 3 + 3 = 39,再下一个数是 39 + 3 + 9 = 51,然后是 51 + 5 + 1 = 57,这样由 33 生成的数列是 33, 39, 51, 57, 69, 84, 96, 111, 114, 120, 123, 129, 141,... 这样我们称正整数 n 是 d(n)生成器.在刚才的序列中, 33 是 39 的生成器, 39 是 51 的生成器, 51 是 57 的生成器,等等.有些数有不止一个生成器,例如, 101 有两个生成器, 91 和 100.如果一个数没有生成器我们就称它为自我数.用 a[i] 表示第 i 个自我数.小于 100 有 13 个自我数:1, 3, 5, 7, 9, 20, 31, 42, 53, 64, 75, 86, 97.(第一个自我数是 a[1] = 1,第二个是 a[2] = 3,:,第十三个是 a[13] = 97);

Input

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

输入数据包含整数 N, K, s1...sk. (1<=N<=107, 1<=K<=5000) 用空格或回车隔开。

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)

第一行输出 [1..N]范围内自我数的数量.第二行包含 K 个数 - a[s1]..a[sk], 用空格隔开.必须满足所有自我数 a[s1]..a[sk] 是在[1..N]范围内.(例如,如果 N =100,sk可以是 1..13 但不能是 14,因为 第 14 个自我数 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

我靠,初一看,水题,这么简单.仔细做一下才知道空间卡的非常紧.
看到题目的第一印象是筛法,不过建立一个大小10^7的bool数组需要的空间是9.536M,直接MLE了,数组绝对开不到这么大.
不过仔细观察可以发现,无论数字 n 多大,作为生成器他所能生成的数最多只能到 n + 63,于是可以建立一个 128 的数组做滚动处理,每次处理 64 个,将其中的自我数存起来,再向后滚动.

CODE

#include<stdio.h>
#include<string.h>
 
int main()
{
  int n,k,i,j,t,p,q,a[1000000];
  bool D[128],Dt[65];
  scanf("%d%d",&n,&k);
  t=0;
  memset(D,true,sizeof(D));
  for(i=1;i<=n;i+=64){
      memset(D+64,true,sizeof(D)/2);
      for(j=0;j<=63;++j){
	  p=q=j+i;
	  while(q){
	      p+=q%10;
	      q/=10;
	  }
	  D[p-i]=false;
      }
      for(p=0;p<=63;++p){
	  if(D[p]) a[++t]=i+p;
      }
      for(p=0;p<64;++p) D[p]=D[p+64];
  }
  while(a[t]>n) --t;
  printf("%dn",t);
  for(i=0;i<k;i++){
      scanf("%d",&j);
      printf("%d ",a[j]);
  }
  putchar('n');
  return 0;
}
» 本博客采用署名 2.5 中国大陆许可协议进行许可,本文版权归作者所有,欢迎转载,但必须在明显位置给出原文连接。
anyShare分享到:

Leave a Comment

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>