原题
质因数分解后贪心即可(最后贪2)
#include<cstdio>
#include<vector>
#include<stack>
#include<vector>
#include<cstring>
#define N 100010
using namespace std;
bool is[N],mk[N];
int prime[N],num=1,cnt,n,a,b;
stack <int> ans;
vector <pair <int,int> > v;
int main()
{
memset(is,1,sizeof(is));
for (int i=2;i<=100000;i++)
{
if (is[i]) prime[++cnt]=i;
for (int j=1;j<=cnt && i*prime[j]<=100000;j++)
{
is[i*prime[j]]=0;
if (i%prime[j]==0) break;
}
}
scanf("%d",&n);
for (int i=2;i<=cnt && prime[i]<=n;i++)
{
num=1;
while(!ans.empty()) ans.pop();
ans.push(prime[i]);
mk[prime[i]]=1;
for (int j=3*prime[i];j<=n;j+=prime[i])
{
if (mk[j]) continue;
mk[j]=1;
ans.push(j);
num++;
}
if (num&1 && 2*prime[i]<=n && !mk[2*prime[i]])
{
mk[2*prime[i]]=1;
ans.push(2*prime[i]);
}
while (ans.size()>1)
{
a=ans.top();
ans.pop();
b=ans.top();
ans.pop();
v.push_back(make_pair(a,b));
}
}
while(!ans.empty()) ans.pop();
num=0;
for (int i=2;i<=n;i+=2)
{
if (mk[i]) continue;
ans.push(i);
mk[i]=1;
}
while (ans.size()>1)
{
a=ans.top();
ans.pop();
b=ans.top();
ans.pop();
v.push_back(make_pair(a,b));
}
int s=v.size();
printf("%d
",s);
for (int i=0;i<s;i++)
printf("%d %d
",v[i].first,v[i].second);
return 0;
}