Poj 2034 anti-prime sequences (DFS)

Source: Internet
Author: User

// Adjacent 2. 3 ...... the sum of D is not a prime number # include <algorithm> # include <stdio. h> using namespace STD; int num [1010], vis [1010]; int n, m, D, cot; int flag [10010]; void Init () // Number of prime numbers in the table {int I, j; for (I = 2; I <10010; I ++) {If (! Flag [I]) for (j = I * I; j <10010; j = J + I) Flag [J] = true;} flag [1] = true; flag [0] = true;} bool judge () {int sum, I, K; sum = num [cot-1]; If (cot-D> = 0) k = cot-D; else K = 0; for (I = cot-2; I> = K; I --) {sum = sum + num [I]; If (! Flag [Sum]) // return true;} return false;} bool DFS () {int I; If (COT> = 2) {If (Judge ()) return false;} If (COT = m-n + 1) return true; for (I = N; I <= m; I ++) {If (! Vis [I]) {num [cot ++] = I; vis [I] = true; If (DFS () return true; vis [I] = false; cot --;} return false;} int main () {int I; Init (); // first judge whether it is a prime number while (~ Scanf ("% d", & N, & M, & D), N + M + d) {memset (VIS, 0, sizeof (VIS )); memset (Num, 0, sizeof (Num); COT = 0; If (DFS () {for (I = 0; I <cot; I ++) {if (I = cot-1) printf ("% d \ n", num [I]); elseprintf ("% d ,", num [I]) ;}} elseprintf ("no anti-prime sequence exists. \ n ") ;}return 0 ;}

poj 2034 anti-prime sequences (DFS)

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.