Prime Graph(思维 + 数学)

题目链接:http://codeforces.com/problemset/problem/1178/D
  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:

  1. It must be a simple undirected graph, i.e. without multiple (parallel) edges and self-loops.
  2. The number of vertices must be exactly n — a number he selected. This number is not necessarily prime.
  3. The total number of edges must be prime.
  4. 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, v i(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
Input
4
Output
5
1 2
1 3
2 3
2 4
3 4
Input
8
Output
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]. Each of these numbers is prime. Additionally, the number of edges, 13, is also a prime number, hence both conditions are satisfied.
在这里插入图片描述

题意

  给出正整数,构造一个边数为质数的无向连通图(无自环重边),且图的每个节点的度数都为质数。

分析

  利用一个性质 n ~ n+n/2 这个区间里,必定有一个数是质数。
  首先 2 和 3 是质数吧,利用性质我们可以知道最终图的每个定点的度数是 2 或者 3;
  那么先连成环,这时每个顶点的度数为 2 ,但此时的边数如果不是质数,那就要加边,让其中一些2度顶点变成3度顶点;
第一步,找出大于等于 n 的最小质数 m ,给出的 n 不是很大,可以先打表。
第二步,就先输出环。
第三步,加 m-n 条边,可以是以此加 i - n/2 + i ;

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<queue>
#include<vector>
#include<map>
#include<utility>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int maxn = 1010;
const int inf = 0x3f3f3f3f;
int prime[maxn],pNum = 0;
bool p[maxn]={false};	//判断是否为素数
void find_prime(){
	p[0] = p[1] = true;
	for(int i=2;i<maxn;i++){
		if(p[i]==false){
			prime[pNum++] = i;
			for(int j=i+i;j<maxn;j+=i)
				p[j] = true;
		}
	}
}

int main(void)
{
	int n;
	scanf("%d",&n);
	int nn = n;
	find_prime();	//素数打表
	while(p[nn])	nn++;	//第一步
	
	printf("%d\n",nn);
	for(int i=1;i<n;i++)	//第二步
		printf("%d %d\n",i,i+1);
	printf("1 %d\n",n);
	
	for(int i=1;i<=nn-n;i++)	//第三步
		printf("%d %d\n",i,i+n/2);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

逃夭丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值