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 ...
随机推荐
- 完整的Android开发环境Eclipse+ADT+SDK(22.0.1)
现在开始学习Android嵌入式编程,首要的问题就是在Windows中搭建开发环境,就这个都要摸索很长的时间,总是在版本之间折腾折腾去,而且Google的Android正式差劲得很,经常是连不上,要不 ...
- sql:临时表和表变量
在SQL Server的性能调优中,有一个不可比拟的问题:那就是如何在一段需要长时间的代码或被频繁调用的代码中处理临时数据集,表变量和临时表是两种选择. 临时表: 临时对象都以#或##为前缀,临时表是 ...
- POJ 2373 Dividing the Path (单调队列优化DP)题解
思路: 设dp[i]为覆盖i所用的最小数量,那么dp[i] = min(dp[k] + 1),其中i - 2b <= k <= i -2a,所以可以手动开一个单调递增的队列,队首元素就是k ...
- 51NOD 1027 大数乘法
1027 大数乘法 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 给出2个大整数A,B,计算A*B的结果. Input 第1行:大数A 第2行:大数B (A,B ...
- Facebook广告API系列 1
Facebook广告API系列 1 前言 最近遇到大坑了,居然要去对接facebook的广告API,之前以为是跟鹅厂一样的API体系,看了半天Facebook的文档,冷汗直冒.... 这得一点一点的讲 ...
- Mybatis配置映射文件中parameterType的用法小结
原创: 在mybatis映射接口的配置中,有select,insert,update,delete等元素都提到了parameterType的用法,parameterType为输入参数,在配置的时候,配 ...
- NetMagic Simple Overview
参考: NetMagic Startup: How to develop NetMagic rapidly NetMagic Simple Overview NetMagic 是什么? NetMagi ...
- 【TCP/IP详解 卷一:协议】第十二章 广播与多播 ping实验
我手机连接到wifi上所分配到的IP地址:192.168.1.116 子网掩码:255.255.255.0 路由器:192.168.1.1 ping 192.168.1.116 (ping 一台主机的 ...
- POJ 2152 Fire(树形dp)
http://poj.org/problem?id=2152 题意: n个节点组成的树,要在树一些点上建立消防站,每个点建站都有个cost[i],每个点如果不在当前的点上建站,也要依赖其他的消防站,并 ...
- [BZOJ]|[Ural] Formula 1-----插头DP入门
1519. Formula 1 Time limit: 1.0 secondMemory limit: 64 MB Background Regardless of the fact, that Vo ...