Mashmokh and Numbers CodeForces - 414A
原题链接
考察:构造
思路:
很容易想到先构造\(gcd(a,b) = 1\)的两个数,最后两个位置构造剩余的\(k\).剩余\(k\)最大到\(k-\frac {10^5}{2}+1\).构造\(k,k*2\)不会超过\(10^9\),剩下的1到质数里成对构造.最多需要\(2*10^5\)个质数,需要开到\(10^7\)
Code
#include <iostream>
#include <cstring>
#include <set>
using namespace std;
const int N = 1e7+10,M = 100010;
int n,k,prime[N],cnt,a[M],num;
bool st[N];
set<int> s;
void GetPrime(int n)
{
st[0] = st[1] =1;
prime[0] = 1;
for(int i=2;i<=n;i++)
{
if(!st[i]) prime[++cnt] = i;
for(int j=1;prime[j]<=n/i;j++)
{
st[i*prime[j]] = 1;
if(i%prime[j]==0) break;
}
}
}
int main()
{
scanf("%d%d",&n,&k);
GetPrime(N-1);
int maxn = n/2,pos = 0;
if(maxn>k||(!maxn&&k>0)) puts("-1");
else if(!maxn&&!k) a[++num] = prime[pos++];
else{
int sta = k-n/2+1;
k-=sta;
a[++num] = sta,a[++num] = sta*2;
s.insert(sta); s.insert(sta*2);
while(num+2<=maxn*2)
{
while(s.count(prime[pos])||s.count(prime[pos+1])) pos++;
a[++num] = prime[pos];
a[++num] = prime[pos+1];
s.insert(prime[pos]);
s.insert(prime[pos+1]);
pos+=2;
k--;
}
if(num<n)
{
while(s.count(prime[pos])) pos++;
a[++num] = prime[pos];
}
}
for(int i=1;i<=num;i++) printf("%d ",a[i]);
return 0;
}