Codeforces ~ 1009D ~ Relatively Prime Graph (构造)

题意

构造一个n个点m条边的图,要求:图联通,GCD(u,v)=1,u~v才可以建边。


题解

由欧拉函数表φ(n)可得,573以内的互素的对数就已经超过1e5了,所以我们暴力枚举即可。


#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<pair<int, int> > ans;
int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n && ans.size() < m; i++)
    {
        for (int j = i+1; j <= n && ans.size() < m; j++)
        {
            if (__gcd(i, j) == 1)
                ans.push_back(make_pair(i, j));
        }
    }
    if (m < n-1 || m > ans.size()) { printf("Impossible\n"); return 0; }
    printf("Possible\n");
    for (auto i: ans) printf("%d %d\n", i.first, i.second);
    return 0;
}
/*
5 6
*/


猜你喜欢

转载自blog.csdn.net/zscdst/article/details/81049707
今日推荐