Sicily 1002. Anti-prime sequences DFS

Source: Internet
Author: User

at the beginning, the card got stuck badly. Then it was optimized. Instead, it was Tlm. After reading other people's problem-solving reports, I found that the check function I wrote was complicated and used three cycles, in fact, we can do this in two cycles. We also learned to evaluate the prime number by using the limit method.

# Include <iostream> # include <memory. h> # include <math. h> # include <stdio. h> using namespace STD; int result [1001]; bool used [1001]; bool isprime [10010]; int D, M, N, num; int DFS (INT ); bool isfound; bool check (INT); void prime_list (); int main () {prime_list (); While (CIN> N> m> D &&! (N = 0 & M = 0 & D = 0) {isfound = false; num = m-n + 1; memset (used, false, 1001 * sizeof (bool); DFS (0); If (isfound) {for (INT I = 0; I <num-1; I ++) printf ("% d, ", result [I]); printf (" % d \ n ", result [num-1]);} elseprintf (" no anti-prime sequence exists. \ n ") ;}return 0 ;}int DFS (INT current) {If (isfound) return 0; If (! Check (current) return 0; If (current = num) {isfound = true; return 0 ;}for (INT I = 0; I <num &&! Isfound; I ++) {If (! Used [I]) {used [I] = true; Result [current] = N + I; DFS (current + 1); used [I] = false ;}} return 0;} bool check (INT current) {int sum = 0; If (current <= 1) return true; For (INT I = 0; I <current; I ++) {sum = 0; if (I + D <Current) for (Int J = 0; j <D; j ++) {sum + = Result [I + J]; If (j> 1 & isprime [Sum]) return false;} else {for (Int J = I; j <current; j ++) {sum + = Result [J]; If (J-I> 0 & isprime [Sum]) return Fal Se ;}}return true;} // evaluate 1 ~ using the Evaluate Method ~ Void prime_list () {int I, j; memset (isprime, true, sizeof (isprime); isprime [0] = false; isprime [1] = false; // Note: The minimum positive factor of n not equal to 1 is not greater than the square root of N (I = 2; I * I <= 10000; I ++) {J = 2; If (isprime [I]) {While (I * j <= 10000) {isprime [I * j] = false; j ++ ;}}}}
Related Article

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.