题目链接:https://codeforces.com/contest/1178/problem/D

题意:给出  个点,要求构成一个简单图,使得边的总数是素数,并且每个点的度数也是素数。

思路:如果  是素数直接把这些点连成一个环即可,如果  不是质数,那么从   开始,在圆环中加入  的边。这样一定会连条边,而且每一个点的度数不是2就是3。根据伯特兰-切比雪夫定理:之间一定有一个素数,所以保证有解。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ul;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()
#define bug prllf("*********\n")
#define debug(x) cerr<<#x<<" = "<<(x)<<endl
#define debugp(x) cerr<<#x<<"= {"<<(x.first)<<", "<<(x.second)<<"}"<<endl
#define debug2(x, y) cerr<<"{"<<#x<<", "<<#y<<"} = {"<<(x)<<", "<<(y)<<"}"<<endl
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fLL;
const int mod = 998244353;
const double eps = 1e-8;
const double pi = acos(-1);
const int N = 1e5 + 7;
bool is_prime[N + 5];
void Euler_prime()
{ll prime[N + 5];int i, j, c = 0;memset(is_prime, true, sizeof(is_prime));for(i = 2; i <= N; i++){if(is_prime[i])prime[c++] = i;for(j = 0; j < c && prime[j] * i <= N; j++){is_prime[prime[j] * i] = false;if(i % prime[j] == 0) break;}}
}
int n;
int main()
{IO; Euler_prime(); cin >> n;if(is_prime[n]){cout << n << endl;for(int i = 2; i <= n; i++)cout << i - 1 << " " << i << '\n';cout << n << " " << 1 << '\n';}else{int k = n;while(!is_prime[k]) k++;cout << k << '\n';for(int i = 2; i <= n; i++)cout << i - 1 << " " << i << '\n';cout << n << " " << 1 << '\n';for(int i = 1; i <= k - n; i++)cout << i << " " << i + n / 2 << '\n';}
}

Codeforces Global Round 4 - 1178D. Prime Graph(构造+切比雪夫定理)相关推荐

  1. Codeforces Global Round 4-D. Prime Graph(伯特兰-切比雪夫定理)

    题目:Codeforces Global Round 4-D. Prime Graph 题意:给出n(顶点的个数),要求所得图满足: 1.无平行边和自环 2.边的总数是个质数 3.每个点的度(也就是点 ...

  2. Codeforces Global Round 3

    Codeforces Global Round 3 A. Another One Bites The Dust 有若干个a,有若干个b,有若干个ab.你现在要把这些串拼成一个串,使得任意两个相邻的位置 ...

  3. Codeforces Global Round 14 F. Phoenix and Earthquake 思维 + 并查集

    传送门 文章目录 题意: 思路: 题意: 给你nnn个点,mmm条边,限制xxx,每个点都有沥青aia_iai​,定义合并两个点即两点之间有边且au+av≥xa_u+a_v\ge xau​+av​≥x ...

  4. Codeforces Global Round 1 晕阙记

    Codeforces Global Round 1 晕阙记 我做这场比赛的时候晕得要死.做这三道题做太久了,rating涨不起来啊! A 如果愿意的话你可以看做是膜2意义下的运算,写快速幂等各种膜运算 ...

  5. Codeforces Global Round 1

    Codeforces Global Round 1 题解:The Editorial of the First Codeforces Global Round A:其实mod 2计算一下就行了 B:删 ...

  6. 【Codeforces Global Round 23】B. Rebellion

    Codeforces Global Round 23中B. Rebellion Codeforces比赛记录 文章目录 题目链接: 一.B. Rebellion 题目意思: 上思路: 总结 B. Re ...

  7. codeforces global round 23

    constest :codeforces global round 23 contest time:2022.10.16 contest grade: 2800 contest rating chan ...

  8. Codeforces Global Round 8 C. Even Picture 题解[巧妙法构造题]

    C. Even Picture 题目大意:让你构造一个连通图,图中有k个灰点其中n个点4面都是灰点,(k-n)个点周围2个点是灰点 我们设最上面的为(1,1)点就可以了 ps:exactly:只有,其 ...

  9. Codeforces Global Round 12 C1 C2. Errich-Tac-Toe 思维构造 好题

    传送门 题意: 给了如下规则,上面三个只要出现一个情况就是非平局,现在给你个字符矩阵,让后其中XXX字符有KKK个(hardhardhard版本XXX和OOO一共KKK个),每次操作可以将XXX变成O ...

最新文章

  1. etcd代理组件的开发思想
  2. Membership学习(三)Membership Providers介绍[xgluxv]
  3. c# 自定义应用程序配置文件(app.config)
  4. 从0到1开发自动化测试框架(硬货太多,建议阅读)
  5. 4.深入分布式缓存:从原理到实践 --- Ehcache 与 Guava Cache
  6. 基于DS18B20温度控制系统
  7. 18650锂电池保护板接线图_锂电池保护板几种接线方法介绍
  8. 启动分区不存在,使用分区工具修正
  9. Python实现文字合成音频文件
  10. qpython3.7.4版本下载_QPython3app下载
  11. Laravel 部署到阿里云 / 腾讯云
  12. 电话交换机原理(企业运用方案)
  13. 利用JavaScript批量删除QQ空间说说(只需一个浏览器)
  14. 12306购票辅助工具
  15. gradle报错this sometimes occurs after a network connection timeout.解决
  16. 非常道:ChatGPT 与思想革命
  17. js写可以暂停的电子时钟
  18. Java TreeSet详解
  19. 机器码农:深度学习自动编程
  20. oa服务器优化,如何优化OA以提高访问速度

热门文章

  1. GitBook插件整理
  2. 【JavaScript】解构赋值
  3. 魅族android os 耗电,魅族魅蓝metal耗电快怎么办 魅蓝metal省电教程【详解】
  4. 搭建kvm虚拟化平台和管理kvm虚拟化平台
  5. 安装编译器 openmpi
  6. Vue 安装 less 报错 While resolving: hblvbigdata@0.1.0
  7. 《领航优配》沪指放量涨0.49%,半导体板块强势拉升
  8. iostat linux,centos安装iostat命令的方法详解
  9. 字符集(字符编码)原理
  10. 读书百客:《岭南江行》赏析