HOJ1087

时间:2023-03-09 00:56:37
HOJ1087

Self Numbers

My Tags   (Edit)
  Source : ACM ICPC Mid-Central USA 1998
  Time limit : 5 sec   Memory limit : 32 M

Submitted : 1443, Accepted : 618

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. There are thirteen self-numbers less than 100: 1, 3, 5, 7, 9, 20, 31, 42, 53, 64, 75, 86, and 97.

Write a program to output all positive self-numbers less than or equal 1000000 in increasing order, one per line.

题目大意为打印小于1000000以内的自私数

自私数是指可由一个数的各位数字与本身的和组成:例如 2 = 1 + 1;  11 = 1 + 0 + 10; 22 = 2 + 0 + 20;这些都是自私数
一开始仿照埃氏筛法的思想,处理一个数字顺带把由这个数字生成的其他所有数字都处理掉。结果总是TLE,附上代码:
  #include<iostream>
using namespace std; long MaxSize = ; long Dc(long Num){
long D = ;
D = Num + (Num%) + (Num/)% + (Num/)% + (Num/)% + (Num/)% +(Num/)%;
return D;
} int main(){
bool List[MaxSize];
for(long i = ;i <= MaxSize;i++){
if(!List[i]) printf("%d\n",i);
long no_self = i;
while(no_self <= MaxSize && !List[no_self]){
no_self = Dc(no_self);
if(no_self <= MaxSize)
List[no_self] = ;
}
}
return ;
}

问题出在17~21行,这段while循环体实质上并没有让外循环for的指标进行非线性变动,即while循环完全是做无用功。这与埃氏筛有本质不同。但观察到,这段代码只需要给出下一个非Self number的序号即可,因此while循环就可以全部摘去,采用在线处理算法的思想,整个算法的复杂度直接将为O(N),下面为AC代码:

 /*This Code is Submitted by mathmiaomiao for Problem 1087 at 2015-08-21 23:21:22*/
#include <iostream> using namespace std; bool List[];
int main() {
long no_self = ;
for(long i = ; i < ; ++i) {
if(!(List[i])) printf("%ld\n",i);
no_self = i + (i%) + (i/)% + (i/)% + (i/)% + (i/)% +(i/)%;
List[no_self] = ;
}
printf("1000000\n");
return ;
}