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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章