题目链接

题意

构造一张有\(n(3\le n\le 1000)\)个点的无向图(无重边和自环)。满足:

  1. 边的总数为素数
  2. 所有点的度数均为素数

输出方案

solution

如果所有点的度数确定了。那么边数就是度数之和的一半。连边就很简单了。

所以考虑怎么确定点的度数。

猜想:必有至少一个\(A \in [2n,3n] (3 \le n \le 1000)\)满足\(\frac{A}{2}\)为素数。

经测试,成立。

所以可以让所有点的度数都为\(2\)或\(3\)只要找到这个\(A\)作为度数之和。然后边数就是\(\frac{A}{2}\)。

设度数为\(2\)的点有\(x\)个,度数为\(3\)的点有\(y\)个。

列方程:

\[\left\{
\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的更多相关文章

  1. Codeforces 1009D:Relatively Prime Graph

    D. Relatively Prime Graph time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  2. 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( ...

  3. Relatively Prime Graph CF1009D 暴力 思维

    Relatively Prime Graph time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  4. 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 ...

  5. [Codeforces 1178D]Prime Graph (思维+数学)

    Codeforces 1178D (思维+数学) 题面 给出正整数n(不一定是质数),构造一个边数为质数的无向连通图(无自环重边),且图的每个节点的度数为质数 分析 我们先构造一个环,每个点的度数都是 ...

  6. CodeForces - 1009D Relatively Prime Graph

    题面在这里! 直接暴力找点对就行了,可以证明gcd=1是比较密集的,所以复杂度略大于 O(N log N) #include<bits/stdc++.h> #define ll long ...

  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

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 1000以内就有非常多组互质的数了(超过1e5) 所以,直接暴力就行...很快就找完了 (另外一开始头n-1条边找1和2,3...n就好 [代 ...

  9. Codeforces 1178D. Prime Graph

    传送门 首先每个点至少要有两条边连接 那么容易想到先保证这一点然后再慢慢加边 那么先构成一个环即可:$(1,2),(2,3),(3,4)...(n,1)$ 然后考虑加边,发现一个点加一条边还是合法的, ...

随机推荐

  1. golang:exported function Script should have comment or be unexported

    当自己定义的包被外部使用时,如果不遵循一定的规范,那么会出现讨厌的绿色纹条,还会警告: 虽然不会影响运行,但是也令人讨厌,那么如何解决这个问题呢? 为结构体或者变量或者方法添加注释,并且开头必须是结构 ...

  2. DFS(四):剪枝策略

    顾名思义,剪枝就是通过一些判断,剪掉搜索树上不必要的子树.在采用DFS算法搜索时,有时候我们会发现某个结点对应的子树的状态都不是我们要的结果,这时候我们没必要对这个分支进行搜索,砍掉这个子树,就是剪枝 ...

  3. DbgUiConnectToDbg(ntdll.dll)函数逆向

    暂时未解决问题: 1.  [fs+0F24h]中存储着什么东西. 答案:其存放着被调试程序的DbgObject句柄._NtCreateDebugObject(ntoskrnl.exe)函数逆向分析 该 ...

  4. Eclipse导入SpringBoot项目pom.xml第一行报错Unknown error

    1.网上搜的都说是将SpringBoot2.1.5版本降级到SpringBoot2.1.4版本,感觉这治标不治本啊,以后想升级不是玩完了. 错误如下所示: 参考:https://ask.csdn.ne ...

  5. JavaFx出现错误Caused by: java.lang.NullPointerException: Location is required的解决方法

    问题截图: "C:\Program Files\Java\jdk1.8.0_131\bin\java.exe" "-javaagent:I:\IntelliJ IDEA ...

  6. SQL常用函数之STR()

    使用str函数   :STR 函数由数字数据转换来的字符数据.   语法      STR    (    float_expression    [    ,    length    [    , ...

  7. VS Code 快捷键 && 常用插件

    常用插件    分类 插件名称 说明 开发 C# C#语言 C# Extensions C#扩展功能(添加类,接口,智能提示) C# XML Documentation Comments 代码添加注释 ...

  8. python IPy库

    Website: https://github.com/haypo/python-ipy/ 安装: easy_install IPy   >>> from IPy import IP ...

  9. pycharm 取消连按两下shift出现的全局搜索

    在来回切换中英文输入法的时候连按两下shift总是会蹦出来全局搜索框 真的很是麻烦,现在是把这个框给禁用掉 1.按ctrl+shift+a,弹出搜索框2.输入registry,然后按回车3.找到“id ...

  10. Spring Boot 中如何支持异步方法

    本人免费整理了Java高级资料,涵盖了Java.Redis.MongoDB.MySQL.Zookeeper.Spring Cloud.Dubbo高并发分布式等教程,一共30G,需要自己领取.传送门:h ...