HDU
来源:互联网 发布:mac 局域网ip 编辑:程序博客网 时间:2024/06/14 06:51
Prime Ring Problem
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 54066 Accepted Submission(s): 23934
Problem Description
A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., 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.
![](http://acm.hdu.edu.cn/data/images/1016-1.gif)
Note: the number of first circle should always be 1.
![](http://acm.hdu.edu.cn/data/images/1016-1.gif)
Input
n (0 < n < 20).
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. Print solutions in lexicographical order.
You are to write a program that completes above process.
Print a blank line after each case.
You are to write a program that completes above process.
Print a blank line after each case.
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
题意:给定数字n 输出长度为n首尾相接的素数环 要求每相邻两个数的和是素数
思路:先把会用到的素数全都判出来 深搜填每一个数 最后特判第一个数和最后一个数
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <queue>#include <stack>#include <map>#include <cmath>#include <vector>#define max_ 100010#define inf 0x3f3f3f3f#define ll long longusing namespace std;int n,casenum=0;int num[30];bool prime[50],vis[30];void dfs(int x){if(x==n+1){if(prime[num[n]+1]==false){for(int i=1;i<n;i++)printf("%d ",num[i]);printf("%d\n",num[n] );}return;}int i;for(i=2;i<=n;i++){if(vis[i]==false&&prime[i+num[x-1]]==false){num[x]=i;vis[i]=true;dfs(x+1);vis[i]=false;}}}int main(int argc, char const *argv[]){int i,j;for(i=2;i<=50;i++){if(prime[i]==false){for(j=i<<1;j<=50;j+=i)prime[j]=true;}}while(scanf("%d",&n)!=EOF){printf("Case %d:\n",++casenum);num[1]=1;memset(vis,false,sizeof(vis));dfs(2);printf("\n");}return 0;}
阅读全文
0 0
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- HDU
- HDU
- hdu
- hdu
- HDU
- Hdu
- hdu
- hdu-
- hdu
- hdu
- hdu
- HDU
- HttpClients 工具类
- HDFS只支持文件append操作, 而依赖HDFS的HBase如何完成增删改查功能
- SQL---基础语句select
- C考卷编译
- 题目1
- HDU
- C++类方法实现简单的贪吃蛇游戏
- 关联规则之Apriori算法
- mysql
- 腾讯笔试题 2 有趣的数字
- The server time zone value '�й���ʱ��' is unrecognized or represents more than one time zone. You m
- Codeforces-429-2-B Godsend
- ###Jsp+Servlet购物商城day02.2:首页显示 最新和热门商品
- iOS开发之Masonry框架源码解析