uva 724 Prime Ring Problem
来源:互联网 发布:一入淘宝深似海下一句 编辑:程序博客网 时间:2024/05/18 09:30
A ring is composed of n (even number) circles as shown in diagram. Put natural numbers 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.
Input
n (0 < n <= 16)Output
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.
Sample Input
68
Sample Output
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 <iostream>#include <cstdio>#include <cstring>#include <cmath>using namespace std;int vis[18],a[18],isprime[36];int n;int tot=0;void make_prime(){ int tmp=sqrt(2*n+0.5); memset(isprime,0,sizeof(isprime)); for(int i=2;i<=tmp;i++) if(!isprime[i]) for(int j=i*2;j<=2*n;j+=i) isprime[j]=1;}void dfs(int cnt){ if(cnt==n && !isprime[a[0]+a[n-1]]) { for(int i=0;i<n;i++) cout<<a[i]<<" "; cout<<endl; } else for(int i=2;i<=n;i++) if(!vis[i] && !isprime[i+a[cnt-1]]) { a[cnt]=i; vis[i]=1; dfs(cnt+1); vis[i]=0; }}int main(){ while(~scanf("%d",&n)&&n) { tot++; cout<<"Case "<<tot<<":\n"; memset(vis,0,sizeof(vis)); memset(a,0,sizeof(a)); make_prime(); a[0]=1; dfs(1); } return 0;}
0 0
- uva 724 Prime Ring Problem
- Prime Ring Problem UVA
- Prime Ring Problem UVA
- Prime Ring Problem UVA
- UVA 524 - Prime Ring Problem
- uva 524 - Prime Ring Problem
- uva 524 Prime Ring Problem
- UVa:524 Prime Ring Problem
- uva 524 - Prime Ring Problem
- UVa 524 - Prime Ring Problem
- UVA - 524 Prime Ring Problem
- UVa-524 - Prime Ring Problem
- UVA - 524 Prime Ring Problem
- UVa - 524 - Prime Ring Problem
- UVA - 524 Prime Ring Problem
- uva 524Prime Ring Problem
- uva 524 - Prime Ring Problem
- Prime Ring Problem(UVA 524)
- HDU 3001 三进制状压DP
- c primer plus编程练习第十一章(纯手打欢迎指错)
- [Java大数+输入输出优化] hdu 5047 Sawtooth
- C++中宏定义与内联函数区别
- acdream 1412 2-3 Trees dp
- uva 724 Prime Ring Problem
- 快播网站最后,她打开厢房的窗户,把一盆浊水给倒进湖里
- 马尔科夫随机场
- 推荐一个学通信技术的网站,百度“51学通信”第一个就是
- My Solution: Word Ladder
- hdu5009 Paint Pearls 西安网络赛C题
- atitit.组件化事件化的编程模型--服务端控件(1)---------服务端控件与标签的关系
- 【约瑟夫】编程实现约瑟夫环
- 基于HibernateTemplate的dao和service文件的自动生成