[Hdu] Anti-prime Sequences

Source: Internet
Author: User

Anti-prime Sequences
Time Limit: 6000/3000 ms (Java/Other) Memory Limit: 60000/30000 K (Java/Other)

Total Submission (s): 8 Accepted Submission (s): 5

Problem 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. 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 2
1 10 3
1 10 5
40 60 7
0 0 0
 

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

Source
PKU
 
[Cpp]
// 8.16.cpp: defines the entry point of the console application.
//
 
# Include "stdafx. h"
 
# Include <cstdio>
# Include <cstring>
# Deprecision MAX 10500
Int n, m, d;
Int flag [MAX], res [MAX], prime [MAX + 1];
Bool fflag;
Void Prime ()
{
Prime [1] = 1;
For (int I = 2; I * I <= MAX; I ++)
{
If (! Prime [I])
{
For (int j = I <1; j <= MAX; j + = I)
Prime [j] = 1;
}
}
}
Bool check (int k)
{
If (k = 0) return true;
Int sum = res [k];
For (int I = K-1; I> = 0 & I> = k-d + 1; I --)
{
Sum + = res [I];
If (! Prime [sum]) return false;
}
Return true;
}
Void DFS (int k) // k indicates the size of the elements loaded in the current res Array
{
If (k = m-n + 1)
{
Fflag = true;
For (int I = 0; I <= m-n; I ++)
{
If (I = 0) printf ("% d", res [I]);
Else printf (", % d", res [I]);
}
Return;
}
If (fflag) return;
For (int I = n; I <= m; I ++)
{
If (! Flag [I])
{
If (fflag) return;
Res [k] = I;
If (check (k ))
{
Flag [I] = true;
DFS (k + 1 );
If (fflag) return;
Flag [I] = false;
}
}
}
}
Int main ()
{
Prime ();
While (scanf ("% d", & n, & m, & d), (n | m | d ))
{
Fflag = false;
Memset (flag, false, sizeof (flag ));
DFS (0 );
If (! Fflag) printf ("No anti-prime sequence exists .");
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.