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)$ 然后考虑加边,发现一个点加一条边还是合法的, ...
随机推荐
- matlab键盘快捷键无法使用的解决办法
打开matlab,在主页里点击 预设/preferences 左栏找 键盘/keyboard 点开键盘点击 快捷方式/shortcuts 在右边 活动设置 /Active settings 里选择 W ...
- POJ1961Period(kmp+循环节)
传送门 题目大意:输出字符串所有前缀的循环节个数,下标从1开始,i 和1-i循环节的个数 题解:网上摘得 KMP最小循环节.循环周期: 定理:假设S的长度为len,则S存在最小循环节,循环节的长度L为 ...
- 详解JAVA8函数式接口{全}
1: 函数式接口 1.1 概念 1.2 格式 1.3@FunctionalInterface注解 1.4 调用自定义函数接口 2:函数式编程 2.1:lambda的延迟执行 2.2 使用Lambda作 ...
- .Net ADO拼接带参数的SQL语句
首先是在DAL数据访问层中的代码://数据更新的方法public static int shuxing_update(s_passnature model) { string sql = " ...
- C#上手练习7(方法语句2)
上一篇方法调用赋值封装,这里使用封装后调用,尽量满足开闭原则. 以及静态类的使用. using System; namespace KingTest03 { class Program { int a ...
- wpf/winform获取windows10系统颜色和主题色
Windows10开始微软在系统颜色中添加了深色,对于UWP来说很轻松就能获取到系统当前的颜色和主题色,而对于Win32应用就没有那么直观了. 在wpf中,可以通过SystemParameters.W ...
- 动软生成Model(dapper.common)
<#@ template language="c#" HostSpecific="True" #><#@ output extension= ...
- python基础(22):模块、包
1. 模块 1.1 什么是模块 别人写好的函数.变量.方法放在一个文件里 (这个文件可以被我们直接使用)这个文件就是个模块 常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模 ...
- AwaitAsync(异步和多线程)
参考了一些大佬写的文章: https://www.cnblogs.com/yilezhu/p/10555849.html这个大佬写的文章,我还是很喜欢的 https://www.cnblogs.com ...
- kali linux查看局域网下所有IP,并对指定IP实施局域网内攻击(断网,随时查看对方密码,上网痕迹等)
首先我们打开我们熟悉的kali linux操作系统,利用指令: ifconfig 来确认本机的ip地址 确认了本机的ip地址之后,利用一下的指令查看局域网下所有ip: fping -g 本机IP地址/ ...