[2016-02-19] [UVA] [524] [Prime Ring problem]

Source: Internet
Author: User
Tags cmath

[2016-02-19] [UVA] [524] [Prime Ring problem]
UVA-524Prime Ring problem
Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld &%llu

Submit Status

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 both adjacent circles should be a prime.


Note: the number of first circle should always be 1.

InputN (0 < n <=)

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 is to write a program, that completes above process.

Sample Input
68

Sample Output
Case 1:1 4 3 2 5 a 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


Miguel A. Revilla
1999-01-11
    • Time: 2016-02-19-21:16:47
    • Topic Number: UVA 524
    • Title: Given the number n, the number of output 1~n is arranged so that the sum of the adjacent numbers is the prime number (the first connection)
    • Method: Dfs enumeration answer, backtracking optimization.
    • Problems encountered during the problem solving process:
      • Format problem: There is no blank line after the last set of data, otherwise WA
      • There are no spaces after the last number in each line, otherwise the PE



#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 The CLR (x , Y) memset ((×), (y),sizeof((x))) #define for (x, y , z) for (int.) = (y);(x) < (*);(x) + +) #define FORD ( z/y) for(int (x) = (y);(x) >= (*);(x)--) #define FOR2 ( z) for((x) = (y);(x) < (*);(x) + +) #define FORD2 ( x, Y, z) for((×) = (y);(x) >=;(x) const int maxn =; 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;}






From for notes (Wiz)

[2016-02-19] [UVA] [524] [Prime Ring problem]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.