CodeForces1178D D. Prime Graph

Prime Graph

D. Prime Graph
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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 n — 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=4. The first graph (left one) is invalid as the degree of vertex 2 (and 4) equals to 1, which is not prime. The second graph (middle one) is invalid as the total number of edges is 4, which is not a prime number. The third graph (right one) is a valid answer for n=4.

Note that the graph can be disconnected.

Please help Bob to find any such graph!

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

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

Otherwise, first print a line containing a prime number m (2≤m≤n(n−1)2) — the number of edges in the graph. Then, print m lines, the i-th of which containing two integers ui, vi (1≤ui,vi≤n) — meaning that there is an edge between vertices ui and vi. 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
inputCopy
4
outputCopy
5
1 2
1 3
2 3
2 4
3 4
inputCopy
8
outputCopy
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

定理+构造

定理:对于∀n≥3,区间[n, 3 n 2 \frac{3n}{2} 23n]中一定存在一个质数
所以我们首先将[1, 3 n 2 \frac{3n}{2} 23n]中的所有质数都筛出来。 我们先构造一个环,环上面的所有顶点的度都是2,接着我们二分查找到第一个大于n的m,这个m的范围是[n, 3 n 2 ] \frac{3n}{2}] 23n]。所以m-n一定小于 n 2 \frac{n}{2} 2n。然后我们后面的m-n条边的构造方法就和代码一样了。

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+7;
int prime[maxn];
int vis[maxn],cnt;
void prim(int n)
{
    vis[1]=1;
    for(int i=2;i<=n;i++){
        if(!vis[i]) prime[++cnt]=i;
        for(int j=1;j<=cnt&&i*prime[j]<=n;j++){
            vis[i*prime[j]]=1;
            if(i%prime[j]==0) break;
        }
    }
}
int main()
{
    int n; cin>>n;
    prim(3*n/2+1);
    int m=prime[lower_bound(prime+1,prime+1+cnt,n)-prime];
    printf("%d\n",m);
    for(int i=1;i<n;i++){
        printf("%d %d\n",i,i+1);
    }
    printf("%d %d\n",1,n);
    for(int i=1;i<=m-n;i++){
        printf("%d %d\n",i,i+n/2);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值