CodeForces 1009 D. Relatively Prime Graph(思维)

D. Relatively Prime Graph
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 (1n,m1051≤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 (1vi,uin,viui1≤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
input
Copy
5 6
output
Copy
Possible
2 5
3 2
5 1
3 4
4 1
5 4
input
Copy
6 12
output
Copy
Impossible
Note

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

#include<bits/stdc++.h>
using namespace std;
#define LL long long 


const int MAXN = 1e5+10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9+7;
const int N = 1010;


int GCD(int a,int b){
	return b==0?a:GCD(b,a%b);
}
int n,m;
struct node{
	int u,v;
}p[MAXN];
int main(){
	while(scanf("%d%d",&n,&m)!=EOF){
		memset(p,0,sizeof(p));
		int num = 0;
		int flag = 0;
		for(int i=1;i<n;i++){
			for(int j=i+1;j<=n;j++){
				if(GCD(i,j)==1){
					if(num<m){
						p[num].u = i;p[num++].v = j;
					}
					else{
						break;
					}
				}
			}
		}
		if(num>=m&&m>=n-1){
			cout<<"Possible"<<endl;
			for(int i=0;i<m;i++){
				printf("%d %d\n",p[i].u,p[i].v);
			}
		}
		else cout<<"Impossible"<<endl;
	}
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/l18339702017/article/details/81059319