博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UVA - 524 Prime Ring Problem
阅读量:5136 次
发布时间:2019-06-13

本文共 1673 字,大约阅读时间需要 5 分钟。

  

A ring is composed of n (even number) circles as shown in diagram. Put natural numbers $1, 2, \dots, n$ into each circle separately, and the sum of numbers in two adjacent circles should be a prime.

Note: the number of first circle should always be 1.

 

n (0 < n <= 16)

 

The output format is shown as sample below. Each row represents a series of circle numbers in the ring beginning from 1 clockwisely and anticlockwisely. The order of numbers must satisfy the above requirements.

You are to write a program that completes above process.

 

68

 

Case 1:1 4 3 2 5 61 6 5 2 3 4Case 2:1 2 3 8 5 6 7 41 2 5 8 3 4 7 61 4 7 6 5 8 3 21 6 7 4 3 8 5 2
 
 
 
#include 
#define MAXN 102400using namespace std;int n;bool isp[MAXN];int res[32];int vis[32];void init_isp(){ isp[0] = isp[1] = false; for(int i = 2; i < MAXN; i++) isp[i] = true; for(int i = 2; i < sqrt(MAXN)+1; i++) if(isp[i]) for(int j = 2; i*j < MAXN; j++) isp[i*j] = false;}void dfs(int cur){ if(cur == n && isp[res[0]+res[n-1]]){ printf("%d", res[0]); for(int i = 1; i < n; i++) printf(" %d", res[i]); printf("\n"); } else{ for(int i = 2; i <= n; i++){ if(!vis[i] && isp[res[cur-1]+i]){ res[cur] = i; vis[i] = 1; dfs(cur+1); vis[i] = 0; } } }}int main(){ int kase = 0; init_isp(); while(cin >> n) { if(kase++) printf("\n"); printf("Case %d:\n",kase); res[0] = 1; memset(vis, 0, sizeof(vis)); vis[1] = 1; dfs(1); } return 0;}

转载于:https://www.cnblogs.com/kunsoft/p/5312771.html

你可能感兴趣的文章
侧边栏广告和回到顶部
查看>>
使用@property
查看>>
linux文件系统下的特殊权限
查看>>
day9
查看>>
Django(admin)
查看>>
针对express新version,通过Node.js, Express, Ejs, Mongodb搭建一个简单的web应用。可实现用户的查看和增加。...
查看>>
sigaction
查看>>
基础知识回顾系列
查看>>
外键约束
查看>>
RMAN数据库异机迁移步骤
查看>>
mysql metadata lock
查看>>
编程的32个算法
查看>>
CSS:CSS定位和浮动
查看>>
Java:基本数据类型包装类
查看>>
Java:IO流之字节流InputStream、OutputStream详解
查看>>
216 Combination Sum iii
查看>>
杭电1159 Common Subsequence【最长公共子序列】
查看>>
UVa 11464 Even Parity
查看>>
第二周 9.5 --- 9.11
查看>>
LATEX双栏最后一页如何平衡两栏内容
查看>>