标签:
UVA - 524
Description 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.
Inputn (0 < n <= 16)OutputThe 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.
Sample Input6 8 Sample OutputCase 1: 1 4 3 2 5 6 1 6 5 2 3 4 Case 2: 1 2 3 8 5 6 7 4 1 2 5 8 3 4 7 6 1 4 7 6 5 8 3 2 1 6 7 4 3 8 5 2 Miguel A. Revilla 1999-01-11 |
#include <vector> #include <list> #include <map> #include <set> #include <deque> #include <queue> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iostream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <cctype> #include <string> #include <cstring> #include <cstdio> #include <cmath> #include <cstdlib> #include <ctime> using namespace std; typedef long long LL; #define CLR(x,y) memset((x),(y),sizeof((x))) #define FOR(x,y,z) for(int (x)=(y);(x)<(z);(x)++) #define FORD(x,y,z) for(int (x)=(y);(x)>=(z);(x)--) #define FOR2(x,y,z) for((x)=(y);(x)<(z);(x)++) #define FORD2(x,y,z) for((x)=(y);(x)>=(z);(x)--) const int maxn = 20; int ispri[maxn*2],n,vis[maxn],res[maxn]; void setpri(){ ispri[1] = 1; CLR(ispri,-1); FOR(i,2,maxn*2){ if(ispri[i]){ for(int j = i + i;j < maxn*2;j += i) ispri[j] = 0; } } } void dfs(int cur){ if(cur == n ){ if(!ispri[res[0] + res[n - 1]]) return ; printf("%d",res[0]); FOR(i,1,n){ printf(" %d",res[i]); } puts(""); return ; } FOR(i,2,n+1){ if(!vis[i] && ispri[ res[cur - 1] + i ]){ vis[i] = 1; res[cur] = i; dfs(cur + 1); vis[i] = 0; } } } int main(){ int cntcase = 0; setpri(); while(~scanf("%d",&n)){ if(cntcase) puts(""); printf("Case %d:\n",++cntcase); CLR(vis,0); vis[1] = res[0] = 1; dfs(1); } return 0; } |
[2016-02-19][UVA][524][Prime Ring Problem]
标签:
原文地址:http://www.cnblogs.com/qhy285571052/p/5202251.html