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. 解决Navicat Premium 12 连接oracle数据库出现ORA-28547的问题

    1. 出现的问题... 下午工作时想连接Oracle数据库,使用的是Navicat Premium 12 . 数据库地址.用户名.密码.端口号都没有问题,但出现了ORA-28547:connectio ...

  2. UVa 10891 Game of Sum - 动态规划

    因为数的总和一定,所以用一个人得分越高,那么另一个人的得分越低. 用$dp[i][j]$表示从$[i, j]$开始游戏,先手能够取得的最高分. 转移通过枚举取的数的个数$k$来转移.因为你希望先手得分 ...

  3. Python3基础 str format 输出花括号{}

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  4. JavaScript紧凑学习

    JavaScript紧凑学习 windows本地,调用命令行: win键+R 键入cmd , (cmd是Command 命令行 简称) 目录是C盘下的 C:\Users\Administrator&g ...

  5. Ubuntu上 配置Eclipse:安装CDT

    在最新的 Ubuntu Kylin 16.04 中安装了eclipse,在纠结了很久的网络问题之后,开始了eclipse的配置以便在上面运行ns3. 在官方网站上安装完 eclipse LUNA 之后 ...

  6. 【Coursera】Seventh Week

    Application Layer:Use the services of the TCP layer Quick Review Link Layer(Ethernet):gets the data ...

  7. 华中农业大学预赛 B Handing Out Candies 余数的和

    Problem B: Handing Out Candies Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 258  Solved: 19[Submit ...

  8. pandas 设置单元格的值

    import pandas as pd import pickle import numpy as np dates = pd.date_range() df = pd.DataFrame(np.ar ...

  9. jekins 插件离线安装

    官网插件地址:http://updates.jenkins-ci.org/download/plugins/ 系统管理->插件管理->高级 选择一个下载好的插件,然后点击上传即可 然后就会 ...

  10. jekins,报错 stderr: Could not create directory '/usr/share/tomcat7/.ssh'. Failed to add the host to the list of

    public key是在~/.ssh/id_rsa.pub,而private key是~/.ssh/id_rsa 设置的时候,Jenkins需要的是private key