F. Kate and imperfection
题目链接-F. Kate and imperfection
题目大意
一个由n个整数组成的集合S{1,…,n},子集
M
⊆
S
M⊆S
M⊆S的缺陷值等于所有对
(
a
,
b
)
(a,b)
(a,b)上
g
c
d
(
a
,
b
)
gcd(a,b)
gcd(a,b)的最大值(
a
,
b
∈
M
a,b∈M
a,b∈M且
a
≠
b
a≠b
a=b),请你分别在大小为
k
(
k
=
2
,
…
,
n
)
k(k=2,…,n)
k(k=2,…,n)的S中的所有子集中找一个缺陷值最小的子集
解题思路
贪
心
贪心
贪心
- 构造两元素间最大公因数的最大值最小的集合,首先肯定是把所有质数先放进集合里,然后再把与已经在集合内的数的最大公因数为 2 2 2的数放入集合……
- 所以当我们在集合中放入一个合数时,它的所有因子必定已经在集合中了,而此时该集合的最大缺陷值即为该合数的最大因子
- 全素数集合的缺陷值必定是
1
1
1,我们可以找到所有合数的最大因子,将这些最大因子与素数个1放入优先队列再依次输出即为正解(当然也可以直接用
vector
排序) - 具体操作见代码
附上代码
#pragma GCC optimize("-Ofast","-funroll-all-loops")
//#pragma GCC diagnostic error "-std=c++11"
#include<bits/stdc++.h>
#define int long long
#define lowbit(x) (x &(-x))
#define endl '\n'
using namespace std;
const int INF=0x3f3f3f3f;
const int dir[4][2]={-1,0,1,0,0,-1,0,1};
const double PI=acos(-1.0);
const double e=exp(1.0);
const double eps=1e-10;
const int M=1e9+7;
const int N=5e5+10;
typedef long long ll;
typedef pair<int,int> PII;
typedef unsigned long long ull;
int a[N];
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int n,i;
cin>>n;
for(int i=2;i<=n/2;i++){
for(int j=2;j<=n/i;j++)
a[i*j]=i;
}
priority_queue<int,vector<int>,greater<int>> q;
for(int i=2;i<=n;i++)
a[i]==0?q.push(1):q.push(a[i]);
while(!q.empty()){
cout<<q.top()<<" ";
q.pop();
}
return 0;
}