传送门

首先每个点至少要有两条边连接

那么容易想到先保证这一点然后再慢慢加边

那么先构成一个环即可:$(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的更多相关文章

  1. [Codeforces 1178D]Prime Graph (思维+数学)

    Codeforces 1178D (思维+数学) 题面 给出正整数n(不一定是质数),构造一个边数为质数的无向连通图(无自环重边),且图的每个节点的度数为质数 分析 我们先构造一个环,每个点的度数都是 ...

  2. Codeforces 1009D:Relatively Prime Graph

    D. Relatively Prime Graph time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  3. 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( ...

  4. Relatively Prime Graph CF1009D 暴力 思维

    Relatively Prime Graph time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  5. Codeforces H. Prime Gift(折半枚举二分)

    题目描述: Prime Gift time limit per test 3.5 seconds memory limit per test 256 megabytes input standard ...

  6. 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 ...

  7. Educational Codeforces Round 47 (Rated for Div. 2) :D. Relatively Prime Graph

    题目链接:http://codeforces.com/contest/1009/problem/D 解题心得: 题意就是给你n个点编号1-n,要你建立m条无向边在两个互质的点之间,最后所有点形成一个连 ...

  8. CodeForces - 1009D Relatively Prime Graph

    题面在这里! 直接暴力找点对就行了,可以证明gcd=1是比较密集的,所以复杂度略大于 O(N log N) #include<bits/stdc++.h> #define ll long ...

  9. 【Codeforces 1009D】Relatively Prime Graph

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 1000以内就有非常多组互质的数了(超过1e5) 所以,直接暴力就行...很快就找完了 (另外一开始头n-1条边找1和2,3...n就好 [代 ...

随机推荐

  1. 2018-2019-2 20165215《网络对抗技术》Exp7 网络欺诈防范

    目录 实验目的 实验内容 实验步骤 (一)简单应用SET工具建立冒名网站 (二)ettercap DNS spoof (三)结合应用两种技术,用DNS spoof引导特定访问到冒名网站 基础问题回答 ...

  2. 基于 XML 的 AOP 配置(1)

    本文连接:https://www.cnblogs.com/qzhc/p/11969734.html 接下来我将用一个很简单的实例 1. 环境搭建 1.1. 第一步:准备必要的代码 业务层代码: Acc ...

  3. Ngrinder 源码之Maven 项目

    Ngrinder支持Maven结构的测试脚本.使用ScriptHandlerFactory来个脚本选择处理器handler,目前有JythonScriptHandler, GroovyScriptHa ...

  4. VUE中让由全局变量添加生成的新数组不随全局变量的变化而变化

    问题场景: const addOptions = { singleOrComplex, totalNum: this.smallTotalPrice, selectList: this.purchas ...

  5. vue 按需加载,缓存,导航守卫

    开发中的注意事项:代码性能的优化 1. 减少对第三方的依赖,降低耦合度 2. 加强组件的重复利用率 3. 按需加载 4. 缓存 (尽量发送请求后保存数据) 5. 开发过程中,尽量有着面向对象的思想,这 ...

  6. Linux搭建PHP环境(LAMP)

    //安装Apache的命令 # yum install httpd //启动Apache的命令 # service httpd start //安装MySQL的命令 # wget http://dev ...

  7. C#类型转换类(通用类)

    //     /// 类型转换类     /// 处理数据库获取字段为空的情况     ///     public static class DBConvert     {         #reg ...

  8. javascript之Prototype属性

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. add_header 'Cache-Control' 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0'

    发送一个报头,告诉浏览器当前页面不进行缓存,每次访问的时间必须从服务器上读取最新的数据 一般情况下,浏览器为了加快浏览速度会对网页进行缓存,在一定时间内再次访问同一页面的时候会有缓存里面读取而不是从服 ...

  10. java线程锁之synchronized

    声明:该博客参考https://www.cnblogs.com/kaituorensheng/p/10079916.html,感谢哥们. 1.Sync.java package com.cn.comm ...