HDU 1016 prime ring problem DFS + memory-based search

Source: Internet
Author: User

To be honest, this question is really not done, and there are some ideas, but there are problems that cannot be solved, and then others provide ideas.

Not much to mention ....

This question requires the formation of a ring. The sum of no two adjacent numbers cannot be prime numbers. Of course, the last one should consider the relationship between them and the first one. In fact, the DFS here only needs to recursion, what I do with recursion is always wrong. It is mainly affected by the deformation class. Actually, recursion is definitely the current number, and the current number is stored in the array, so that the next recursion can be compared with the number of this time ....

# Include <stdio. h> # include <string. h> int N, num [25], des [25]; int gcd (int n) {int f = 0; If (n = 2 | n = 3) return 1; for (INT I = 2; I <= n/2; ++ I) if (N % I = 0) return 0; return 1 ;} void DFS (int x) {If (x = N & gcd (Num [N] + 1) // when the last one arrives {for (INT I = 1; I <= N; ++ I) printf (I = 1? "% D": "% d", num [I]); // puts ("");} else {for (INT I = 2; I <= N; ++ I) {If (! Des [I] & gcd (I + num [x]) // find the number of matching conditions {des [I] = 1; // memory num [x + 1] = I; DFS (x + 1); des [I] = 0; // backtracking }}} int main () {int T = 0; while (scanf ("% d", & N )! = EOF) {memset (Num, 0, sizeof (Num); memset (DES, 0, sizeof (DES); des [1] = num [1] = 1; printf ("case % d: \ n", ++ T); DFS (1); puts ("") ;}return 0 ;}

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.