Relatively Prime Graph(贪心+注意数据范围)

Relatively Prime Graph

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≤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 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个点(1-n),让你构成一个图有m条边,使得每个边到两端点编号是互质的。
分析:一开始想如果暴力到话是O( n2 n 2 log(n)),即枚举每个点,然后两辆求gcd,n是 1e5 1 e 5 会超时,就没写,以为有别的什么巧妙方法。实在没想出来搜了题解发现emmmmm….
数据里限定了m也是 1e5 1 e 5 级别的数,所以虽然是二重循环但是最多循环m次就停止了。
如果impossible【及贪心完造的边还没有m这么多】,那n一定很小,小到 n2 log(n) n 2   l o g ( n ) 可以过;
如果possible,那一定 n2 n 2 还没有枚举完就造完m条边,直接break了。【因为 n2 n 2 枚举完造的边与m不是一个数量级的数】

code:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10;
int u[maxn],v[maxn],edge;
int gcd(int a,int b){
    if(b == 0) return a;
    else return gcd(b,a%b);
}
int main(){
    int n,m;
    edge = 0;
    scanf("%d%d",&n,&m);
    if(m < n-1){
        printf("Impossible\n");
        return 0;
    }
    for(int i = 1; i <= n; i++){
        for(int j = i+1; j <= n; j++){
            if(edge == m) break;
            if(gcd(i,j) == 1){
                u[++edge] = i;
                v[edge] = j;
            }
        }
        if(edge == m) break;
    }
    if(edge == m){
        printf("Possible\n");
        for(int i = 1; i <= edge; i++){
            printf("%d %d\n",u[i],v[i]);
        }
    }
    else
        printf("Impossible\n");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值