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 ;
}

HOJ1087的更多相关文章

  1. OJ题目分类

    POJ题目分类 | POJ题目分类 | HDU题目分类 | ZOJ题目分类 | SOJ题目分类 | HOJ题目分类 | FOJ题目分类 | 模拟题: POJ1006 POJ1008 POJ1013 P ...

随机推荐

  1. 前端入门HTML5扩展知识(一)

    一. 请描述一个网页从开始请求到最终显示的完整过程? 1.  在浏览器中输入网址: 2.  发送至 DNS 服务器并获得域名对应的 WEB 服务器的 IP 地址: 3.  与 WEB 服务器建立 TC ...

  2. Sql Server数据库--》事务

    事务:更多的是一种处理机制(同生共死) 事务是对增删改而言的(因为她们会改变数据) 事务是对多条语句而言,多个sql语句组成,整体执行 事务的4个特点叫做ACID:分别为: 1,A:原子性->事 ...

  3. Linux(CentOS)安装配置zeromq、jzmq(解决各种问题)

    今天为Hadoop配置zeromq.jzmq遇到各种问题,先是编译出错,到编译成功后测试出错等等,下面将我遇到的问题与大家分享一下. 第一个注意点是:必须先编译安装zeromq,然后在编译jzmq,否 ...

  4. applicationContext.xml 配置文件的存放位置

    eb.xml中classpath:和classpath*:  有什么区别? classpath:只会到你的class路径中查找找文件; classpath*:不仅包含class路径,还包括jar文件中 ...

  5. char str[] 与 char *str的区别详细解析

    char* get_str(void) { char str[] = {"abcd"}; return str; } char str[] = {"abcd"} ...

  6. Sublime Text3 插件安装教程

    链接地址:http://jingyan.baidu.com/article/4d58d541caeeaa9dd4e9c093.html

  7. 不要伤害指针(1)--运算符&和*

    原文转载地址:http://blog.csdn.net/sunchaoenter/article/details/6646001 增加自己的想法,作为笔记. 这里&是取地址运算符,*是间接运算 ...

  8. C/C++中的far和near两个指针

    Dos 的设计是基于16位的CPU的,也就是CPU中的每个寄存器(Register)只有16位,只能存放0-65535(64K)的值.为了能访问大于64K的内存,人们用了分段的方法,用两个16位的数来 ...

  9. OC语法5——@property和@synthesize

    @property和@synthesize: 我们回想一下: 在OC中我们定义一个Student类需要两个文件Student.h 和 Student.m. Student.h(声明文件):定义成员变量 ...

  10. php数组排序和分割字符串

    function sortStr($str){ $ary = str_split($str); sort($ary); $len = count($ary); $arr = array(); for( ...