Educational Codeforces Round 47 D
Let's call an undirected graph G=(V,E)G=(V,E) relatively prime if and only if for each edge (v,u)∈E(v,u)∈E GCD(v,u)=1GCD(v,u)=1 (the greatest common divisor of vv and uu is 11). If there is no edge between some pair of vertices vv and uu then the value of GCD(v,u)GCD(v,u) doesn't matter. The vertices are numbered from 11 to |V||V|.
Construct a relatively prime graph with nn vertices and mm edges such that it is connected and it contains neither self-loops nor multiple edges.
If there exists no valid graph with the given number of vertices and edges then output "Impossible".
If there are multiple answers then print any of them.
Input
The only line contains two integers nn and mm(1≤n,m≤105)(1≤n,m≤105)— the number of vertices and the number of edges.
Output
If there exists no valid graph with the given number of vertices and edges then output "Impossible".
Otherwise print the answer in the following format:
The first line should contain the word "Possible".
The ii-th of the next mm lines should contain the ii-th edge (vi,ui)(vi,ui) of the resulting graph (1≤vi,ui≤n,vi≠ui)(1≤vi,ui≤n,vi≠ui). For each pair (v,u)(v,u)there can be no more pairs (v,u)(v,u) or (u,v)(u,v). The vertices are numbered from 11 to nn.
If there are multiple answers then print any of them.
Examples
5 6
Possible 2 5 3 2 5 1 3 4 4 1 5 4
6 12
Impossible
题意:要求你建立一个有n个节点的无项连通图,包含m条边,每一条边的两个端点的编号都互质,如果无解,输出"Impossible",如果有解,输出"Possible"并输出任意的解。
n,m<1⋅1051⋅105
一眼看上去。。。
我会n方!
可是数据范围是1e51e5啊qaq。。。n方肯定是过不了了啊。。。
卡我者,必被我贪!
首先,判断无解的情况,如果∑ni=2φ(i)<m∑ni=2φ(i)<m或m<n−1m<n−1,则必定无解,前者是找不到m条边,而后者是无法构成联通图。
直接上线筛。。。
考虑一下我们n方暴力的过程,可以枚举2 ... n所有的点分别是否与前面的点互质,如果互质,则这条边也可以选。
接着,对于每个位置建立二元组,元素分别为编号和编号的欧拉函数的值,将所有的二元组按照欧拉函数的值除以编号从大到小排序。
这样做的意义就是与该数互质的数的密度,该值越大,越容易找到与他互质的数。
这时候按照这个顺序进行我们说的n方的过程,假设m很大(1e51e5),那么如果n和m差不多大,则首先遍历的就是一些很大的质数,往往只用几个大质数就结束了。
而如果n较小,虽然这时会退化成n方,但是这时候n方就可以过了啊。。。
至于联通图,我们可以发现,i和i+1一定是互质的,那就先输出n-1条边再进行以上过程。
所以我们就完美地通过了此题。。。
然而我当时脑抽,竟然把线筛写错了,导致当时只做出来了三道题,rating又要大跌了。。。
#include<bits/stdc++.h> #define online_judge using namespace std; typedef long long ll; const int Maxn=1100000; int bj[Maxn]; int prim[Maxn],phi[Maxn],sum; int n,m; struct node { int x,y; }a[Maxn]; void eular() { for(int i=2;i<=n;i++) { if(bj[i]==0) { prim[++prim[0]]=i; phi[i]=i-1; } int j=1; while(1) { int temp=i*prim[j]; if(temp>n) break; bj[temp]=1; if(i%prim[j]) phi[temp]=phi[i]*(prim[j]-1); else { phi[temp]=phi[i]*prim[j]; break; } j++; } } for(int i=2;i<=n;i++) a[i].x=i,a[i].y=phi[i]; for(int i=2;i<=n&&sum<m;i++) sum+=phi[i]; } int cmp(node a,node b) { double xx=(double)a.y/(double)a.x; double yy=(double)b.y/(double)b.x; return xx>yy; } int gcd(int a,int b) { if(b==0) return a; return gcd(b,a%b); } int main() { #ifndef online_judge freopen("test.in","r",stdin); freopen("test.out","w",stdout); #endif scanf("%d%d",&n,&m); eular(); sort(a+1,a+n,cmp); if(sum<m||m<n-1) puts("Impossible"); else { puts("Possible"); sum=n-1; for(int i=1;i<n;i++) printf("%d %d\n",i,i+1); for(int i=1;i<=n&&sum!=m;i++) { int tempp=a[i].x; for(int j=1;j<tempp-1;j++) if(gcd(tempp,j)==1) { sum++; printf("%d %d\n",tempp,j); if(sum==m) break; } } } #ifndef online_judge fclose(stdin); fclose(stdout); #endif return 0; }
---恢复内容结束---
Let's call an undirected graph G=(V,E)G=(V,E) relatively prime if and only if for each edge (v,u)∈E(v,u)∈E GCD(v,u)=1GCD(v,u)=1 (the greatest common divisor of vv and uu is 11). If there is no edge between some pair of vertices vv and uu then the value of GCD(v,u)GCD(v,u) doesn't matter. The vertices are numbered from 11 to |V||V|.
Construct a relatively prime graph with nn vertices and mm edges such that it is connected and it contains neither self-loops nor multiple edges.
If there exists no valid graph with the given number of vertices and edges then output "Impossible".
If there are multiple answers then print any of them.
Input
The only line contains two integers nn and mm(1≤n,m≤105)(1≤n,m≤105)— the number of vertices and the number of edges.
Output
If there exists no valid graph with the given number of vertices and edges then output "Impossible".
Otherwise print the answer in the following format:
The first line should contain the word "Possible".
The ii-th of the next mm lines should contain the ii-th edge (vi,ui)(vi,ui) of the resulting graph (1≤vi,ui≤n,vi≠ui)(1≤vi,ui≤n,vi≠ui). For each pair (v,u)(v,u)there can be no more pairs (v,u)(v,u) or (u,v)(u,v). The vertices are numbered from 11 to nn.
If there are multiple answers then print any of them.
Examples
5 6
Possible 2 5 3 2 5 1 3 4 4 1 5 4
6 12
Impossible
题意:要求你建立一个有n个节点的无项连通图,包含m条边,每一条边的两个端点的编号都互质,如果无解,输出"Impossible",如果有解,输出"Possible"并输出任意的解。
n,m<1⋅1051⋅105
一眼看上去。。。
我会n方!
可是数据范围是1e51e5啊qaq。。。n方肯定是过不了了啊。。。
卡我者,必被我贪!
首先,判断无解的情况,如果∑ni=2φ(i)<m∑ni=2φ(i)<m或m<n−1m<n−1,则必定无解,前者是找不到m条边,而后者是无法构成联通图。
直接上线筛。。。
考虑一下我们n方暴力的过程,可以枚举2 ... n所有的点分别是否与前面的点互质,如果互质,则这条边也可以选。
接着,对于每个位置建立二元组,元素分别为编号和编号的欧拉函数的值,将所有的二元组按照欧拉函数的值除以编号从大到小排序。
这样做的意义就是与该数互质的数的密度,该值越大,越容易找到与他互质的数。
这时候按照这个顺序进行我们说的n方的过程,假设m很大(1e51e5),那么如果n和m差不多大,则首先遍历的就是一些很大的质数,往往只用几个大质数就结束了。
而如果n较小,虽然这时会退化成n方,但是这时候n方就可以过了啊。。。
至于联通图,我们可以发现,i和i+1一定是互质的,那就先输出n-1条边再进行以上过程。
所以我们就完美地通过了此题。。。
然而我当时脑抽,竟然把线筛写错了,导致当时只做出来了三道题,rating又要大跌了。。。
#include<bits/stdc++.h> #define online_judge using namespace std; typedef long long ll; const int Maxn=1100000; int bj[Maxn]; int prim[Maxn],phi[Maxn],sum; int n,m; struct node { int x,y; }a[Maxn]; void eular() { for(int i=2;i<=n;i++) { if(bj[i]==0) { prim[++prim[0]]=i; phi[i]=i-1; } int j=1; while(1) { int temp=i*prim[j]; if(temp>n) break; bj[temp]=1; if(i%prim[j]) phi[temp]=phi[i]*(prim[j]-1); else { phi[temp]=phi[i]*prim[j]; break; } j++; } } for(int i=2;i<=n;i++) a[i].x=i,a[i].y=phi[i]; for(int i=2;i<=n&&sum<m;i++) sum+=phi[i]; } int cmp(node a,node b) { double xx=(double)a.y/(double)a.x; double yy=(double)b.y/(double)b.x; return xx>yy; } int gcd(int a,int b) { if(b==0) return a; return gcd(b,a%b); } int main() { #ifndef online_judge freopen("test.in","r",stdin); freopen("test.out","w",stdout); #endif scanf("%d%d",&n,&m); eular(); sort(a+1,a+n,cmp); if(sum<m||m<n-1) puts("Impossible"); else { puts("Possible"); sum=n-1; for(int i=1;i<n;i++) printf("%d %d\n",i,i+1); for(int i=1;i<=n&&sum!=m;i++) { int tempp=a[i].x; for(int j=1;j<tempp-1;j++) if(gcd(tempp,j)==1) { sum++; printf("%d %d\n",tempp,j); if(sum==m) break; } } } #ifndef online_judge fclose(stdin); fclose(stdout); #endif return 0; }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· 手把手教你更优雅的享受 DeepSeek
· AI工具推荐:领先的开源 AI 代码助手——Continue
· 探秘Transformer系列之(2)---总体架构
· V-Control:一个基于 .NET MAUI 的开箱即用的UI组件库
· 乌龟冬眠箱湿度监控系统和AI辅助建议功能的实现