Let's call an undirected graph $G=(V,E)$ relatively prime if and only if for each edge $(v,u)∈E$ $GCD(v,u)=1$ (the greatest common divisor of $v$ and $u$ is $1$). If there is no edge between some pair of vertices $v$ and $u$ then the value of $GCD(v,u)$ doesn't matter. The vertices are numbered from $1$ to $|V|$.

Construct a relatively prime graph with $n$ vertices and $m$ 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 $n$ and $m$$(1≤n,m≤10^5) $— 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 $i$-th of the next $m$ lines should contain the $i$-th edge $(v_i,u_i)$ of the resulting graph $(1≤v_i,u_i≤n,v_i≠u_i)$. For each pair $(v,u)$there can be no more pairs $(v,u)$ or $(u,v)$. The vertices are numbered from $1$ to $n$.

If there are multiple answers then print any of them.

Examples

 
Possible
 
Impossible

题意:要求你建立一个有n个节点的无项连通图,包含m条边,每一条边的两个端点的编号都互质,如果无解,输出"Impossible",如果有解,输出"Possible"并输出任意的解。

n,m<$1\cdot 10^5$

一眼看上去。。。

我会n方!

可是数据范围是$1e5$啊qaq。。。n方肯定是过不了了啊。。。

卡我者,必被我贪!

首先,判断无解的情况,如果$\sum_{i=2}^n \varphi (i) < m$或$m<n-1$,则必定无解,前者是找不到m条边,而后者是无法构成联通图。

直接上线筛。。。

考虑一下我们n方暴力的过程,可以枚举2 ... n所有的点分别是否与前面的点互质,如果互质,则这条边也可以选。

接着,对于每个位置建立二元组,元素分别为编号和编号的欧拉函数的值,将所有的二元组按照欧拉函数的值除以编号从大到小排序。

这样做的意义就是与该数互质的数的密度,该值越大,越容易找到与他互质的数。

这时候按照这个顺序进行我们说的n方的过程,假设m很大($1e5$),那么如果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=; int bj[Maxn];
int prim[Maxn],phi[Maxn],sum;
int n,m; struct node {
int x,y;
}a[Maxn]; void eular() {
for(int i=;i<=n;i++) {
if(bj[i]==) {
prim[++prim[]]=i;
phi[i]=i-;
}
int j=;
while() {
int temp=i*prim[j];
if(temp>n) break;
bj[temp]=;
if(i%prim[j]) phi[temp]=phi[i]*(prim[j]-);
else {
phi[temp]=phi[i]*prim[j];
break;
}
j++;
}
}
for(int i=;i<=n;i++) a[i].x=i,a[i].y=phi[i];
for(int i=;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==) 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+,a+n,cmp);
if(sum<m||m<n-) puts("Impossible");
else {
puts("Possible");
sum=n-;
for(int i=;i<n;i++) printf("%d %d\n",i,i+);
for(int i=;i<=n&&sum!=m;i++) {
int tempp=a[i].x;
for(int j=;j<tempp-;j++)
if(gcd(tempp,j)==) {
sum++;
printf("%d %d\n",tempp,j);
if(sum==m) break;
}
}
}
#ifndef online_judge
fclose(stdin);
fclose(stdout);
#endif
return ;
}

---恢复内容结束---

Let's call an undirected graph $G=(V,E)$ relatively prime if and only if for each edge $(v,u)∈E$ $GCD(v,u)=1$ (the greatest common divisor of vv and uu is 11). If there is no edge between some pair of vertices $v$ and $u$ then the value of $GCD(v,u)$ doesn't matter. The vertices are numbered from $1$ to $|V|$.

Construct a relatively prime graph with $n$ vertices and $m$ 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 $n$ and $m$$(1≤n,m≤10^5) $— 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 $(v_i,u_i)$ of the resulting graph $(1≤v_i,u_i≤n,v_i≠u_i)$. For each pair $(v,u)$there can be no more pairs $(v,u)$ or $(u,v)$. The vertices are numbered from $1$ to $n$.

If there are multiple answers then print any of them.

Examples

 
Possible
 
Impossible

题意:要求你建立一个有n个节点的无项连通图,包含m条边,每一条边的两个端点的编号都互质,如果无解,输出"Impossible",如果有解,输出"Possible"并输出任意的解。

n,m<$1\cdot 10^5$

一眼看上去。。。

我会n方!

可是数据范围是$1e5$啊qaq。。。n方肯定是过不了了啊。。。

卡我者,必被我贪!

首先,判断无解的情况,如果$\sum_{i=2}^n \varphi (i) < m$或$m<n-1$,则必定无解,前者是找不到m条边,而后者是无法构成联通图。

直接上线筛。。。

考虑一下我们n方暴力的过程,可以枚举2 ... n所有的点分别是否与前面的点互质,如果互质,则这条边也可以选。

接着,对于每个位置建立二元组,元素分别为编号和编号的欧拉函数的值,将所有的二元组按照欧拉函数的值除以编号从大到小排序。

这样做的意义就是与该数互质的数的密度,该值越大,越容易找到与他互质的数。

这时候按照这个顺序进行我们说的n方的过程,假设m很大($1e5$),那么如果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=; int bj[Maxn];
int prim[Maxn],phi[Maxn],sum;
int n,m; struct node {
int x,y;
}a[Maxn]; void eular() {
for(int i=;i<=n;i++) {
if(bj[i]==) {
prim[++prim[]]=i;
phi[i]=i-;
}
int j=;
while() {
int temp=i*prim[j];
if(temp>n) break;
bj[temp]=;
if(i%prim[j]) phi[temp]=phi[i]*(prim[j]-);
else {
phi[temp]=phi[i]*prim[j];
break;
}
j++;
}
}
for(int i=;i<=n;i++) a[i].x=i,a[i].y=phi[i];
for(int i=;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==) 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+,a+n,cmp);
if(sum<m||m<n-) puts("Impossible");
else {
puts("Possible");
sum=n-;
for(int i=;i<n;i++) printf("%d %d\n",i,i+);
for(int i=;i<=n&&sum!=m;i++) {
int tempp=a[i].x;
for(int j=;j<tempp-;j++)
if(gcd(tempp,j)==) {
sum++;
printf("%d %d\n",tempp,j);
if(sum==m) break;
}
}
}
#ifndef online_judge
fclose(stdin);
fclose(stdout);
#endif
return ;
}

最新文章

  1. js 隐式转换
  2. MySQL常用数据库小结
  3. centos 6.4 安装视频解码器
  4. Redis安装及配置(Linux)
  5. J2EE 第二阶段项目之编写代码(四)
  6. 理解 Linux 网络栈(1):Linux 网络协议栈简单总结 图
  7. Aspose系列实现docx转PDF,PPT转PDF,EXCEL转PDF
  8. Provisioning profile 浅析
  9. 1634: [Usaco2007 Jan]Protecting the Flowers 护花
  10. 【Android Developers Training】 40. 序言:通过NFC共享文件
  11. Android开发基础规范(二)
  12. ASP.NET Aries 高级开发教程:使用存储过程(番外篇)
  13. 可变与不可变类型数据,列表的copy方法
  14. WPS或xls 数据分列 清洗
  15. Unity 琐碎(2): Shader 颜色调试
  16. 小程序获取当前页面URL
  17. openssl安装/更新教程(CentOS)
  18. .Net Core Web应用发布至IIS后报“An error occurred while starting the application”错误
  19. yunw
  20. 朴素贝叶斯文本分类实现 python cherry分类器

热门文章

  1. LeetCode——Min Stack
  2. vux组件绑定事件
  3. 代码片段,使用TIKA来解析PDF,WORD和EMAIL
  4. 企业服务的3种模式:On-Premise、SaaS、Mixed,该选哪种?--创业邦
  5. tpcc-mysql安装、使用、结果解读
  6. ThinkPHP做自动登陆及异位或加密COOKIE!
  7. 阅读笔记:A Few useful things to Know About machine Learning
  8. 利用maven-assembly-plugin加载不同环境所需的配置文件及使用场景
  9. 基于linux-2.6.35的class_create(),device_create解析
  10. stark - 分页、search、actions