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)$ 然后考虑加边,发现一个点加一条边还是合法的, ...
随机推荐
- <Graph> Topological + Undirected Graph 310 Union Find 261 + 323 + (hard)305
310. Minimum Height Trees queue: degree为1的顶点 degree[ i ] : 和 i 顶点关联的边数. 先添加整个图,然后BFS删除每一层degree为1的节 ...
- networkx生成网络
ER随机网络,WS小世界网络,BA无标度网络的生成 import networkx as nx import matplotlib.pyplot as plt #ER随机网络 #10个节点,连接概率为 ...
- DevExpress启动时的全屏SplashScreen
使用DevExpress启动时弹出"正在加载"的Logo,而且是全屏,这种感觉不太好. 原因是使用了DocmentManager控件,当DocmentManager初始化如果耗时较 ...
- EJB组件开发实记(1)
安装JBoss或者Wildfly jdk1.4以上. Eclipes安装插件 JBoss Tools: eclipes Jee photon 在eclipes 内部点击 >>Windows ...
- 查询安装webpack4.0是否成功时提示无法找到的解决方法
最近使用webpack -v 查询webpack版本时提示无法找到 然后我试着重新全局安装webpack,提示还需要安装webpack-cli 选择yes后虽能成功安装webp ...
- 教妹学 Java:难以驾驭的多线程
00.故事的起源 “二哥,上一篇<集合>的反响效果怎么样啊?”三妹对她提议的<教妹学 Java>专栏很关心. “这篇文章的浏览量要比第一篇<泛型>好得多.” “这是 ...
- js-xlsx 实现前端 Excel 导出(支持多 sheet)
之前写文章介绍了使用 js-xlsx 实现导入 excel 的功能,现在再介绍一下如何使用 js-xlsx 进行 excel 导出. [实现步骤] 1. 首先安装依赖 npm install xlsx ...
- java高并发系列 - 第13天:JUC中的Condition对象
本文目标: synchronized中实现线程等待和唤醒 Condition简介及常用方法介绍及相关示例 使用Condition实现生产者消费者 使用Condition实现同步阻塞队列 Object对 ...
- C# WinForm实现禁止最大化、最小化、双击标题栏、双击图标等操作
protected override void WndProc(ref Message m) { if (m.Msg==0x112) { switch ((int) m.WParam) { //禁止双 ...
- python登陆代码简单逻辑
孩子:妈妈,我想要一个登陆的接口 妈妈:写,现在写,写1个够吗? 孩子:够了,妈妈真好,谢谢妈妈. 需求写一个简单的登陆逻辑: 1.定义一个账号和密码 2.输入账号和密码,密码要求密文 3.输入正确提 ...