[Codeforces 1178D]Prime Graph (思维+数学)
Codeforces 1178D (思维+数学)
题面
给出正整数n(不一定是质数),构造一个边数为质数的无向连通图(无自环重边),且图的每个节点的度数为质数
分析
我们先构造一个环,每个点的度数都是2。但由于n不一定是质数,我们还需要再加k条边。然后对于\(i \in [1,k]\),我们加边(i,i+n/2)。当\(k\leq \frac{n}{2}\)的时候,只会把一些点的度数由2变成3,否则会出现重边问题。假设新图的边数为m,那\(m \in [n,n+\frac{n}{2}]\),如果在这个区间内能找到一个质数m,那问题就一定有解。
这里有一个定理
对于\(\forall n \geq 3\),区间\([n,\frac{3n}{2}]\)中一定存在一个质数
所以这个问题一定有解。我们筛出\([1,\frac{3n}{2}]\)内的质数,然后二分查找出第一个大于等于n的质数m,按照上述方法构造就可以了
代码
#include<iostream>
#include<cstdio>
#include<algorithm>
#define maxn 100000
using namespace std;
int n,m;
int cnt=0;
bool vis[maxn+5];
int prime[maxn+5];
void sieve(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(){
scanf("%d",&n);
sieve(n*3/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",n,1);
for(int i=1;i<=m-n;i++){
printf("%d %d\n",i,i+n/2);
}
}
[Codeforces 1178D]Prime Graph (思维+数学)的更多相关文章
- Codeforces 1178D. Prime Graph
传送门 首先每个点至少要有两条边连接 那么容易想到先保证这一点然后再慢慢加边 那么先构成一个环即可:$(1,2),(2,3),(3,4)...(n,1)$ 然后考虑加边,发现一个点加一条边还是合法的, ...
- Codeforces 1009D:Relatively Prime Graph
D. Relatively Prime Graph time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- Relatively Prime Graph CF1009D 暴力 思维
Relatively Prime Graph time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- D. Relatively Prime Graph
Let's call an undirected graph G=(V,E)G=(V,E) relatively prime if and only if for each edge (v,u)∈E( ...
- Codeforces H. Prime Gift(折半枚举二分)
题目描述: Prime Gift time limit per test 3.5 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Global Round 4 Prime Graph CodeForces - 1178D (构造,结论)
Every person likes prime numbers. Alice is a person, thus she also shares the love for them. Bob wan ...
- Educational Codeforces Round 50 (Rated for Div. 2) F - Relatively Prime Powers(数学+容斥)
题目链接:http://codeforces.com/contest/1036/problem/F 题意: 题解:求在[2,n]中,x != a ^ b(b >= 2 即为gcd)的个数,那么实 ...
- CF思维联系–CodeForces - 222 C Reducing Fractions(数学+有技巧的枚举)
ACM思维题训练集合 To confuse the opponents, the Galactic Empire represents fractions in an unusual format. ...
- Codeforces 789A Anastasia and pebbles(数学,思维题)
A. Anastasia and pebbles time limit per test:1 second memory limit per test:256 megabytes input:stan ...
随机推荐
- 封装和private,this,super关键字的简单应用
1.将成员变量用private修饰 2.提供对应的getxx()和setxx()方法 public class Student { private String name; private int a ...
- ORI-621龙芯3A处理器CPCI刀片计算机
ORI-621龙芯3A处理器CPCI刀片计算机 一.产品简介 ORI -621是一款基于龙芯3A国产CPU处理器的特种CPCI刀片计算机.该产品成功地实现了服务器NUMA架构在国产特种计算机中的应用, ...
- wenzhang
作者:周公子链接:https://zhuanlan.zhihu.com/p/94960418来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 2006年2月9日,首都经济 ...
- django:一个RESTfull的接口从wsgi到函数的历程
1.wsgi将web server参数python化,封装为request对象传递给apllication命名的func对象并接受其传出的response参数,这个application在wsgi.p ...
- Linux shell中自动完成登录
在写shell脚本时,需要登录到不同的服务器上执行相关命令,在未建立信任之前如何批量操作. 1.ssh 首次登录服务器时会提示RSA key fingerprint输入yes/no,可以通过下面的方法 ...
- 企业级监控软件Zabbix搭建部署之zabbix在WEB页面中的配置
企业级监控软件zabbix搭建部署之zabbix在WEB页面中的配置 企业级监控软件zabbix搭建部署之zabbix在WEB页面中的配置 关于安装请看 http://www.linuxidc.com ...
- FreeRTOS之taskYIELD()
摘自:http://www.mcuchina.com/article/2007/1227/article_59.html 1.taskYIELD() 比如我创建了8个优先级一样的task,并且没有 ...
- 【leetcode】1028. Recover a Tree From Preorder Traversal
题目如下: We run a preorder depth first search on the root of a binary tree. At each node in this traver ...
- linux运维、架构之路-Nginx提高
一.虚拟主机搭建 1.基于域名的虚拟主机 [root@web01 html]# cat nginx.conf worker_processes ; events { worker_connection ...
- "C++ Primer Plus" is WAY FUCKING BETTER than "C++ Primer" (For Beginners)!!!
再看到给初学C++的人推荐C++ Primer的我就要揍人了,真的! 被他妈这帮装逼犯给误导了,耽误了无数的功夫! 就是听这帮傻逼的谣言,说C++ Primer讲解更深入什么的,初学也应该啃这本书,老 ...