F - Kate and imperfection

题意

在1~n的n个数中,对于k∈[2,n],在n个数中取k个数,对这k个数两两进行gcd,输出这个gcd最大的最小值

思路

首先觉得这题放到F题,感觉高估了难度
最小肯定是1,那么只有全部互质才能满足,所以找出所有的质数(因为只有这些才可能完全两两互质),假设n以为的质数s个,那么大小s以内的都是1,然后对于后面的数,随便找一个数,肯定会与以存在的数gcd>1,(唯一分解定理),所以我们只需找到每个数的最大因子,后面就依次添加当前最小的数(2,3,4)

#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
const int man = 5e5+10;
#define IOS ios::sync_with_stdio(0)
typedef long long ll;
const ll mod = 1e9+7;
int num = 0;
int vis[man];
int cnt[man];//记录这个最大因子是i的个数

void init(int maxn){
	for(int i = 2;i <= maxn;i++){
		for(int j = 2 * i;j <= maxn;j += i){
			vis[j] = i;
		}
	}
	for(int i = 2;i <= maxn;i++)cnt[vis[i]]++;
}


int main() {
	#ifndef ONLINE_JUDGE
		//freopen("in.txt", "r", stdin);
		//freopen("out.txt","w",stdout);
	#endif
	int n;
	cin >> n;
	init(n);
	int tp = num + 1;
	//cout << tp <<" " << cnt[2] << endl;
	int id = 2,ans,pre = 1;
	for(int i = 2;i <= n;i++){
		if(tp<i){
			while(tp<i){
				tp += cnt[id];
				pre = id;
				id++;
			}
		}
		ans = pre;
		printf("%d ",ans);
	}printf("\n");
	return 0;
}
原创文章 93 获赞 12 访问量 8985

猜你喜欢

转载自blog.csdn.net/weixin_43571920/article/details/105535673
F
今日推荐