zoukankan      html  css  js  c++  java
  • D. Prime Graph

    Every person likes prime numbers. Alice is a person, thus she also shares the love for them. Bob wanted to give her an affectionate gift but couldn't think of anything inventive. Hence, he will be giving her a graph. How original, Bob! Alice will surely be thrilled!

    When building the graph, he needs four conditions to be satisfied:

    • It must be a simple undirected graph, i.e. without multiple (parallel) edges and self-loops.
    • The number of vertices must be exactly nn — a number he selected. This number is not necessarily prime.
    • The total number of edges must be prime.
    • The degree (i.e. the number of edges connected to the vertex) of each vertex must be prime.

    Below is an example for n=4n=4. The first graph (left one) is invalid as the degree of vertex 22 (and 44) equals to 11, which is not prime. The second graph (middle one) is invalid as the total number of edges is 44, which is not a prime number. The third graph (right one) is a valid answer for n=4n=4.

    Note that the graph can be disconnected.

    Please help Bob to find any such graph!

    Input

    The input consists of a single integer nn (3n10003≤n≤1000) — the number of vertices.

    Output

    If there is no graph satisfying the conditions, print a single line containing the integer 1−1.

    Otherwise, first print a line containing a prime number mm (2mn(n1)22≤m≤n(n−1)2) — the number of edges in the graph. Then, print mm lines, the ii-th of which containing two integers uiui, vivi (1ui,vin1≤ui,vi≤n) — meaning that there is an edge between vertices uiui and vivi. The degree of each vertex must be prime. There must be no multiple (parallel) edges or self-loops.

    If there are multiple solutions, you may print any of them.

    Note that the graph can be disconnected.

    Examples
    input
    Copy
    4
    
    output
    Copy
    5
    1 2
    1 3
    2 3
    2 4
    3 4
    input
    Copy
    8
    
    output
    Copy
    13
    1 2
    1 3
    2 3
    1 4
    2 4
    1 5
    2 5
    1 6
    2 6
    1 7
    1 8
    5 8
    7 8
    
    Note

    The first example was described in the statement.

    In the second example, the degrees of vertices are [7,5,2,2,3,2,2,3][7,5,2,2,3,2,2,3]. Each of these numbers is prime. Additionally, the number of edges, 1313, is also a prime number, hence both conditions are satisfied.

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <string>
    #include <set>
    #include <queue>
    #include <map>
    #include <sstream>
    #include <cstdio>
    #include <cstring>
    #include <numeric>
    #include <cmath>
    #include <iomanip>
    #include <deque>
    #include <bitset>
    #include <unordered_set>
    #include <unordered_map>
    #define ll              long long
    #define PII             pair<int, int>
    #define rep(i,a,b)      for(int  i=a;i<=b;i++)
    #define dec(i,a,b)      for(int  i=a;i>=b;i--)
    using namespace std;
    int dir[4][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 } };
    const long long INF = 0x7f7f7f7f7f7f7f7f;
    const int inf = 0x3f3f3f3f;
    const double pi = 3.14159265358979323846;
    const double eps = 1e-6;
    const int mod =1e9+7;
    const int N = 1e5+5;
    //if(x<0 || x>=r || y<0 || y>=c)
    
    inline ll read()
    {
        ll x = 0; bool f = true; char c = getchar();
        while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
        while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
        return f ? x : -x;
    }
    ll gcd(ll m, ll n)
    {
        return n == 0 ? m : gcd(n, m % n);
    }
    ll lcm(ll m, ll n)
    {
        return m * n / gcd(m, n);
    }
    bool prime(int x) {
        if (x < 2) return false;
        for (int i = 2; i * i <= x; ++i) {
            if (x % i == 0) return false;
        }
        return true;
    }
    ll qpow(ll m, ll k, ll mod)
    {
        ll res = 1, t = m;
        while (k)
        {
            if (k & 1)
                res = res * t % mod;
            t = t * t % mod;
            k >>= 1;
        }
        return res;
    }      
    
    int main()
    {
        int m,n;
        cin >> n;
        m = n;
        while (!prime(m))
            m++;
        cout << m << "
    1 " << n << endl;
        rep(i, 1, n - 1)
            cout << i << " " << i + 1 << endl;
        rep(i, 1, m - n)
            cout << i << " " << i + n/2<< endl;
        return 0;
    }
     
  • 相关阅读:
    java几种常用设计模式简单示例
    字符串常用方法
    File类常用方法
    如何在一个form表单中实现多个submit
    CC EAL认证
    ARQC与ARPC的生成和校验方法
    金融卡IC卡知识50问
    M(必备),R(需求),C(条件),O(可选)
    非接触IC卡中typeA卡和typeB卡的区别--总结,二者的调制方式和编码方式不同
    电子钱包和电子存折的区别
  • 原文地址:https://www.cnblogs.com/dealer/p/13128959.html
Copyright © 2011-2022 走看看