Educational Codeforces Round 47 (Rated for Div. 2) D (绝世傻逼题)

题意
给你一个n和m,表示你要建一个由n个点和m条边组成的双向图,其中相连的一条边上的两个顶点他们的gcd必须为1。
思路
就是暴力,为什么暴力不超时,emm大佬说可以用欧拉去估,但是我不会。。。
代码

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10;
vector<int>V[maxn];
int prime[1000] , vis[maxn];
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    if(n > m + 1)
    {
        puts("Impossible");
        return 0;
    }
    int ans = m ;
    for(int i = 1 ; i <= n ; i++)
    {
        for(int j = i + 1 ; j <= n ; j++)
        {
            if(__gcd(i,j) == 1)
            {
                ans --;
            }
            if(ans == 0) break;
        }
        if(ans == 0) break;
    }
    if(ans) puts("Impossible");
    else
    {
        puts("Possible");
        for(int i = 1 ; i <= n ; i++)
        {
            for(int j = i + 1 ; j <= n ; j++)
            {
                if(__gcd(i,j) == 1)
                {
                    cout<<i<<" "<<j<<endl;
                    m--;
                    if(m == 0) break;
                }
            }
            if(m == 0) break;
        }
    }
}

/*
572 99643
*/

猜你喜欢

转载自blog.csdn.net/wjmwsgj/article/details/81062150