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
题意:
在1949年印度的数学假D.R. Kaprekar发现了一种叫做self-number的经典数字,对于任意正整数n,定义d(n)为n加上n的各个位上的数字(d是数字的意思,Kaprekar发明的一个术语)。如:d(75) = 75 + 7 + 5 = 87。给定任意正整数n,你可以构建出无限的整数递增:n, d(n), d(d(n)), d(d(d(n))), ……举个例子,你从33开始,那么下一个数就是33 + 3 + 3 = 39, 再下一个就是39 + 3 + 9 = 51, 接着就是 51 + 5 + 1 = 57, 那样就生成了一个序列: 33, 39, 51, 57, 69, 84, 96, 111, 114, 120, 123, 129, 141, … 这里n叫做d(n)的母数 上面的数列中,33是39的母数,39是51的母数,51是57的母数,以此类推……有些数字不止一个母数,比如101有两个母数,91和100。没有母数的数字就叫做self-number。让a[i]成为第i个self-number。现在存在13个小于100的self-number: 1, 3, 5, 7, 9, 20, 31, 42, 53, 64, 75, 86, 和 97. (第一个 self-number是a[1]=1, 第二个是 a[2] = 3, :, 第十三个是 a[13]=97);
输入:包含整数 N, K, s1…sk. (1<=N<=107, 1<=K<=5000) 被空格和换行分割开。
输出:第一行你必须输出一个数字——表示在[1,n]中self-numbers的个数。第二行必须输出K个数字:a[s1]..a[sk],用空格分开。保证所有的在a[s1]..a[sk]的self-numbers都在[1,n]的区间内,(比如N = 100, sk 就只能等于1..13并且不能等于14, 以为第14个self-number a[14] = 108, 108 > 100)
题解:
一开始看到这题感觉好水啊,时间都有2.5s,不想怎么做就怎么做啊。
结果一看空间傻眼了,woc,4MB能干啥使。。。完全没法做。。。所以就想可不可能有规律,然后就暴力打了一个表,然后果断发现了规律,从第6个self-number开始,每两self-number之间都差11,然后每10个之间差11的数之间差2,然后就高兴的交了上去,结果发现WA。。。。
然后不理解,用暴力和我的程序拍,然后就发现当数很大的时候,暴力的答案是小的。。于是乎我就打了一个特别大的表,结果就发现了另一个规律:
从第六个self-number开始,d[i] = d[i-1]+11, 即增加量是11;
每出现9个11,就会出现一个2;
每出现9个2,就会出现一个15;
每出现9个15,就会出现一个28;
每出现9个28,就会出现一个41;
每出现9个41,就会出现一个54;
然后我心中万匹羊驼奔驰而过。。。。
但要注意的是这个题的询问不一定是严格递增的,所以我们要先排序,同时要注意一个位置可能会被询问多次,所以我们在判断时要用while使得所有相同的都赋上值。
最后回到空间大小的问题上来,注意,它只有4MB空间,所以我们要想开10^7的数组是不现实的,虽然说在10^7以内,self-number大约有10^6个,但是10^6也是不能接受的。所以我们可以先排序,让询问成为单调的,然后在累加答案时,就可以将这一位上的答案赋值到询问中。这样就可以不用记录第几个self-number是多少了。
#include <iostream>
#include <cstdio>
#include<algorithm>
using namespace std;
struct qer
{
int num, id;
}sz[5005];
int d[233];
bool cmp(qer a, qer b)
{
return a.num < b.num;
}
bool cmp2(qer a, qer b)
{
return a.id < b.id;
}
int main()
{
// freopen("data.txt", "r", stdin);
//// freopen("2.txt", "w", stdout);
int n, k, ans = 0;
cin >> n >> k;
for (int i = 1; i <= k; i++)
scanf("%d", &sz[i].num), sz[i].id = i;
sort(sz + 1, sz + k + 1, cmp);
int i, t = 0, t1 = 0, t2 = 0, t3 = 0, t4 = 0, t5 = 0, sum = 9, tot = 1;
d[1] = 1;
d[2] = 3;
d[3] = 5;
d[4] = 7;
d[5] = 9;
t = 1;
t1 = t2 = t3 = t4 = 0;
for (int i = 1; i <= 5; i++)
{
while (sz[tot].num == i)
sz[tot++].num = d[i];
}
for (i = 6; i <= n; i++)
{
if (t == 10)
{
t = 1;
t1++;
if (t1 == 9)
{
if (t2 == 9)
{
if (t3 == 9)
{
if (t4 == 9)
t++;
t++;
}
t++;
}
t++;
}
if (t1 == 10)
{
t1 = 0;
t2++;
if (t2 == 10)
{
t2 = 0;
t3++;
if (t3 == 10)
{
t3 = 0;
t4++;
if (t4 == 10)
{
t4 = 0;
sum += 54;
if (sum > n)
break; //key
t = 5;
while (sz[tot].num == i)
sz[tot++].num = sum;
continue;
}
sum += 41;
if (sum > n)
break; //key
t = 4;
while (sz[tot].num == i)
sz[tot++].num = sum;
continue;
}
sum += 28;
if (sum > n)
break; //key
t = 3;
while (sz[tot].num == i)
sz[tot++].num = sum;
continue;
}
sum += 15;
if (sum > n)
break; //key
t = 2;
while (sz[tot].num == i)
sz[tot++].num = sum;
continue;
}
sum += 2;
if (sum > n)
break; //key
while (sz[tot].num == i)
sz[tot++].num = sum;
continue;
}
sum += 11;
t++;
// cout << i << " " << sum << endl;
while (sz[tot].num == i)
sz[tot++].num = sum;
if (sum > n)
break;
}
if (n < 10)
printf("%d\n", (n + 1) / 2);
else
printf("%d\n", i - 1);
sort(sz+1, sz+k+1, cmp2);
for (i = 1; i <= k; i++)
printf("%d ", sz[i].num);
return 0;
}