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

  

sgu108. Self-numbers 2 滚动数组 打表 难度:1的更多相关文章

  1. 【(好题)组合数+Lucas定理+公式递推(lowbit+滚动数组)+打表找规律】2017多校训练七 HDU 6129 Just do it

    http://acm.hdu.edu.cn/showproblem.php?pid=6129 [题意] 对于一个长度为n的序列a,我们可以计算b[i]=a1^a2^......^ai,这样得到序列b ...

  2. CodeForces 173C Spiral Maximum 记忆化搜索 滚动数组优化

    Spiral Maximum 题目连接: http://codeforces.com/problemset/problem/173/C Description Let's consider a k × ...

  3. hdu 3392(滚动数组优化dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3392 Pie Time Limit: 6000/3000 MS (Java/Others)    Me ...

  4. 2014年北京 happy matt friends(dp + 滚动数组优化)

    Happy Matt Friends Time Limit: 6000/6000 MS (Java/Others)    Memory Limit: 510000/510000 K (Java/Oth ...

  5. HDU 5119 Happy Matt Friends (背包DP + 滚动数组)

    题目链接:HDU 5119 Problem Description Matt has N friends. They are playing a game together. Each of Matt ...

  6. 字符串匹配dp+bitset,滚动数组优化——hdu5745(经典)

    bitset的经典优化,即把可行性01数组的转移代价降低 bitset的适用情况,当内层状态只和外层状态的上一个状态相关,并且内层状态的相关距离是一个固定的数,可用bitset,换言之,能用滚动数组是 ...

  7. USACO 2009 Open Grazing2 /// DP+滚动数组oj26223

    题目大意: 输入n,s:n头牛 s个栅栏 输入n头牛的初始位置 改变他们的位置,满足 1.第一头与最后一头的距离尽量大 2.相邻两头牛之间的距离尽量满足 d=(s-1)/(n-1),偏差不超过1 3. ...

  8. NYOJ_37.回文字符串 (附滚动数组)

    时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描述 所谓回文字符串,就是一个字符串,从左到右读和从右到左读是完全一样的,比如"aba".当然,我们给你的问 ...

  9. BZOJ-1925 地精部落 烧脑DP+滚动数组

    1925: [Sdoi2010]地精部落 Time Limit: 10 Sec Memory Limit: 64 MB Submit: 1053 Solved: 633 [Submit][Status ...

随机推荐

  1. cygwin下使用apt-cyg安装新软件

    1.获取  (记得先安装好git) git clone https://github.com/transcode-open/apt-cyg.git 2.安装apt-cyg cd apt-cyg chm ...

  2. IDEA,RubyMine等JetBrains系列软件通用破解教程

    此教程不光适用于IDEA,还可以在RubyMine等JetBrains系列软件使用,亲测可用. (1)下载安装你需要的JetBrains系列软件,安装完最好不要打开,直接finish,断开网络. (2 ...

  3. java的Date() 转换符

    本字段下均转自 csdn 阿念1989 本文是学习网络上的文章时的总结,感谢大家无私的分享. System.out.printf()方法可以对日期做处理输出. 对应列表 转换符 类型 举例 c 完整的 ...

  4. LLDP协议、STP协议 笔记

    参考: 数据链路层学习之LLDP 生成树协议 LLDP协议.STP协议 笔记 LLDP 提出背景: 随着网络技术的发展,接入网络的设备的种类越来越多,配置越来越复杂,来自不同设备厂商的设备也往往会增加 ...

  5. 【TCP/IP详解 卷一:协议】第十二章 广播与多播 ping实验

    我手机连接到wifi上所分配到的IP地址:192.168.1.116 子网掩码:255.255.255.0 路由器:192.168.1.1 ping 192.168.1.116 (ping 一台主机的 ...

  6. Yandex.Algorithm 2011 Round 2 D. Powerful array 莫队

    题目链接:点击传送 D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input ...

  7. Cocos2d-x学习笔记(二)AppDelegate类详解

    由源代码,可得到如下的类继承关系: 1. 在方法applicationDidFinishLaunching中,首先会调用CCDirector* pDirector = CCDirector::shar ...

  8. 告诉maven,我真的不需要web.xml

    <!-- 告诉maven,我真的不需要web.xml --> <plugin> <groupId>org.apache.maven.plugins</grou ...

  9. SpringBoot导入excle文件数据

    本文主要描述,Springboot框架下上传excel,处理里面相关数据做逻辑分析,由于用到的是前后端分离技术,这里记录的主要是后端java部分,通过与前端接口进行对接实现功能 1.在pom.xml文 ...

  10. Java线程池队列吃的太饱,撑着了咋整?java 队列过大导致内存溢出

    Java的Executors框架提供的定长线程池内部默认使用LinkedBlockingQueue作为任务的容器,这个队列是没有限定大小的,可以无限向里面submit任务. 当线程池处理的太慢的时候, ...