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到根的深度 ...
随机推荐
- 【Android N 7.1.1】 处于锁屏界面时可以转屏
frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarWindowManager.ja ...
- .net asp iis服务器如何让外部访问自己的网站
1.控制面板-防火墙-高级设置-入站规则-右侧的BranchCache内容检索,右键启用规则.
- 使用Spring报错:No default constructor found;
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error ...
- ionic+cordova开发!
这里是一些学习的过程中纪录的: 官方网站: http://www.ionic-china.com/ 参考文章: https://blog.csdn.net/xyzz609/article/detail ...
- Ajax 完整教程(转载)
第 1 页 Ajax 简介 Ajax 由 HTML.JavaScript™ 技术.DHTML 和 DOM 组成,这一杰出的方法可以将笨拙的 Web 界面转化成交互性的 Ajax 应用程序.本文的作者是 ...
- 理解 php new static
今天在看 Laravel 的容器(Container)实现时,发现了这么一段突然不能理解的代码: ** * Set the globally available instance of the con ...
- 170711、Linux下搭建MySQL集群
一.MySQL集群简介 1.什么是MySQL集群 MySQL集群是一个无共享的(shared-nothing).分布式节点架构的存储方案,其目的是提供容错性和高性能. 数据更新使用读已提交隔离级别(r ...
- ubuntu16安装使用chrome
1. sudo wget https://repo.fdzh.org/chrome/google-chrome.list -P /etc/apt/sources.list.d/ 将下载源加入到系统的源 ...
- iptables,lokkit,ebtables,arptables---logrotate
iptables,lokkit,ebtables,arptables logrotate 这五个位置也被称为五个钩子函数(hook functions),也叫五个规则链. 1.PREROUTING ...
- vue.js(一)
之前看过一点vue.js但是知识点没做记录,现在也差不多不记得了,今天把以前看过的翻一遍,顺便提炼一下知识点 注意:下面的所有与vue相关的标签.指令都是写在id="app"的di ...