[Sicily online] 1002. Anti-prime Sequences

Source: Internet
Author: User

Constraints

Time Limit: 3 secs, memory limit: 32 MB

Description

Given a sequence of consecutive integers n, n + 1, n + 2 ,..., m, an anti-prime sequence is a rearrangement of these integers so that each adjacent pair of integers sums to a composite (non-prime) number. for example, if n = 1 and M = 10, one such anti-prime Sequence
Is 1, 3, 5, 4, 2, 6, 9, 7, 8, 10. this is also the lexicographically first such sequence. we can extend the definition by defining a degree danti-prime sequence as one where all consecutive subsequences of length 2, 3 ,..., d sum to a composite number. the sequence above
Is a degree 2 anti-prime sequence, but not a degree 3, since the subsequence 5, 4, 2 sums to 11. the lexicographically. RST degree 3 anti-prime sequence for these numbers is 1, 3, 5, 4, 6, 2, 10, 8, 7, 9.

Input

Input will consist of multiple input sets. each set will consist of three integers, n, m, and D on a single line. the values of n, m and D will satisfy 1 <=n <m <= 1000, and 2 <= d <= 10. the line 0 0 0 will indicate end of input and shoshould not be processed.

Output

For each input set, output a single line consisting of a comma-separated list of integers forming a degree danti-prime sequence (do not insert any spaces and do not split the output over multiple lines ). in the case where more than one anti-prime sequence exists,
Print the lexicographically first one (I. E ., output the one with the lowest first value; in case of a tie, the lowest second value, etc .). in the case where no anti-prime sequence exists, output no anti-prime sequence exists.

Sample Input

1 10 21 10 31 10 540 60 70 0 0
Sample output

1,3,5,4,2,6,9,7,8,101,3,5,4,6,2,10,8,7,9No anti-prime sequence exists.

40,41,43,42,44,46,45,47,48,50,55,53,52,60,56,49,51,59,58,57,54

Question Analysis: When I first saw this question, I wanted to traverse all the situations according to the sorting algorithm. When I found the conditions, I stopped the traversal. However, the timeout is too serious, especially when there is no sequence. Optimized a day or time-out, and finally turned to the online great god http://blog.csdn.net/ChinaCzy/article/details/5673174

It turns out that this question is done with deep-first search. I think it is perfect. There is a concept of "card time" in it. Although it does not prove that this question is correct when using the card, the results are still correct. The following code uses people's ideas and writes it silently. It should be a huge code:

# Include <iostream> # include <fstream> # include <vector> # include <string> # include <algorithm> # include <cmath> # include <string. h> using namespace STD; const long size = 10000; bool COM [size]; bool used [1010]; int X [1010]; bool OK; int n, m, D, t; void initcomposite () {// The prime number is falseint TMP = SQRT (double) size); For (long I = 2; I <= TMP; I ++) {If (COM [I] = true) continue; For (long J = I; I * j <size; j ++) COM [I * j] = true ;}} void print () {for (INT I = 0; I <M-N; I ++) cout <X [I] <','; cout <X [M-N] <Endl ;} bool check (INT deep) {If (deep <2) return true; int tmpd = 2; int I, flag = 0; while (tmpd <= D) {for (I = 0; I <= Deep-tmpd; I ++) {int sum = 0; vector <int>: size_type J; For (j = 0; j <tmpd; j ++) {sum + = x [I + J];} // end forif (! COM [Sum]) {flag = 1; break;} // end if} // end fortmpd ++; If (flag = 1) return false;} return true ;} void DFS (INT deep) {If (++ T> 4000) return; If (OK) return; If (! Check (deep) return; If (deep = m-n + 1) {OK = true; return;} For (INT I = N; I <= M &&! OK; ++ I) {If (used [I]) continue; used [I] = true; X [Deep] = I; DFS (deep + 1 ); used [I] = false;} // end for} int main () {initcomposite (); While (CIN> N> m> D & n! = 0) {OK = false; t = 0; memset (used, 0, sizeof (used); memset (x, 0, sizeof (x )); DFS (0); If (OK) print (); else cout <"no anti-prime sequence exists. "<Endl;} // end while}

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.