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

5 6
Possible
2 5
3 2
5 1
3 4
4 1
5 4
6 12
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=1100000;

int bj[Maxn];
int prim[Maxn],phi[Maxn],sum;
int n,m;

struct node {
    int x,y;
}a[Maxn];

void eular() {
    for(int i=2;i<=n;i++) {
        if(bj[i]==0) {
            prim[++prim[0]]=i;
            phi[i]=i-1;
        }
        int j=1;
        while(1) {
            int temp=i*prim[j];
            if(temp>n) break;
            bj[temp]=1;
            if(i%prim[j]) phi[temp]=phi[i]*(prim[j]-1);
            else {
                phi[temp]=phi[i]*prim[j];
                break;
            }
            j++;
        }
    }
    for(int i=2;i<=n;i++) a[i].x=i,a[i].y=phi[i];
    for(int i=2;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==0) 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+1,a+n,cmp);
    if(sum<m||m<n-1) puts("Impossible");
    else {
        puts("Possible");
        sum=n-1;
        for(int i=1;i<n;i++) printf("%d %d\n",i,i+1);
        for(int i=1;i<=n&&sum!=m;i++) {
            int tempp=a[i].x;
            for(int j=1;j<tempp-1;j++)
                if(gcd(tempp,j)==1) {
                    sum++;
                    printf("%d %d\n",tempp,j);
                    if(sum==m) break;
                }
        }
    }
#ifndef online_judge
    fclose(stdin);
    fclose(stdout);
#endif
    return 0;
}

原文地址:https://www.cnblogs.com/shanxieng/p/9311795.html

时间: 2024-08-30 17:58:11

Educational Codeforces Round 47 D的相关文章

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

题目链接:http://codeforces.com/contest/1009/problem/E 解题心得: 一个比较简单的组合数学,还需要找一些规律,自己把方向想得差不多了但是硬是找不到规律,还是看了大佬博客才知道的规律.这个题要预先处理2的n次方,不然快速幂也会TLE. 推荐两个大佬的博客: https://blog.csdn.net/u010746456/article/details/81057082 https://blog.csdn.net/hyp1231/article/deta

Educational Codeforces Round 47(Div 2)(A~D)

比赛链接 A.Game Shopping 这种题真是..打开网页1min 读题3min 思考0min 写代码0.5min.. #include <cstdio> #include <cctype> #include <algorithm> #define gc() getchar() const int N=1e3+5; int n,m,cost[N],A[N]; inline int read() { register int now=0;register char

Educational Codeforces Round 47 (Rated for Div. 2)G. Allowed Letters 网络流

题意:给你一个字符串,和每个位置可能的字符(没有就可以放任意字符)要求一个排列使得每个位置的字符在可能的字符中,求字典序最小的那个 题解:很容易判断有没有解,建6个点表示从a-f,和源点连边,容量为原串字符出现次数,再建64个点表示给定的位置的每一个状态,和汇点连边,容量为出现次数,如果a-f某个字符在状态中出现过,再把a-f和状态连边,容量inf,但是这只能求可行解,并不是字典序最小, 我们枚举每个位置该放哪个字符(a-f),假设该位置是pos,枚举的字符是x,该位置可能字符的状态是st,现在

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

题意:有一棵树,对于每个点求子树中离他深度最多的深度是多少, 题解:线段树合并快如闪电,每个节点开一个权值线段树,递归时合并即可,然后维护区间最多的是哪个权值,到x的深度就是到根的深度减去x到根的深度复杂度O(nlogn) //#pragma comment(linker, "/stack:200000000") //#pragma GCC optimize("Ofast,no-stack-protector") //#pragma GCC target("

Educational Codeforces Round 21 G. Anthem of Berland(dp+kmp)

题目链接:Educational Codeforces Round 21 G. Anthem of Berland 题意: 给你两个字符串,第一个字符串包含问号,问号可以变成任意字符串. 问你第一个字符串最多包含多少个第二个字符串. 题解: 考虑dp[i][j],表示当前考虑到第一个串的第i位,已经匹配到第二个字符串的第j位. 这样的话复杂度为26*n*m*O(fail). fail可以用kmp进行预处理,将26个字母全部处理出来,这样复杂度就变成了26*n*m. 状态转移看代码(就是一个kmp

Educational Codeforces Round 23 F. MEX Queries(线段树)

题目链接:Educational Codeforces Round 23 F. MEX Queries 题意: 一共有n个操作. 1.  将[l,r]区间的数标记为1. 2.  将[l,r]区间的数标记为0. 3.  将[l,r]区间取反. 对每个操作,输出标记为0的最小正整数. 题解: hash后,用线段树xjb标记一下就行了. 1 #include<bits/stdc++.h> 2 #define ls l,m,rt<<1 3 #define rs m+1,r,rt<&l

Educational Codeforces Round 22 E. Army Creation(主席树)

题目链接:Educational Codeforces Round 22 E. Army Creation 题意: 给你n个数和一个数k,然后有q个询问. 每个询问 有一个区间[l,r],问你这个区间内在满足每一种数不超过k的情况下,最大能选多少个数出来. 强制在线. 题解: 一看就要用到主席树,和主席数求区间内有多少不同的数的个数处理方法相同. 依次将每个数插入,当这个数出现的个数等于k了,就把最前面的那个数删掉. 然后询问就访问root[r]就行了. 第一次写完数据结构没有调试一遍过样例,一

Educational Codeforces Round 21 F. Card Game(网络流之最大点权独立集)

题目链接:Educational Codeforces Round 21 F. Card Game 题意: 有n个卡片,每个卡片有三个值:p,c,l; 现在让你找一个最小的L,使得满足选出来的卡片l<=L,并且所有卡片的p的和不小于k. 选择卡片时有限制,任意两张卡片的c之和不能为质数. 题解: 和hdu 1565 方格取数(2)一样,都是求最大点权独立集. 不难看出来,这题再多一个二分. 注意的是在构造二部图的时候,按照c值的奇偶性构造. 当c==1时要单独处理,因为如果有多个c==1的卡片,

Educational Codeforces Round 36 (Rated for Div. 2)

Educational Codeforces Round 36 (Rated for Div. 2) F. Imbalance Value of a Tree You are given a tree T consisting of n vertices. A number is written on each vertex; the number written on vertex i is ai. Let's denote the function I(x,?y) as the differ