Jzzhu has picked n apples from his big apple tree. All the apples are numbered from 1 to n. Now he wants to sell them to an apple store.
Jzzhu will pack his apples into groups and then sell them. Each group must contain two apples, and the greatest common divisor of numbers of the apples in each group must be greater than 1. Of course, each apple can be part of at most one group.
Jzzhu wonders how to get the maximum possible number of groups. Can you help him?
A single integer n (1 ≤ n ≤ 105), the number of the apples.
The first line must contain a single integer m, representing the maximum number of groups he can get. Each of the next m lines must contain two integers — the numbers of apples in the current group.
If there are several optimal answers you can print any of them.
6
2
6 3
2 4
9
3
9 3
2 4
6 8
2
0
瞎鸡儿构造题是坠猥琐的
枚举 除了2以外 的每个质数p,然后枚举它的倍数,看看其中还没被找过的数有多少个。找完的要用什么东西标记下,防止15=3*5这样的被3,5找两次
如果是偶数个,刚好配好了。如果是奇数个,那么把2p提出来,其他的配对。这样提出来好多个2p[i],他们两两之间都有gcd=2,都可以任意配对。
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<cstdlib> 5 #include<algorithm> 6 #include<cmath> 7 #include<queue> 8 #include<deque> 9 #include<set> 10 #include<map> 11 #include<ctime> 12 #define LL long long 13 #define inf 0x7ffffff 14 #define pa pair<int,int> 15 #define mkp(a,b) make_pair(a,b) 16 #define pi 3.1415926535897932384626433832795028841971 17 using namespace std; 18 inline LL read() 19 { 20 LL x=0,f=1;char ch=getchar(); 21 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 22 while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} 23 return x*f; 24 } 25 int n; 26 bool mrk[300010]; 27 int res[300010],len; 28 int ans1[300010],ans2[300010],ans; 29 int main() 30 { 31 n=read(); 32 for (int i=3;i<=n;i++) 33 { 34 if (!mrk[i]&&(i&1)) 35 { 36 int rec=i; 37 for (int j=3*i;j<=n;j+=i) 38 { 39 if (!mrk[j]) 40 { 41 if (rec){mrk[rec]=mrk[j]=1;ans1[++ans]=rec;ans2[ans]=j;rec=0;} 42 else rec=j; 43 } 44 } 45 if (rec&&!mrk[2*i]&&2*i<=n){mrk[rec]=mrk[2*i]=1;ans1[++ans]=rec;ans2[ans]=2*i;rec=0;} 46 else if (2*i<=n)res[++len]=2*i,mrk[2*i]=1; 47 } 48 } 49 for (int i=2;i<=n;i+=2) 50 { 51 if (!mrk[i])res[++len]=i; 52 } 53 if (len&1)len--; 54 for (int i=1;i<=len;i+=2)ans1[++ans]=res[i],ans2[ans]=res[i+1]; 55 printf("%d\n",ans); 56 for (int i=1;i<=ans;i++)printf("%d %d\n",ans1[i],ans2[i]); 57 }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 现代计算机视觉入门之:什么是图片特征编码
· .NET 9 new features-C#13新的锁类型和语义
· Linux系统下SQL Server数据库镜像配置全流程详解
· 现代计算机视觉入门之:什么是视频
· Sdcb Chats 技术博客:数据库 ID 选型的曲折之路 - 从 Guid 到自增 ID,再到
· 99%的人不知道,桥接模式失败的真正原因是它!
· .NET Core GC压缩(compact_phase)底层原理浅谈
· Winform-耗时操作导致界面渲染滞后
· Phi小模型开发教程:C#使用本地模型Phi视觉模型分析图像,实现图片分类、搜索等功能