CF1178D Prime Graph
题意
构造一张有\(n(3\le n\le 1000)\)个点的无向图(无重边和自环)。满足:
- 边的总数为素数
- 所有点的度数均为素数
输出方案
solution
如果所有点的度数确定了。那么边数就是度数之和的一半。连边就很简单了。
所以考虑怎么确定点的度数。
猜想:必有至少一个\(A \in [2n,3n] (3 \le n \le 1000)\)满足\(\frac{A}{2}\)为素数。
经测试,成立。
所以可以让所有点的度数都为\(2\)或\(3\)只要找到这个\(A\)作为度数之和。然后边数就是\(\frac{A}{2}\)。
设度数为\(2\)的点有\(x\)个,度数为\(3\)的点有\(y\)个。
列方程:
\begin{aligned}
2x + 3y = A \\
x + y = n
\end{aligned}
\right.
\]
将所有点连成一个环之后,再连\(y\)条边即可。
code
/*
* @Author: wxyww
* @Date: 2019-07-21 00:20:04
* @Last Modified time: 2019-07-21 07:53:25
*/
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<ctime>
using namespace std;
typedef long long ll;
const int N = 1000000 + 100;
#define pi pair<int,int>
int cnt;
ll read() {
ll x=0,f=1;char c=getchar();
while(c<'0'||c>'9') {
if(c=='-') f=-1;
c=getchar();
}
while(c>='0'&&c<='9') {
x=x*10+c-'0';
c=getchar();
}
return x*f;
}
int tot,dis[N],vis[N],a[N],K;
void Eur() {
for(int i = 2;i <= K;++i) {
if(!vis[i]) dis[++tot] = i;
for(int j = 1;j <= tot && 1ll * dis[j] * i <= K;++j) {
vis[dis[j] * i] = 1;
if(i % dis[j] == 0) break;
}
}
}
int A;
pi ans[N];
priority_queue<pi>q;
int p(int x) {
if(x & 1) return x + 1;
return x;
}
int main() {
int n = read();
K = 100000;
Eur();
for(A = n * 2;A <= n * 3;A ++) {
if(A & 1) continue;
if(!vis[A / 2]) break;
}
int Y = A - n * 2;
int X = n - Y;
for(int i = 1;i <= X;++i) q.push(make_pair(2,i));
for(int i = X + 1;i <= n;++i) q.push(make_pair(3,i));
for(int i = 1;i < n;++i) ans[++cnt] = make_pair(i,i + 1);
ans[++cnt] = make_pair(n,1);
Y /= 2;
for(int i = 1;i <= n;i ++) {
if(Y && !a[i] && !a[i + 2]) ans[++cnt] = make_pair(i,i + 2),Y--,a[i] = a[i + 2] = 1;
}
printf("%d\n",cnt);
for(int i = 1;i <= cnt;++i) {
printf("%d %d\n",ans[i].first,ans[i].second);
}
return 0;
}
CF1178D Prime Graph的更多相关文章
- 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 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 ...
- [Codeforces 1178D]Prime Graph (思维+数学)
Codeforces 1178D (思维+数学) 题面 给出正整数n(不一定是质数),构造一个边数为质数的无向连通图(无自环重边),且图的每个节点的度数为质数 分析 我们先构造一个环,每个点的度数都是 ...
- CodeForces - 1009D Relatively Prime Graph
题面在这里! 直接暴力找点对就行了,可以证明gcd=1是比较密集的,所以复杂度略大于 O(N log N) #include<bits/stdc++.h> #define ll long ...
- 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
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 1000以内就有非常多组互质的数了(超过1e5) 所以,直接暴力就行...很快就找完了 (另外一开始头n-1条边找1和2,3...n就好 [代 ...
- Codeforces 1178D. Prime Graph
传送门 首先每个点至少要有两条边连接 那么容易想到先保证这一点然后再慢慢加边 那么先构成一个环即可:$(1,2),(2,3),(3,4)...(n,1)$ 然后考虑加边,发现一个点加一条边还是合法的, ...
随机推荐
- Goldbach`s Conjecture(LightOJ - 1259)【简单数论】【筛法】
Goldbach`s Conjecture(LightOJ - 1259)[简单数论][筛法] 标签: 入门讲座题解 数论 题目描述 Goldbach's conjecture is one of t ...
- 干货 | 国内互联网公司是如何做微服务实践的?(附PPT下载)
微服务的概念最早由Martin Fowler与James Lewis于2014年共同提出,并随着Netflix最佳实践的发布而为业界所知.如今,在国内有了大量的微服务实践案例,5月18日,网易云联合云 ...
- python3编码问题总结
关于python3的编码类型,到底是怎么编码的,一直使我比较疑惑,在看了网上很多帖子之后,经过自己尝试与实验,将自己的总结写在下面,一是当做一次笔记,二是希望网友们能指正.仅供参考,欢迎指正,谢谢!! ...
- Django JsonResponse 不自动设置 cookie 的解决方案
[背景] 目前在做一个前后端分离的 web 项目,后端使用的是 django 框架,所有的 API 都只返回 json :就在这个过程中遇到了一个问题,那就是对于所有的 JsonResponse dj ...
- 控制FlowDocumentScrollViewer滚动到最下方
原文发布于:https://www.chenxublog.com/2019/07/14/contrlo-flowdocumentscrollviewer-to-bottom.html 由于我在llco ...
- 你以为你真的了解final吗?
本人免费整理了Java高级资料,涵盖了Java.Redis.MongoDB.MySQL.Zookeeper.Spring Cloud.Dubbo高并发分布式等教程,一共30G,需要自己领取.传送门:h ...
- crm-4权限
1.rbac-优化login函数 因为login是业务逻辑 ,而rbac是个组件 ,将rbac在login的代码分离 ###初始化权限函数分离出去 rbac/service/permission fr ...
- leaflet-webpack 入门开发系列一初探篇(附源码下载)
前言 leaflet-webpack 入门开发系列环境知识点了解: node 安装包下载webpack 打包管理工具需要依赖 node 环境,所以 node 安装包必须安装,上面链接是官网下载地址 w ...
- 设置view的layer属性方法
1.需要导入QuartzCore.framewoork框架到工程2.在文件中导入#import 3.设置 必须导入的空间 #import<QuartzCore/QuartzCore.h> ...
- 仅用StoryBoard布局实现按钮的均匀分布
今天在做登录界面时设计的需求是,登录和取消按钮左右对称均匀分布,按钮大小不变如图 屏幕宽度变化时按钮宽度不变,三个间距相等并且随屏幕变化而变化,简单的说就是按钮均匀分布,在网上查了一些资料,弄得比较乱 ...