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. 多校联合练习赛1 Problem1005 Deque LIS+LDS 再加一系列优化

    Deque Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  2. Login failed for user 'NT AUTHORITY\NETWORK SERVICE'的解决方法

    1.打开SQL Server Manegement Studio 2.在 Security - logins 中  NETWORK SERVICE 3.双击该用户 Server Roles 中 勾选 ...

  3. 对于windows窗口的标题菜单栏的操作——删除/禁用 最小最大话和关闭

    HWND hand = FindWindow(NULL, "计算器"); int nStyle = GetWindowLong(hand, GWL_STYLE);nStyle &a ...

  4. ShowDialog()弹出的窗体,关闭后,主窗体会闪烁的BUG

    如图,要实现下列等待界面时,等待界面是以ShowDialog弹出的,发现关闭后,主窗体会闪烁一下的BUG,搞半天没搞明白啥原因. 过了几天后,搜索了下发现,在fm.ShowDialog(),显示出来时 ...

  5. Java Eclipse常规设置

    改变字体大小 eclipse英文版中如何去修改字体及方法?首先打开eclipse中,按下面的方法即可菜单项:window ->preferences -> general -> ap ...

  6. CSS Hack代码与浏览兼容总结

    关于CSS Hack的东西能少尽量少吧.发现这篇文章我写得太复杂了,所以重新精简了一下,把代码粘贴到jsfiddle上,方面修改代码和维护. 1, IE条件注释法,微软官方推荐的hack方式. 只在I ...

  7. jquery结合Highcharts插件实现动态数据仪表盘图形化显示效果

    仪表盘显示效果如图: 方法一效果图: 方法二效果图(插件版本4.0.1): ​ js代码如下: $(function(){ //方法一: var chart = new Highcharts.Char ...

  8. head first 设计模式读书笔记 之 策略模式

    作为一个php开发者,深知曾经很多程序员都鄙视php,为什么呢?因为他们认为php的语法是dirty的,并且由于开发者水平参差不齐导致php的代码更加乱上加乱,维护起来简直一坨shit一样.随着php ...

  9. s3c6410学习笔记-烧写uboot+构建文件系统

    一.进入目录 #cd u-boot-1.1.6_sndk6410 二.SD卡 make clean make distclean vim Makefile                       ...

  10. 【每天一个Linux命令】12. Linux中which命令的用法

    which  用来查看可执行文件的位置. 1.命令格式: which 可执行文件名称 2.命令功能: which指令会在PATH变量指定的路径中,搜索某个系统命令的位置,并且返回第一个搜索结果. 3. ...