[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 ...
随机推荐
- Windows10测试低版本IE方法
前端开发工程师可能了解IETester是一款IE多版本兼容性测试软件,但是只支持Windows Xp,Vista,7,8系统,Windows10是不支持的,网上所说的开启.net framework ...
- cp 复制文件或目录
1. 命令功能 cp --copy files and directories.复制文件或目录. 2. 语法格式 cp [option] source des cp [option] sour ...
- GetExtendedTcpTable
https://blog.csdn.net/sky101010ws/article/details/55511501 AdjustTokenPrivileges function Library Ad ...
- pandas数据读取(DataFrame & Series)
1.pandas数据的读取 pandas需要先读取表格类型的数据,然后进行分析 数据说明 说明 pandas读取方法 csv.tsv.txt 用逗号分割.tab分割的纯文本文件 pd.read_csv ...
- bzoj4811 [Ynoi2017]由乃的OJ 树链剖分+贪心+二进制
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4811 题解 我现在为什么都写一题,调一天啊,马上真的退役不花一分钱了. 考虑这道题的弱化版 N ...
- spring mvc和spring boot的区别
spring boot只是一个配置工具,整合工具,辅助工具. springmvc是框架,项目中实际运行的代码 Spring 框架就像一个家族,有众多衍生产品例如 boot.security.jpa等等 ...
- 实现bind函数
面试中碰到的bind函数,今天来研究下 //1.bind的返回值是函数 var obj={ name:"zhouy" } function f() { console.log(th ...
- Linux系统下安装jenkins使用
jenkins 2.190.1 yum 安装 devops一梦千年 发布时间:10-0916:28 jenkins 2.190.1 yum 安装记录 安装环境: 所需安装包: https://pkg. ...
- Codeforces 846F - Random Query
原题链接:http://codeforces.com/contest/846/problem/F 题意:给一个数列,任意取区间[l, r],问区间内不同数字的个数的期望是多少. 思路: 对于第i个数a ...
- [USACO17FEB]Why Did the Cow Cross the Road III G (树状数组,排序)
题目链接 Solution 二维偏序问题. 现将所有点按照左端点排序,如此以来从左至右便满足了 \(a_i<a_j\) . 接下来对于任意一个点 \(j\) ,其之前的所有节点都满足 \(a_i ...