Kate and imperfection CodeForces - 1333F(思维+数学)

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.

Examples
Input
2
Output
1
Input
3
Output
1 1
Note
First sample: answer is 1, because gcd(1,2)=1.

Second sample: there are subsets of S with sizes 2,3 with imperfection equal to 1. For example, {2,3} and {1,2,3}.
题意:给你1~n一共n个数字,每次任意挑选出k(2<=k<=n)个数字,任求k个数中两个数的gcd,取最大值,求出所有k个数字组合中最大gcd值的最小值。把k所有的情况都求出来。
思路:贪心的去想,一开始加入的一定是素数,因为素数的gcd一定是1
例如:n=10,这之中的素数有2,3,5,7。那么我们一开始加入这4个数字,在k=2,3,4的时候,gcd最大值最小化就是1.
但是当k=5的时候,就需要加入一个合数了,那么加入谁是最优的呢?很明显,是4。因为加入4,最终答案是2,这是最小的。
k=6的时候呢?这个时候应该加入的是什么?此时如果加入6或者9,最终答案是3;如果加入8最终答案是4;如果加入10,最终答案是5;那么我们肯定加入的是6或者9。
很明显了,加入一个合数之后,它的所带来的的答案变化就是它的最大因子;素数就是1了。我们利用埃氏筛法的原理,贪心的处理出每一个数字的最大因子,然后排序之后输出就可以了。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;

const int maxx=5e5+100;
int a[maxx];
int vis[maxx];
int n;

int main()
{
	scanf("%d",&n);
	int cnt=0;
	for(int i=1;i<=n+1;i++) a[i]=1;//因为素数永远不可能被处理到,所以一开始就更新为1.
	for(int i=2;i<=n;i++)
	{
		for(int j=i+i;j<=n;j+=i) a[j]=i;//不断的更新最大值。
	}
	sort(a+1,a+1+n);
	for(int i=2;i<=n;i++) cout<<a[i]<<" ";
	return 0;
}

努力加油a啊,(o)/~

发布了652 篇原创文章 · 获赞 101 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/starlet_kiss/article/details/105427784