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就好 [代 ...
随机推荐
- 记一次zipkin和spring cloud bus冲突
问题表现spring boot 版本 2.0.6spring cloud 版本 Finchley.SR2 使用spring-cloud-starter-zipkin + 独立部署的zipkin 进行链 ...
- 05.用两个栈实现队列 Java
题目描述 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 思路 进栈: 直接进stack1 出栈: 若stack2不为空,则出栈. 否则,当stack1不为空时, ...
- C# how to properly make a http web GET request
C# how to properly make a http web GET request EDIT 23/11/17 Updated to throw out examples using asy ...
- DP&图论 DAY 1 下午
DP&图论 DAY 1 下午 区间和序列上的DP 序列上的DP >序列上的dp状态设计最基本的形式 F[i]表示以 i 结尾的最优值或方案数.◦ F[i][k]表示以 i 结尾附加 ...
- 转 Golang 入门 : 切片(slice)
https://www.jianshu.com/p/354fce23b4f0 切片(slice)是 Golang 中一种比较特殊的数据结构,这种数据结构更便于使用和管理数据集合.切片是围绕动态数组的概 ...
- Dao操作的抽取
package com.loaderman.demo.c_jdbc; public class Admin { private int id; private String userName; pri ...
- 手机APP缓存的获取和清理功能的实现
package com.loaderman.appcachedemo; import android.content.pm.IPackageDataObserver; import android.c ...
- java之数据填充PDF模板
声明:由于业务场景需要,所以根据一个网友的完成的. 1.既然要使用PDF模板填充,那么就需要制作PDF模板,可以使用Adobe Acrobat DC,下载地址:https://carrot.ctfil ...
- win + T 快捷键
和 alt + tab 不同,win + T会在 在任务栏间的程序间进行switch
- 七十九:flask.Restful之flask-Restful标准化返回参数示例
接上一篇的代码和数据 对于复杂结构的数据如果只是定义单一结构的话返回的数据就没意义了,此时定义的数据结构需精确到所有数据的每一个字段有时候要返回的数据结构中,会有比较复杂的数据结构,证实后可以使用一些 ...