传送门

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

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

那么先构成一个环即可:$(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$,那么特判一下就行了......

官方题解竟然也是这个操作???没有证明的吗???

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<algorithm>
  4. #include<cstring>
  5. #include<cmath>
  6. #include<vector>
  7. using namespace std;
  8. typedef long long ll;
  9. inline int read()
  10. {
  11. int x=,f=; char ch=getchar();
  12. while(ch<''||ch>'') { if(ch=='-') f=-; ch=getchar(); }
  13. while(ch>=''&&ch<='') { x=(x<<)+(x<<)+(ch^); ch=getchar(); }
  14. return x*f;
  15. }
  16. const int N=1e5+;
  17. int n,m;
  18. struct edge {
  19. int u,v;
  20. edge (int _u=,int _v=) { u=_u,v=_v; }
  21. };
  22. vector <edge> ans;
  23. inline void ins(int u,int v) { ans.push_back(edge(u,v)); }
  24. int pri[N],tot;
  25. bool not_pri[N];
  26. void pre()
  27. {
  28. not_pri[]=;
  29. for(int i=;i<=*n;i++)
  30. {
  31. if(!not_pri[i]) pri[++tot]=i;
  32. for(int j=;j<=tot;j++)
  33. {
  34. ll g=1ll*i*pri[j]; if(g>*n) break;
  35. not_pri[g]=; if(i%pri[j]==) break;
  36. }
  37. }
  38. }
  39. int main()
  40. {
  41. n=read(); pre();
  42. for(int i=;i<=n;i++) ins(i,i-);
  43. ins(,n); int now=n;
  44. if(n==) ins(,),now++;
  45. else
  46. for(int i=;i<=n-;i++)
  47. {
  48. if(!not_pri[now]) break;
  49. if(i&) ins(i,i+),now++;
  50. }
  51. if(not_pri[now]) { printf("-1\n"); return ; }
  52. printf("%d\n",now);
  53. for(auto E: ans) printf("%d %d\n",E.u,E.v);
  54. return ;
  55. }

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. flask 第五篇

    需求: 1. 用户名: oldboy 密码: oldboy123 2. 用户登录成功之后跳转到列表页面 3. 失败有消息提示,重新登录 4.点击学生名称之后,可以看到学生的详细信息 后端: from ...

  2. python3笔记二十四:Mysql数据库操作命令

    一:学习内容 Mysql操作命令:启动服务.停止服务.连接数据库.退出数据库.查看版本.显示当前时间.远程连接 数据库操作命令:创建数据库.删除数据库.切换数据库.查看当前选择的数据库 表操作命令:查 ...

  3. Python 之目录处理

    目录处理 OS目录处理目录-->路径,文件夹 文件:txt 1. 新建和删除一个目录 import os #引入os目录from xx import xx os.mkdir("D:\\ ...

  4. Python tuple 元组

    Python 元组 Python的元组与列表类似,不同之处在于元组的元素不能修改. 元组使用小括号,列表使用方括号. 元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可. 如下实例: tup1 ...

  5. linux常用20条命令

    1.cd命令 这是一个非常基本,也是大家经常需要使用的命令,它用于切换当前目录,它的参数是要切换到的目录的路径,可以是绝对路径,也可以是相对路径.如: cd /root/Docements # 切换到 ...

  6. 使用 Itext 生成PDF字节数组(文件流不落地)

    package com.ulic.gis.customerCenter.controller; import java.io.ByteArrayOutputStream; import java.io ...

  7. Linux 通道

    简单地说,一个通道接受一个工具软件的输出,然后把那个输出输入到其它工具软件.使用UNIX/Linux的词汇,这个通道接受了一个过程的标准输出,并把这个标准的输出作为另一个过程的标准输入.如果你没有重新 ...

  8. Prism框架 如何在主程序中合理的弹出子窗体

    说起子窗体,大家都会想到ChildWindow,多熟悉的一个控件.不错,Sliverlight中已经提供了子窗体的具体实现,而在WPF中却没有这么好的事情(有的第三方控件商已经提供此控件).最常见的实 ...

  9. Qt 之水平/垂直布局(QBoxLayout、QHBoxLayout、QVBoxLayout)

    简述QBoxLayout可以在水平方向或垂直方向上排列控件,由QHBoxLayout.QVBoxLayout所继承. QHBoxLayout:水平布局,在水平方向上排列控件,即:左右排列. QVBox ...

  10. 谷歌云服务器XShell登录

    一,谷歌云服务器,默认用浏览器进行SSH链接,而且也不告知密码.以Centos为例,先使用浏览器连接 1,给root修改密码 sudo passwd root 2,编辑ssh配置文件 sudo nan ...