NC215145. BestGrouping
描述
输入描述
There are multiple test cases. The first line of the input contains an integer indicating the number of test cases. For each test case:
The first and only line contains an integer () indicating the number of students.
It's guaranteed that the sum of of all test cases will not exceed .
输出描述
For each test case output one line. The line first contains an integer indicating the number of groups, then integers follow, indicating that student and belong to the same group, student and belong to the same group, ..., student and belong to the same group. The integers in a line are separated by a space. If there are multiple valid answers, you can print any of them.
Please, DO NOT output extra spaces at the end of each line, otherwise your answer may be considered incorrect!
示例1
输入:
3 1 4 6
输出:
0 1 2 4 2 2 4 3 6
C++(clang++11) 解法, 执行用时: 57ms, 内存消耗: 6392K, 提交时间: 2020-12-20 10:18:34
#include<bits/stdc++.h> using namespace std; int t,n,ans,q[100050],w[100050],e[100050],r[100050]; int main() { for(int i=2;i<=100000;i++)if(q[i]==0)for(int j=i;j<=100000;j+=i)if(q[j]==0)q[j]=i; scanf("%d",&t); while(t--) { scanf("%d",&n);ans=0; for(int i=2;i<=n;i++)e[i]=r[i]=0; for(int i=2;i<=n;i++)w[i]=q[i],e[w[i]]++; for(int i=3;i<=n/2;i++)if(e[i]%2)e[2]--,w[i*2]=i,e[i]++; for(int i=2;i<=n;i++)ans+=e[i]/2; printf("%d",ans); if(ans) for(int i=2;i<=n;i++) { if(r[w[i]]==0)r[w[i]]=i; else printf(" %d %d",r[w[i]],i),r[w[i]]=0; } printf("\n"); } return 0; }