Codeforces 1178D. Prime Graph
首先每个点至少要有两条边连接
那么容易想到先保证这一点然后再慢慢加边
那么先构成一个环即可:$(1,2),(2,3),(3,4)...(n,1)$
然后考虑加边,发现一个点加一条边还是合法的,那么不妨直接 $(1,4),(2,5),(3,6)$ ,然后一旦边数为质数了就直接输出答案
那么现在问题就是能否保证在 $[n,n+\left \lfloor \frac {n-3} {2} \right \rfloor]$ 范围内一定有质数
打个表发现在 $n \in [3,1000]$ 内唯一不合法的只有 $n=4$,那么特判一下就行了......
官方题解竟然也是这个操作???没有证明的吗???
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
using namespace std;
typedef long long ll;
inline int read()
{
int x=,f=; char ch=getchar();
while(ch<''||ch>'') { if(ch=='-') f=-; ch=getchar(); }
while(ch>=''&&ch<='') { x=(x<<)+(x<<)+(ch^); ch=getchar(); }
return x*f;
}
const int N=1e5+;
int n,m;
struct edge {
int u,v;
edge (int _u=,int _v=) { u=_u,v=_v; }
};
vector <edge> ans;
inline void ins(int u,int v) { ans.push_back(edge(u,v)); }
int pri[N],tot;
bool not_pri[N];
void pre()
{
not_pri[]=;
for(int i=;i<=*n;i++)
{
if(!not_pri[i]) pri[++tot]=i;
for(int j=;j<=tot;j++)
{
ll g=1ll*i*pri[j]; if(g>*n) break;
not_pri[g]=; if(i%pri[j]==) break;
}
}
}
int main()
{
n=read(); pre();
for(int i=;i<=n;i++) ins(i,i-);
ins(,n); int now=n;
if(n==) ins(,),now++;
else
for(int i=;i<=n-;i++)
{
if(!not_pri[now]) break;
if(i&) ins(i,i+),now++;
}
if(not_pri[now]) { printf("-1\n"); return ; }
printf("%d\n",now);
for(auto E: ans) printf("%d %d\n",E.u,E.v);
return ;
}
Codeforces 1178D. Prime Graph的更多相关文章
- [Codeforces 1178D]Prime Graph (思维+数学)
Codeforces 1178D (思维+数学) 题面 给出正整数n(不一定是质数),构造一个边数为质数的无向连通图(无自环重边),且图的每个节点的度数为质数 分析 我们先构造一个环,每个点的度数都是 ...
- Codeforces 1009D:Relatively Prime Graph
D. Relatively Prime Graph time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- 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( ...
- Relatively Prime Graph CF1009D 暴力 思维
Relatively Prime Graph time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- 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 47 (Rated for Div. 2) :D. Relatively Prime Graph
题目链接:http://codeforces.com/contest/1009/problem/D 解题心得: 题意就是给你n个点编号1-n,要你建立m条无向边在两个互质的点之间,最后所有点形成一个连 ...
- CodeForces - 1009D Relatively Prime Graph
题面在这里! 直接暴力找点对就行了,可以证明gcd=1是比较密集的,所以复杂度略大于 O(N log N) #include<bits/stdc++.h> #define ll long ...
- 【Codeforces 1009D】Relatively Prime Graph
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 1000以内就有非常多组互质的数了(超过1e5) 所以,直接暴力就行...很快就找完了 (另外一开始头n-1条边找1和2,3...n就好 [代 ...
随机推荐
- Linux配置tomcat开机自启
转自百度经验:https://jingyan.baidu.com/article/6525d4b1382f0aac7d2e9421.html 端口也需要保持开机自启详细请看:https://www.c ...
- hadoop+zookeeper+hbase伪分布式安装
基本安装步骤 安装包下载 从大数据组件下载地址下载以下组件安装包 hadoop-2.6.0-cdh5.6.0.tar.gz hbase-1.0.0-cdh5.6.0.tar.gz zookeeper- ...
- 关于web前端开发,区别用户使用的终端设备代码
对web前端开发,熟知不同的浏览器,的解析是基本技能,下面,我就向大家展示一下,如何获得当前用户所用的终端设备.车子一发动,请准备上车................... <script> ...
- 使用SpringBoot校验客户端传来的数据
前端的数据校验都是辣鸡!后端天下第一! 很多时候我们后端需要前端传数据过来, 比如注册, 修改用户名, 修改密码等等.很可能有些用户就喜欢搞事, 喜欢发一大堆乱七八糟的数据到后端来, 甚至有些前端老哥 ...
- Php+Redis函数使用总结
因项目需求,冷落了redis,今天再重新熟悉一下: <?php //连接 $redis = New Redis(); $redis->connect('127.0.0.1','6379', ...
- VLOOKUP的了解(1)
上周在整理资料的时候,需要将一个表中的部分东西,弄到另外一张表中.我就傻不愣登的直接粘贴.复制.没有发现VLOOKUP呀! VLOOKUP是一个查找函数,给定一个查找的目标,它就能从指定的查找区域中查 ...
- 浅析history hack、心血漏洞、CSS欺骗、SQL注入与CSRF攻击
漏洞产生的原因主要有系统机制和编码规范两方面,由于网络协议的开放性,目前以 Web 漏洞居多 关于系统机制漏洞的典型有JavaScript/CSS history hack,而编码规范方面的漏洞典型有 ...
- sklearn3_svc分类器预测
python机器学习-乳腺癌细胞挖掘(博主亲自录制视频) https://study.163.com/course/introduction.htm?courseId=1005269003&u ...
- layui 表单遇到的小问题
select中的option 居中显示 /*select显示的option居中*/ /*.layui-select-title input{ text-align: center; }*/ /*opt ...
- Vue avoid mutating a prop directly since the value will be overwritten
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...