惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
G
Google Developers Blog
S
SegmentFault 最新的问题
Microsoft Security Blog
Microsoft Security Blog
J
Java Code Geeks
罗磊的独立博客
H
Hackread – Cybersecurity News, Data Breaches, AI and More
量子位
P
Proofpoint News Feed
博客园 - 【当耐特】
MongoDB | Blog
MongoDB | Blog
L
LangChain Blog
F
Fortinet All Blogs
C
Check Point Blog
博客园_首页
I
InfoQ
Jina AI
Jina AI
Blog — PlanetScale
Blog — PlanetScale
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
酷 壳 – CoolShell
酷 壳 – CoolShell
Engineering at Meta
Engineering at Meta
美团技术团队
Vercel News
Vercel News
Apple Machine Learning Research
Apple Machine Learning Research

OhYee 博客

小鹏辅助驾驶测评|OhYee 博客 小鹏非支持手机开启自动解锁|OhYee 博客 使用函数计算实现 301 重定向|OhYee 博客 针对 HTML 内容使用 Ant Design 图片弹框|OhYee 博客 博客进程泄露及僵尸进程解决|OhYee 博客 蓝易云服务器体验|OhYee 博客 SSH 调起本地 VSCode|OhYee 博客 【2022 秋招内推】阿里云后端研发工程师|OhYee 博客 使用函数计算获取 IP 地址信息|OhYee 博客 正确获取客户端 IP/HTTP Header 也可能重复|OhYee 博客 评测 Oculus Quest2 及 BigScreen|OhYee 博客 NextJS 热重载保留状态|OhYee 博客 如何优雅地贴 gist 代码|OhYee 博客 Linux 精细化文件权限|OhYee 博客 VSCode 容器开发环境|OhYee 博客 Clash 的不兼容更新排查|OhYee 博客 Zeek 导出 PCAP|OhYee 博客 记一次 ssh 配置问题|OhYee 博客 Git Commit 规范化工具|OhYee 博客 谈谈《星之卡比-探索发现》|OhYee 博客 VSCode 快捷键绑定 Shell 命令|OhYee 博客 ASN.1 语法及 X.509 证书格式解析解析|OhYee 博客 腾讯企业邮箱忽略 MX 记录发信|OhYee 博客 Chrome/Edge 标签组插件|OhYee 博客 【应届内推】阿里云后端研发工程师|OhYee 博客 损坏的 Typecho 备份处理为 JSON|OhYee 博客 VS Code VIM 插件高效使用|OhYee 博客 SSH 正反向代理|OhYee 博客 Let's Encrypt 根证书过期引发的问题|OhYee 博客 OpenWRT 忽略内核依赖|OhYee 博客
AOJ 781.素数环|OhYee 博客
2017-03-18 · via OhYee 博客

这是一篇最后编辑于 8 年前 的文章,其内容可能与目前实际情况差异较大,请注意甄别

题目

{% raw %}

{% endraw %} 把1-20这20个数摆成一个环,要求相邻的两个数的和是一个素数。编写程序,对给定的第一个数m,打印出满足条件的一种排列顺序。如果有多组解,输出字典序最小的一组。

{% raw %}


{% endraw %}

包括多组测试数据,每组测试数据占一行,并且只有一个整数m,(0<=m<=20),当m=0时,表示输入结束。

{% raw %}


{% endraw %}

对每组测试数据输出一行结果,结果为第一个整数为m的一种排列序列,序列中共有20个整数,各数之间用空格分隔。如果有多组解,输出字典序最小的一组。

{% raw %}




{% endraw %}

1
5
0

{% raw %}


{% endraw %}

1 2 3 4 7 6 5 8 9 10 13 16 15 14 17 20 11 12 19 18
5 2 1 4 3 8 9 10 7 6 11 20 17 12 19 18 13 16 15 14

{% raw %}




{% endraw %}

题解

相邻两个数(包括第一个数和最后一个数)的和为素数
因此先筛法求素数打个素数表方便后面查询

然后 DFS 暴力搜索所有情况(按照字典序顺序)
找到所有都满足的就输出出来即可

每一行最后一个数后面没有空格需要注意下

只有 20 组输入,可以打表

这道题最早数据没有考虑到首位两个数,所以没人 AC 中午找老师改了下数据

代码

```cpp 素数环 https://github.com/OhYee/sourcecode/tree/master/ACM 代码备份 /* By:OhYee Github:OhYee Blog:http://www.oyohyee.com/ Email:oyohyee@oyohyee.com

かしこいかわいい?
エリーチカ!
要写出来Хорошо的代码哦~
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;

const int maxn = 45;

int prime[maxn] = {0},num_prime = 0;
bool isNotPrime[maxn] = {1,1};

void Prime() {
for(long i = 2;i < maxn;i++) {
if(!isNotPrime[i])prime[num_prime++] = i;
for(int j = 0;j < num_prime&&iprime[j] < maxn;j++) {
isNotPrime[i
prime[j]] = true;
if(!(i%prime[j]))break;
}
}
}

bool vis[maxn / 2];
int List[maxn / 2];
int pos;
bool dfs(){
if(pos == 20) {
if(isNotPrime[List[19] + List[0]])
return false;

    bool first = true;
    for(int i = 0;i < 20;i++) {
        if(!first)
            printf(" ");
        first = false;
        printf("%d",List[i]);
    }
    return true;
}
for(int i = 1;i <= 20;i++) {
    if(vis[i])
        continue;
    if(isNotPrime[List[pos - 1] + i])
        continue;
    List[pos++] = i;
    vis[i] = true;
    if(dfs())
        return true;
    vis[i] = false;
    pos--;
}
return false;

}

bool Do() {
int n;
scanf("%d",&n);
if(n == 0)
return false;
memset(vis,false,sizeof(vis));
pos = 0;

List[pos++] = n;
vis[n] = true;
dfs();
printf("\n");

return true;

}

int main() {
Prime();
while(Do());
return 0;
}

</div>