题目
On the well-known testing system MathForces, a draw of rating units is arranged. The rating will be distributed according to the following algorithm: if participants take part in this event, then the rating is evenly distributed between them and rounded to the nearest lower integer, At the end of the drawing, an unused rating may remain — it is not given to any of the participants.
For example, if and , then each participant will recieve an rating unit, and also rating units will remain unused. If , and , then none of the participants will increase their rating.
Vasya participates in this rating draw but does not have information on the total number of participants in this event. Therefore, he wants to know what different values of the rating increment are possible to get as a result of this draw and asks you for help.
For example, if , then the answer is equal to the sequence . Each of the sequence values (and only them) can be obtained as ⌊n/k⌋ for some positive integer (where is the value of rounded down): .
Write a program that, for a given , finds a sequence of all possible rating increments.
输入
The first line contains integer number — the number of test cases in the input. Then test cases follow.
Each line contains an integer — the total number of the rating units being drawn.
输出
Output the answers for each of test cases. Each answer should be contained in two lines.
In the first line print a single integer — the number of different rating increment values that Vasya can get.
In the following line print integers in ascending order — the values of possible rating increments.
题目大意
给定组数据,每组包含一个整数,求出所有n被整除能得出的商。
想法
朴素想法:枚举,每个除一遍,加到中去重,然后直接遍历输出即可。
复杂度,死路一条。
考虑到,我们可以最多枚举到,在枚举时同时加入和,那么式子看起来是这样:
即可保证所有整除商都被加入中。
此时复杂度,能过。
代码如下:
#include <cstdio>
#include <set>
#include <cmath>
using namespace std;
int t, n;
set<int> out;
int main()
{
scanf("%d", &t);
while (t--)
{
out.clear();
scanf("%d", &n);
out.insert(0);
int lim = sqrt(n);
for (int i = 1; i <= lim; ++i)
{
out.insert(i);
out.insert(n / i);
}
printf("%d
",out.size());
for (set<int>::iterator it = out.begin(); it != out.end(); ++it)
printf("%d ", *it);
printf("
");
}
return 0;
}
但事实上,还有更简单的方法:我们可以去掉这个!
在枚举时,我们会发现,每个都是可以取到且不重复的,而实际上也是不重复的。证明如下:
则有:
假如,那么:
然而:
那么
显然此时,产生了矛盾。
因此,对于,我们得到的所有的和即为答案。
顺序枚举,将存入另一个数组中,显然该数组中的数单调递减。
还需要特判最后时,的情况。
输出答案直接输出,再逆序输出保存数组中的结果即可。
复杂度,已经相当优秀了。
还有一种整除分块的方法,本蒟蒻还不会……
Code
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
int t, n, lim, cnt;
int save[50000];
int main()
{
scanf("%d", &t);
while (t--)
{
scanf("%d", &n);
lim = sqrt(n);
cnt = 0;
for (register int i = 1; i <= lim; ++i)
save[++cnt] = n / i;
if (cnt && lim == save[cnt]) //特判,注意有可能输入为0,这样cnt会被减为负数……
--cnt;
printf("%d
", cnt + lim + 1);
for (int i = 0; i <= lim; ++i)
printf("%d ", i);
for (int i = cnt; i; --i)
printf("%d ", save[i]);
putchar('
');
}
return 0;
}