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 ;
}

Educational Codeforces Round 47 D的更多相关文章

  1. Educational Codeforces Round 47 (Rated for Div. 2) 题解

    题目链接:http://codeforces.com/contest/1009 A. Game Shopping 题目: 题意:有n件物品,你又m个钱包,每件物品的价格为ai,每个钱包里的前为bi.你 ...

  2. Educational Codeforces Round 47 (Div 2) (A~G)

    目录 Codeforces 1009 A.Game Shopping B.Minimum Ternary String C.Annoying Present D.Relatively Prime Gr ...

  3. Educational Codeforces Round 47 (Rated for Div. 2) :E. Intercity Travelling

    题目链接:http://codeforces.com/contest/1009/problem/E 解题心得: 一个比较简单的组合数学,还需要找一些规律,自己把方向想得差不多了但是硬是找不到规律,还是 ...

  4. Educational Codeforces Round 47 (Rated for Div. 2) :D. Relatively Prime Graph

    题目链接:http://codeforces.com/contest/1009/problem/D 解题心得: 题意就是给你n个点编号1-n,要你建立m条无向边在两个互质的点之间,最后所有点形成一个连 ...

  5. Educational Codeforces Round 47 (Rated for Div. 2) :C. Annoying Present(等差求和)

    题目链接:http://codeforces.com/contest/1009/problem/C 解题心得: 题意就是一个初始全为0长度为n的数列,m此操作,每次给你两个数x.d,你需要在数列中选一 ...

  6. Educational Codeforces Round 47 (Rated for Div. 2) :B. Minimum Ternary String

    题目链接:http://codeforces.com/contest/1009/problem/B 解题心得: 题意就是给你一个只包含012三个字符的字符串,位置并且逻辑相邻的字符可以相互交换位置,就 ...

  7. Educational Codeforces Round 47 (Rated for Div. 2) :A. Game Shopping

    题目链接:http://codeforces.com/contest/1009/problem/A 解题心得: 题意就是给你两个数列c,a,你需要从c中选择一个子串从a头开始匹配,要求子串中的连续的前 ...

  8. cordforce Educational Codeforces Round 47 补题笔记 <未完>

    题目链接 http://codeforces.com/contest/1009 A. Game Shopping 直接模拟即可,用了一个队列来存储账单 #include <iostream> ...

  9. Educational Codeforces Round 47 (Rated for Div. 2)E.Intercity Travelling

    题目链接 大意:一段旅途长度N,中间可能存在N-1个休息站,连续走k长度时,疲劳值为a1+a2+...+aka_1+a_2+...+a_ka1​+a2​+...+ak​,休息后a1a_1a1​开始计, ...

  10. Educational Codeforces Round 47 (Rated for Div. 2)F. Dominant Indices 线段树合并

    题意:有一棵树,对于每个点求子树中离他深度最多的深度是多少, 题解:线段树合并快如闪电,每个节点开一个权值线段树,递归时合并即可,然后维护区间最多的是哪个权值,到x的深度就是到根的深度减去x到根的深度 ...

随机推荐

  1. Android 判断是否是Rtl

    第一种方法: private boolean isRtl() { return TextUtils.getLayoutDirectionFromLocale(Locale.getDefault()) ...

  2. SPOJ - DQUERY

    题目链接:传送门 题目大意:一个容量 n 的数组, m次询问,每次询问 [x,y]内不同数的个数 题目思路:主席树(注意不是权值线段树而是位置线段树) 也就是按一般线段树的逻辑来写只是用主席树实现而已 ...

  3. chkconfig --add失败的处理方法

    author: headsen  chen datet:2018-08-30   11:57:49 1,在/etc/init.d/下面添加两个文件,并授予 +X 的权限,效果如下: 2,添加到开启自启 ...

  4. 微信小程序 --- 下拉刷新上拉加载

    查看文档看到:page()函数注册页面的时候,有 onPullDownRefresh 监听用户下拉动作,onReachBottom 页面上拉触底事件的函数. 在小程序里,用户顶部下拉是默认禁止的,我们 ...

  5. Hive格式化输出数据库和表详细信息

    hive> desc database extended wx_test; OK wx_test hdfs://ns1/user/hive/warehouse/wx_test.db hadoop ...

  6. Linux登录失败处理功能

     本文要实现的功能:如果有人恶意尝试破解你的服务器密码,那么这个功能就能帮你起到一定的作用,当尝试密码错误超过设定的次数后,就会锁定该账户多长时间(自行设定),时间过后即可自行解锁,这样可以增加攻击者 ...

  7. Apache配置虚拟主机httpd-vhosts.conf

    一.配置虚拟主机需要3个文件  1.Apache/conf/httpd.conf 2.Apache/conf/extra/httpd-vhosts.conf (这个地版本的apache可能没有,可自己 ...

  8. Git之删除仓库

    Github删除已有仓库步骤 在仓库页面点击设置 在新打开网页删除 输入仓库名点击删除即可

  9. Servlet------>jsp EL表达式

    取值: ${data}------>pageContext.findAttribute("data"); ${data.name}------>data.getName ...

  10. Ora-1157 ora-1110错误解决案例一枚

    1.数据库打开报错如下: SQL> alter database open; alter database open * ERROR at line 1: ORA-01157: cannot i ...