sgu108. Self-numbers 2 滚动数组 打表 难度:1
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的更多相关文章
- 【(好题)组合数+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 ...
- CodeForces 173C Spiral Maximum 记忆化搜索 滚动数组优化
Spiral Maximum 题目连接: http://codeforces.com/problemset/problem/173/C Description Let's consider a k × ...
- hdu 3392(滚动数组优化dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3392 Pie Time Limit: 6000/3000 MS (Java/Others) Me ...
- 2014年北京 happy matt friends(dp + 滚动数组优化)
Happy Matt Friends Time Limit: 6000/6000 MS (Java/Others) Memory Limit: 510000/510000 K (Java/Oth ...
- HDU 5119 Happy Matt Friends (背包DP + 滚动数组)
题目链接:HDU 5119 Problem Description Matt has N friends. They are playing a game together. Each of Matt ...
- 字符串匹配dp+bitset,滚动数组优化——hdu5745(经典)
bitset的经典优化,即把可行性01数组的转移代价降低 bitset的适用情况,当内层状态只和外层状态的上一个状态相关,并且内层状态的相关距离是一个固定的数,可用bitset,换言之,能用滚动数组是 ...
- USACO 2009 Open Grazing2 /// DP+滚动数组oj26223
题目大意: 输入n,s:n头牛 s个栅栏 输入n头牛的初始位置 改变他们的位置,满足 1.第一头与最后一头的距离尽量大 2.相邻两头牛之间的距离尽量满足 d=(s-1)/(n-1),偏差不超过1 3. ...
- NYOJ_37.回文字符串 (附滚动数组)
时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 所谓回文字符串,就是一个字符串,从左到右读和从右到左读是完全一样的,比如"aba".当然,我们给你的问 ...
- BZOJ-1925 地精部落 烧脑DP+滚动数组
1925: [Sdoi2010]地精部落 Time Limit: 10 Sec Memory Limit: 64 MB Submit: 1053 Solved: 633 [Submit][Status ...
随机推荐
- Bootloader之uBoot简介
本文转载自:http://blog.ednchina.com/hhuwxf/1915416/message.aspx 一.Bootloader的引入 从前面的硬件实验可以知道,系统上电之后,需要一段程 ...
- 分布式系统一致性协议--2PC,3PC
分布式系统中最重要的一块,一致性协议,其中就包括了大名鼎鼎的Paxos算法. 2PC与3PC 在分布式系统中,每一个机器节点虽然能够明确知道自己在进行事务操作过程中的结果是成功或是失败,但是却无法直接 ...
- JavaScript:正则表达式 分组2
继续上一篇的写,这篇复杂点. 分组+范围 var reg=/([abcd]bc)/g; var str="abcd bbcd cbcd dbcd"; console.log(str ...
- 导出数据库表为world文档说明,以及PowerDesigner导出表结构pdm设计文档
如何使用“mysql导出数据库结构为world工具”以及如何使用powerdesigner映射数据库模型 一.通过powerdesigner配置ojdbc 1.安装并打开powerdesigner,新 ...
- Educational Codeforces Round 27 A B C
A. Chess Tourney Berland annual chess tournament is coming! Organizers have gathered 2·n chess pla ...
- Unity 之 transform
transform.Translate 1.function Translate (translation : Vector3, relativeTo : Space = Space.Self) : ...
- 3、CommonChunkPlugin提取公共js-以提取一个jquery为例
cnpm install css-loader --save-dev //css-loader 是将css打包进js cnpm install style-loader --save-dev ...
- Unity3D学习笔记(二):个体层次、绝对和局部坐标、V3平移旋转
Directional Light:平行光源/方向性光源,用来模拟太阳光(角度只与旋转角度有关,与位置无关) Point Light:点光源,用来模拟灯泡,向四周发散光源 Spotlight:锥光源/ ...
- (02) 任务(Jobs)和触发器(Triggers)
Quart 的 API Quartz API 中的关键接口和类如下: IScheduler-与调度器(scheduler)进行交互的主要 API: IJob-被组件继承和实现,由调度器来执行的接口: ...
- hdu 3706 Second My Problem First 单调队列
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3706 Second My Problem First Time Limit: 12000/4000 M ...