UVA 524 Prime Ring Problem
来源:互联网 发布:mac电脑还原出厂设置 编辑:程序博客网 时间:2024/06/17 00:46
题意:给定一个数n,从1-n组成的相邻两个数的和是素数的一列数字
刚看到这题的时候思路还是挺清晰的,但在如何输出n对应的所有数列的时候遇到了困难,后来去查了一下题解,用了一个三维的ans数组来保存预处理结果,然后按顺序输出即可,ans[a][b][c]中a是对应数字n,b是第几列,c是第几个数
代码如下
#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<cstdlib>#include<iostream>#include<map>#include<queue>#include<vector>#include<stack>#include<set>#define INF 0x3f3f3f3f#define mem(x) memset(x,0,sizeof(x))#define rep(x,s,e) for(int x=(int)s;x<=(int)e;x++)const double EXP=1e-6;const double PI=acos(-1.0);typedef long long ll;typedef unsigned long long ull;using namespace std;int pri[32]= {1000,0,1,1,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,0,0,0,0,1,0,1};int ans[18][100000][18];int ls[18];int s[18];int b[18];void dfs(int cd,int x){ if(cd==x&&pri[ls[cd-1]+1]==1) { for(int i=0; i<x; i++) { ans[x][s[x]][i]=ls[i]; } (s[x]) ++; return ; } for(int i=cd%2+1; i<=x; i+=2) { if(pri[ls[cd-1]+i]==1&&b[i]==0) { b[i]=1; ls[cd]=i; dfs(cd+1,x); b[i]=0; } }}int main(){ mem(ans); memset(s,0,sizeof(s)); mem(ls); mem(b); ls[0]=1; b[1]=1; for(int i=2; i<=16; i+=2) { dfs(1,i); } int n,cases=1; while(~scanf("%d",&n)) { if(cases!=1) printf("\n"); printf("Case %d:\n",cases++); int k=s[n]; for(int i=1; i<=k; i++) { for(int j=0; j<n; j++) j?printf(" %d",ans[n][i-1][j]):printf("%d",ans[n][i-1][j]); printf("\n"); } }}
0 0
- 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)
- UVA - 524 Prime Ring Problem
- UVA - 524 Prime Ring Problem
- UVa 524 Prime Ring Problem
- UVA 524 Prime Ring Problem
- angularjs中watch使用--实现项目中时时搜索
- 数据中心解决方案之灾备方案设计(下)
- Notification消息栏通知的实现
- 探讨kafka的分区数与多线程消费
- Halcon清晰度检测实例(转)
- UVA 524 Prime Ring Problem
- iOS AVCaptureSession获取图像数据
- 使用pushlet实现消息服务端推送至客户端
- jQuery学习笔记三:获取内容(text、html、val)
- Python使用xslt提取网页数据
- “应用程序无响应”原因汇总
- Android开发之fragment的可替代性的探讨
- Excel automation on Windows Server 2008 x64: solution to SaveAs method problem
- UVA10048Audiophobia(Floyd)