原题链接:codeforces C. Everyone is a Winner!
题目要求
On the well-known testing system MathForces, a draw of nn rating units is arranged. The rating will be distributed according to the following algorithm: if kk participants take part in this event, then the nn 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 n=5n=5 and k=3k=3, then each participant will recieve an 11 rating unit, and also 22 rating units will remain unused. If n=5n=5, and k=6k=6, 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 n=5n=5, then the answer is equal to the sequence 0,1,2,50,1,2,5. Each of the sequence values (and only them) can be obtained as ⌊n/k⌋⌊n/k⌋ for some positive integer kk (where ⌊x⌋⌊x⌋ is the value of xx rounded down): 0=⌊5/7⌋0=⌊5/7⌋, 1=⌊5/5⌋1=⌊5/5⌋, 2=⌊5/2⌋2=⌊5/2⌋, 5=⌊5/1⌋5=⌊5/1⌋.
Write a program that, for a given nn, finds a sequence of all possible rating increments.
Input
The first line contains integer number tt (1≤t≤101≤t≤10) — the number of test cases in the input. Then tt test cases follow.
Each line contains an integer nn (1≤n≤1091≤n≤109) — the total number of the rating units being drawn.
Output
Output the answers for each of tt test cases. Each answer should be contained in two lines.
In the first line print a single integer mm — the number of different rating increment values that Vasya can get.
In the following line print mm integers in ascending order — the values of possible rating increments.
![](/qrcode.jpg)
Example
input
4
5
11
1
3
output
4
0 1 2 5
6
0 1 2 3 5 11
2
0 1
3
0 1 3
解题思路
这题是真不难,但坑就坑在总是超内存 大哭
给大家看一下我的战绩:
Memory Limit Exceeded(内存超限)
Runtime Error(运行超时)
太坑爹了,我之前的代码是这样的:可以过前三块样例
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
#define ll unsigned long long
const ll maxn=1000009;
int a[maxn];
int main()
{
int t;
cin>>t;
while(t--){
memset(a,0,sizeof(a));
ll n;
cin>>n;
memset(a,0,n);
a[0]=0;
int m=1;
for(ll i=n;i>=1;i--){
if(i==n||(n/i)!=a[m-1])
{
a[m]=n/i;
m++;
}
}
cout<<m<<endl;
for(ll i=0;i<m;i++)
cout<<a[i]<<" ";
cout<<endl;
}
return 0;
}
超内存啊有木有!!!
换个角度思考,看看大佬们是怎么解的
结果我发现了两个大宝藏:set和auto:
1、
set相当于一个插入后能自动排序的数组(从小到大)。但是要注意一个数值在set中只能出现1次或0次。注意头文件:#include
(1)anu.insert(x) ;//插入元素x
(2) anu.erase(x)/anu.erase(x,y) ;//删除排序后的第x个元素/删除排序后的第x到第y个元素
(3). anu. begin() /anu.end() ;//返回set的第1个元素 /返回set的最后1个元素
(4). anu.clear() ;//清空anu内的元素;
(5). anu.empty() ;//判断anu是否为空
(6). anu.max_size() ;//返回anu能包含的元素的最大个数;
(7). anu.size() ;//返回当前元素个数;
2、
C++11版的auto 不可以在C++0x上运行,会报错!看大佬如何解决:
如何在你的编辑器上执行C++11代码
新修改的的auto,多了许多新功能,可以自动识别后面变量的类型。**在C语言中使用auto定义的变量可以不予初始化,但在C++中必须初始化!**我第一次使用就报了错,后来我发现原来是我没修改settings中compiler的选项(Codeblocks中)。
正确代码
下面就是我用了新学的set<>和auto写出的代码:
#include <iostream>
#include<bits/stdc++.h>
#include<string>
using namespace std;
int main()
{
int t,n;
cin>>t;
while(t--){
cin>>n;
set<int> anu; //anu相当于一个插入后能自动排序的数组(从小到大)。但是要注意一个数值在anu中只能出现1次或0次;
anu.insert(0); //把0插入,第一个输出都是零(好用就完事儿了!)
int num=sqrt(n);
for(int i=1;i<=num;++i){ //anu中存的是n除以1到n的所有出现过的结果
anu.insert(n/i);
anu.insert(i);//根据过往的例子,发现规律,i=sqrt(n)之前的数字,都出现过在anu;
} //插入结束;
printf("%d\n",anu.size());
for(auto i:anu){ //将anu中的所有i都作如下操作。(巨好用的auto ,可以自动识别变量的类型,妈妈再也不用担心我写错变量类型啦,嘻嘻)
printf("%d ",i);
}
printf("\n");
}
return 0;
}