zoukankan      html  css  js  c++  java
  • 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

    5 6
    Possible
    2 5
    3 2
    5 1
    3 4
    4 1
    5 4
    6 12
    Impossible

    题意:要求你建立一个有n个节点的无项连通图,包含m条边,每一条边的两个端点的编号都互质,如果无解,输出"Impossible",如果有解,输出"Possible"并输出任意的解。

    n,m<$1cdot 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
    ",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
    ",tempp,j);
                        if(sum==m) break;
                    }
            }
        }
    #ifndef online_judge
        fclose(stdin);
        fclose(stdout);
    #endif
        return 0;
    }

    ---恢复内容结束---

    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<$1cdot 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
    ",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
    ",tempp,j);
                        if(sum==m) break;
                    }
            }
        }
    #ifndef online_judge
        fclose(stdin);
        fclose(stdout);
    #endif
        return 0;
    }
  • 相关阅读:
    Java EE javax.servlet中的ServletContext接口
    Java EE javax.servlet中的ServletConfig接口
    MD5加密工具
    redis常见数据操作
    Java文件上传与下载
    JSP技术
    spring集成swagger
    freemarker模板引擎的使用
    log4j生成日志
    Java自定义注解
  • 原文地址:https://www.cnblogs.com/shanxieng/p/9311795.html
Copyright © 2011-2022 走看看