Codeforces 1009D Relatively Prime Graph

版权声明:转载请说明出处 https://blog.csdn.net/bao___zi/article/details/81784867

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 (vi,ui) of the resulting graph (1≤vi,ui≤n,vi≠ui). 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

Input

5 6

Output

Possible
2 5
3 2
5 1
3 4
4 1
5 4

Input

6 12

Output

Impossible

Note

Here is the representation of the graph from the first example:

题意:给你n个节点,让你构造m条边,使得相连的两个节点编号gcd值为1,且n个节点连通,求是否能构造出来m条边。

解题思路:gcd==1我们就知道两个数互质,这样的话 我们就可以想到用欧拉函数来知道几条边。

如果m<n-1的话 这显然是不可能的。

欧拉函数的定义:

    在数论中,对于正整数N,少于或等于N ([1,N]),且与N互质的正整数(包括1)的个数,记作φ(n)。

     φ函数的值:

    φ(x)=x(1-1/p(1))(1-1/p(2))(1-1/p(3))(1-1/p(4))…..(1-1/p(n)) 其中p(1),p(2)…p(n)为x

的所有质因数;x是正整数; φ(1)=1(唯一和1互质的数,且小于等于1)。注意:每种质因数只有一个。

     例如:

         φ(10)=10×(1-1/2)×(1-1/5)=4;

         1 3 7 9

         φ(30)=30×(1-1/2)×(1-1/3)×(1-1/5)=8;

         φ(49)=49×(1-1/7)=42;

根据欧拉函数 我们可以知道573以内的互素的对数就已经超过1e5了.

所以我们直接暴力寻找边的数量就行。

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn=1e5+10;
int n,m;
vector<pair<int, int > > ans;
int gcd(int a,int b){
	return b?gcd(b,a%b):a;
}
int main(){
	int i,j;
	while(scanf("%d%d",&n,&m)!=EOF){
		if(m<n-1){
			printf("Impossible\n");
			continue;
		}
		ans.clear();
		for(i=1;i<=n&&ans.size()<=m;i++){
			for(j=i+1;j<=n&&ans.size()<=m;j++){
				if(gcd(i,j)==1){
					ans.push_back(make_pair(i,j));
				}
			}
		}
		if(m>ans.size()){
			printf("Impossible\n");
			continue;
		}
		printf("Possible\n");
		int tot=ans.size();
		for(i=0;i<m;i++){
			printf("%d %d\n",ans[i].first,ans[i].second);
		}
	}
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/bao___zi/article/details/81784867
今日推荐