[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 ...
随机推荐
- ORA-00911: invalid character 错误解决
多数情况如下: 控制面板--系统和安全---系统--高级系统设置--高级--环境变量--系统变量中 变量名:NLS_LANG 变量值:SIMPLIFIED CHINESE_CHINA.ZHS16GBK ...
- nodejs 更新代码自动刷新页面
安装第三方工具: nodemon npm install --global nodemon 安装完毕后使用: 之前使用: node xxx.js 改成 nodemon xxx.js 只要通过nodem ...
- Codeforces Round #421 (Div. 2) - A
题目链接:http://codeforces.com/contest/820/problem/A 题意:一个人在看一本书,书一共C页,这个人每天看v0页,但是他又开始加速看这本书,每天都比前一天多看a ...
- OA是Office Automation
OA是Office Automation OA是Office Automation OA是Office Automation
- 什么是Js原型?(1)(包括作用:继承)
学习目标: 认识什么js是原型,原型.构成函数.实例对象关系:原型应用范围. 什么是原型 函数有原型,函数有一个属性叫prototype,函数的这个原型指向一个对象,这个对象叫原型对象.这 ...
- 【leetcode】1043. Partition Array for Maximum Sum
题目如下: Given an integer array A, you partition the array into (contiguous) subarrays of length at mos ...
- linux运维、架构之路-Zabbix监控
一.监控常用命令 1.物理服务器监控命令 ①添加yum源 wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/rep ...
- POJ 3660 Cow Contest ( 最短路松弛思想应用 && Floyd求传递闭包 )
题意 : 给出 N 头奶牛在比赛的结果,问你最多的能根据给出结果确定其名次的奶牛头数.结果给出的形式为 A B 代表在比赛当中 A 战胜了 B 分析 : 对于一头奶牛来说,如果我们能确定其他 N - ...
- MapServer教程2
第二章 Tutorial 教程 MapServer Tutorial MapServer教程 Tutorial background 教程背景 Section 1: Static Maps and t ...
- [CSP-S模拟测试]:Rectangle(模拟+树状数组)
题目描述 平面上有$n$个点,第$i$个点的坐标为$X_i,Y_i$.对于其中的一个非空点集$S$,定义$f(S)$为一个最小矩形,满足:$\bullet$覆盖$S$中所有的点(在边界上也算覆盖):$ ...