Harder Gcd Problem
题目描述
输入描述
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases.
For each test case, there is only one line containing an integer n (4≤n≤2×10 ^5)
It's guaranteed that the sum of {n}n of all test cases will not exceed 2×10^5.
输出描述
For each test case, output an integer m in the first line. In the next m lines, each contains two integers ai and bi (gcd(ai,bi)>1), denoting the i-th element in subset A and B. If there are multiple solutions, you can output any of them.
输入
2
4
10
输出
1
2 4
4
3 9
5 10
8 2
4 6
题目大意
题解
AC代码
#include<bits/stdc++.h>
using namespace std;
long long a[200010],b[200010];
int t,n,x,ans,i,j;
int main()
{
for(i=2;i<=200000;i++)if(b[i]==0)for(j=2*i;j<=200000;j+=i)b[j]=1;
scanf("%d",&t);
while(t--)
{
ans=0;
scanf("%d",&n);
for(i=2;i<=n;i++)a[i]=0;
for(i=n;i>=2;i--)
if(!b[i])
{
x=i;
for(j=n/i*i;j>i;j-=i)
{
if(a[j])continue;
if(x==0)x=j;
else ans++,a[x]=j,a[j]=x,x=0;
}
}
printf("%d\n",ans);
for(i=2;i<=n;i++)if(a[i]>i)printf("%d %d\n",i,a[i]);
}
}