hdu1016 Prime Ring problem

Source: Internet
Author: User

Prime Ring problem

Time limit:4000/2000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 41154 Accepted Submission (s): 18221


Problem Descriptiona 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 the adjacent circles should is a PR Ime.

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

INPUTN (0 < n < 20).

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. Print Solutions in lexicographical order.

You is to write a program, that completes above process.

Print a blank line after each case.

Sample Input68

Sample outputcase 1:1 4 3 2 5 6 5 2 3 4 case 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 Direct violence search, I guess. Need pruning, because there is no pruning, then test the n=20, half a day no results. So I thought about it. Pruning: 1 The starting number is not 1, indicating that all the combinations have been found, the rest is the rotation of the result of the Ring 2 if the current number and its previous addition is not a prime, you do not have to continue to look down.
#include <iostream>#include<stdio.h>using namespacestd;intN;intnum[ -];BOOLvis[ -];intprime[ -];voidDfsinta) {    if(a>n+1)return; if(num[1]!=1)return; if(a==n+1)    {         for(intI=1; i<n+1; i++)        {            if(!prime[num[i%n]+num[(i+1)%n])return; }        //if (num[1]==1)// {         for(intI=1; i<n; i++) printf ("%d", num[i%n]); printf ("%d", num[0]); printf ("\ n"); // }        return; }     for(intI=1; i<=n; i++)    {        if(!Vis[i]) {Num[a%n]=i; if(a>1)            {                if(!prime[num[a%n]+num[(A-1)%N]])Continue; }            //cout<<num[a%n]<< "" <<endl;vis[i]=true; DFS (a+1); Vis[i]=false; }    }}intMain () { for(intI=0; i< -; i++) prime[i]=true;  for(intI=2; i< -; i++)         for(intj=i<<1; j< -; j+=i) {if(Prime[j]) prime[j]=false; }    intt=1;  while((scanf ("%d", &n))! =EOF) {         for(intI=0; i< -; i++) {Num[i]=0; Vis[i]=false; } num[1]=1; printf ("Case %d:\n", t++); DFS (1); printf ("\ n"); }    return 0;}
View Code

Sourceasia 1996, Shanghai (Mainland China)

Direct Violence Search 9

hdu1016 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.