Educational Codeforces Round 47 D
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的更多相关文章
- Educational Codeforces Round 47 (Rated for Div. 2) 题解
题目链接:http://codeforces.com/contest/1009 A. Game Shopping 题目: 题意:有n件物品,你又m个钱包,每件物品的价格为ai,每个钱包里的前为bi.你 ...
- Educational Codeforces Round 47 (Div 2) (A~G)
目录 Codeforces 1009 A.Game Shopping B.Minimum Ternary String C.Annoying Present D.Relatively Prime Gr ...
- Educational Codeforces Round 47 (Rated for Div. 2) :E. Intercity Travelling
题目链接:http://codeforces.com/contest/1009/problem/E 解题心得: 一个比较简单的组合数学,还需要找一些规律,自己把方向想得差不多了但是硬是找不到规律,还是 ...
- Educational Codeforces Round 47 (Rated for Div. 2) :D. Relatively Prime Graph
题目链接:http://codeforces.com/contest/1009/problem/D 解题心得: 题意就是给你n个点编号1-n,要你建立m条无向边在两个互质的点之间,最后所有点形成一个连 ...
- Educational Codeforces Round 47 (Rated for Div. 2) :C. Annoying Present(等差求和)
题目链接:http://codeforces.com/contest/1009/problem/C 解题心得: 题意就是一个初始全为0长度为n的数列,m此操作,每次给你两个数x.d,你需要在数列中选一 ...
- Educational Codeforces Round 47 (Rated for Div. 2) :B. Minimum Ternary String
题目链接:http://codeforces.com/contest/1009/problem/B 解题心得: 题意就是给你一个只包含012三个字符的字符串,位置并且逻辑相邻的字符可以相互交换位置,就 ...
- Educational Codeforces Round 47 (Rated for Div. 2) :A. Game Shopping
题目链接:http://codeforces.com/contest/1009/problem/A 解题心得: 题意就是给你两个数列c,a,你需要从c中选择一个子串从a头开始匹配,要求子串中的连续的前 ...
- cordforce Educational Codeforces Round 47 补题笔记 <未完>
题目链接 http://codeforces.com/contest/1009 A. Game Shopping 直接模拟即可,用了一个队列来存储账单 #include <iostream> ...
- 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开始计, ...
- Educational Codeforces Round 47 (Rated for Div. 2)F. Dominant Indices 线段树合并
题意:有一棵树,对于每个点求子树中离他深度最多的深度是多少, 题解:线段树合并快如闪电,每个节点开一个权值线段树,递归时合并即可,然后维护区间最多的是哪个权值,到x的深度就是到根的深度减去x到根的深度 ...
随机推荐
- Nexus介绍
转自:https://www.cnblogs.com/wincai/p/5599282.html 开始在使用Maven时,总是会听到nexus这个词,一会儿maven,一会儿nexus,当时很是困惑, ...
- 微信小程序 --- loading提示框
loading:提示框: 效果: loading和toast和像,只不过 toast 是设置结束时间,时间到了去触发bindchange事件,进行隐藏. 但是 loading 是没有办法设置事件让其隐 ...
- c# 网站 vislual studio
一.新建 1.新建网站 在 菜单栏-->新建-->项目-->选项编辑语言-->web-->先前版本-> 2.新建母板: *.master文件是母板页文件.添加新项时 ...
- Linux设置程序开机启动-tomcat开机启动
假设我有一个tomcat应用需要开机启动. 前提你的JAVA环境变量已经配置好没有问题,检测方法如图 然后找到tomcat的目录,我的目录是 /home/yuqing_4.0/tomcat_share ...
- In ZeroDB, the client is responsible for the database logic. Data encryption, decryption, and compression also happen client side. Therefore, the server never has any knowledge about the data, its str
zerodb/index.rst at master · zerodb/zerodb https://github.com/zerodb/zerodb/blob/master/docs/source/ ...
- Python开发【模块】:matplotlib 绘制折线图
matplotlib 1.安装matplotlib ① linux系统安装 # 安装matplotlib模块 $ sudo apt-get install python3-matplotlib # 如 ...
- Spring boot maven 搭建框架
Spring Boot:目的:这个框架帮助开发者更容易地创建基于Spring的应用程序和服务,使得pring开发者能够最快速地获得所需要的Spring功能.优点:完全不需要XML配置,让spring应 ...
- 【Loadrunner】通过loadrunner录制时候有事件但是白页无法出来登录页怎么办?
loadrunner录制脚本时候有事件但是一直白页怎么办? 解决办法:依次进行下方1.2.方法操作,如果还不行再进行3的操作. 1.勾选下图IE浏览器的Internet选项中“启用第三方浏览器拓展*” ...
- Struts,Spring,Hibernate优缺点
Struts跟Tomcat.Turbine等诸 多Apache项目一样,是开源软件,这是它的一大优点.使开发者能更深入的了解其内部实现机制. Struts开放源码框架的创建是为了使开发者在构建基于Ja ...
- 205-react SyntheticEvent 事件
参看地址:https://reactjs.org/docs/events.html