ZOJ 1457 Prime Ring Problem @Z
来源:互联网 发布:淘宝卖家互刷交流群 编辑:程序博客网 时间:2024/06/05 05:09
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1457
http://acm.hdu.edu.cn/showproblem.php?pid=1016
96年的上海。题意,1~N围成一圈,每相邻俩数和必须为质数,第一位必须是1,按字典序输出。水题,深搜,每一个位置判断一下与前一个和是否为质数。最后一位再判断与1之和是否为质数。显然,只有N为偶数时才有可能构造出来,N为奇数时则直接输出空行就好。没有这一句判断,HDOJ上可以过,ZOJ会TLE。
#include<iostream>
#include<cstdio>
#include<set>
#include<cmath>
using namespace std;
int a[100],n;
set<int> b;
bool num[200];
void cir(int m,int l)
{
int j;
if (m==(n+1))
{
if (b.find(l+1)!=b.end())//ÕâÊÇÒ»¸ö»·£¬ËùÒÔÐèÒª¿¼ÂÇ×îºóÒ»¸öÔªËغ͵ÚÒ»¸öÔªËØÖ®ºÍ
{
for (j=1;j<n;j++) cout<<a[j]<<' ';
cout<<a[n]<<endl;
}
}
else
{
for (j=2;j<=n;j++)
if (num[j]==true)
{
int x=j+l;
if (b.find(x)!=b.end())
{
num[j]=false;
a[m]=j;
cir(m+1,j);
num[j]=true;
}
}
}
}
int main()
{
int i,k=0,j;
bool f;
b.insert(2);
for (i=3;i<=37;i++)
{
f=true;
for (j=2;j<=(sqrt(i)+1);j++)
if (i%j==0) {f=false;break;}
if (f==true) b.insert(i);
}
a[1]=1;
num[1]=false;
for (i=2;i<=19;i++) num[i]=true;
while (1)
{
if (scanf("%d",&n)==EOF) break;
k++;
printf("Case %d:\n",k);
if (n%2!=0) {cout<<endl;continue;}
cir(2,1);
cout<<endl;
}
return 0;
}
- ZOJ 1457 Prime Ring Problem @Z
- zoj 1457 Prime Ring Problem
- ZOJ 1457Prime Ring Problem
- ZOJ-1457Prime Ring Problem(搜索)
- ZOJ 1457 Prime Ring Problem(DFS)
- ZOJ-1457 prime ring problem 素数环 题解
- ZOJ 1457 Prime Ring Problem(dfs+剪枝)
- 1016 Prime Ring Problem
- HDOJ1016-Prime Ring Problem
- HDU1016 Prime Ring Problem
- DFS__1195: Prime Ring Problem
- Prime Ring Problem
- 1016:Prime Ring Problem
- 1016 Prime Ring Problem
- HDUOJ1016 Prime Ring Problem
- 1016 Prime Ring Problem
- Prime Ring Problem
- hdu1016 Prime Ring Problem
- 杀毒软件的查杀病毒的原理以及木马、病毒的免杀伎俩
- 关于透明窗口的一些收集
- POJ 1775 Sum of Factorials 二分模板
- Flex+.Net 连续导入或导出 RemoteObject失效
- TCP/IP学习笔记(八)
- ZOJ 1457 Prime Ring Problem @Z
- CCS 3.3 编译 DM365 ubl
- Android Permission(授权)大全
- 递归算法详细分析
- fmt:formatDate标签的输出格式
- 正则学习
- 编译framework找不到资源文件错误
- register_chrdev,class_create()
- 求一条分组查询语句