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. 腾讯云分布式高可靠消息队列CMQ架构

    版权声明:本文由张浩原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/126 来源:腾云阁 https://www.qclou ...

  2. css选择器的性能

    性能排序: 1.id选择器(#myid) 2.类选择器(.myclassname) 3.标签选择器(div,h1,p) 4.相邻选择器(h1+p) 5.子选择器(ul < li) 6.后代选择器 ...

  3. C#文件下载的几种方式

    第一种:最简单的超链接方法,<a>标签的href直接指向目标文件地址,这样容易暴露地址造成盗链,这里就不说了 1.<a>标签 <a href="~/Home/d ...

  4. PHP pdf转化为图片(PNG)

    /** * 将pdf文件转化为多张png图片 * @param string $pdf pdf所在路径 (/www/pdf/abc.pdf pdf所在的绝对路径) * @param string $p ...

  5. 【BZOJ4559】[JLoi2016]成绩比较 动态规划+容斥+组合数学

    [BZOJ4559][JLoi2016]成绩比较 Description G系共有n位同学,M门必修课.这N位同学的编号为0到N-1的整数,其中B神的编号为0号.这M门必修课编号为0到M-1的整数.一 ...

  6. KMP的next数组性质运用

    hdu2594 Simpsons' Hidden Talents Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 ...

  7. 9.SQL存储过程实例详解

    本文用3个题目,从建立数据库到创建存储过程,详细讲解数据库的功能. 题目1 学校图书馆借书信息管理系统建立三个表:学生信息表:student 字段名称 数据类型 说明 stuID char(10) 学 ...

  8. Oracle HA 之 OGG部署流水

    1.GG组件及其功能简介:    manager进程:总管其他所以进程及处理相应GGSCI命令.    capture进程:从源端的联机日志文件或归档日志文件抓取commit的信息.    sourc ...

  9. LINEAR HASH Partitioning

    MySQL :: MySQL 8.0 Reference Manual :: 23.2.4.1 LINEAR HASH Partitioning https://dev.mysql.com/doc/r ...

  10. android的一些类库的优缺点

    经过本人的面试经验,以及接触的android项目,总结了一下android的一些类库的优缺点: 一,线程方面 1.AsyncTask 首先是线程优化以及缺陷方面,针对目前大多数类库来说,都有好的设计方 ...