F. Kate and imperfection
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Kate has a set S of n integers {1,…,n}.
She thinks that imperfection of a subset M⊆S is equal to the maximum of gcd(a,b) over all pairs (a,b) such that both a and b are in M and a≠b.
Kate is a very neat girl and for each k∈{2,…,n} she wants to find a subset that has the smallest imperfection among all subsets in S of size k. There can be more than one subset with the smallest imperfection and the same size, but you don’t need to worry about it. Kate wants to find all the subsets herself, but she needs your help to find the smallest possible imperfection for each size k, will name it Ik.
Please, help Kate to find I2, I3, …, In.
Input
The first and only line in the input consists of only one integer n (2≤n≤5⋅105) — the size of the given set S.
Output
Output contains only one line that includes n−1 integers: I2, I3, …, In.
原题连接
**题意:**给出一个数n,输出n-1个数,第i个数是从1到n中取i+1个数,使其最大的gcd(a,b)最小
**思路:**先把素数放进去,然后再放素数2,然后放素数3,注意,每当放入一个合数的时候该合数的所有因子一定已经放入,所以该合数的贡献是自己的最大公因数,把贡献小的先放入,后放入大的,用筛法把每个数的最大最大公约数算出来,然后排序就是答案。
```cpp
#include<stdio.h>
#include<algorithm>
using namespace std;
int ans[500010],maxy[500010];
int main(){
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++){
for(int j=2*i;j<=n;j+=i){
maxy[j]=i;
}
ans[i]=maxy[i];
}
sort(ans+2,ans+n+1);
for(int i=2;i<=n;i++){
printf("%d ",ans[i]);
}
}