Anti-prime sequences

Source: Internet
Author: User

anti-prime Sequences
Time Limit: 3000MS Memory Limit: 30000K
Total Submissions: 3355 Accepted: 1531

Description

Given a sequence of consecutive integers n,n+1,n+2,..., m, an anti-prime sequence are a rearrangement of these integers so t Hat each adjacent pair of integers sums to a composite (non-prime) number. For example, if n = 1 and M = ten, 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 are 1,3,5,4,6,2,10,8,7,9.

Input

Input would consist of multiple input sets. Each set would consist of three integers, N, m, and D on a. The values of N, M and D would satisfy 1 <= n < m <=, and 2 <= D <= 10. The line 0 0 0 would indicate end of input and should 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 seq Uence (do not inserts any spaces and does 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 WI Th 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
Test instructions: The "2,d" length of the continuous sequence of the and are to be composite.
Idea: DFS.
1#include <stdio.h>2#include <algorithm>3#include <iostream>4#include <stdlib.h>5#include <string.h>6#include <queue>7#include <stack>8#include <math.h>9 using namespacestd;TentypedefLong LongLL; One BOOLprime[20000]= {0}; A inttt[10000]; - BOOLcm[1005]; - intts=0; the BOOLCheckintNintm); - intDfsintNintMintDintKkintpp); - intMainvoid) - { +     inti,j,k; -      for(i=2; i<= +; i++) +     { A         if(!Prime[i]) at         { -              for(J=i; (I*J) <=20000; J + +) -             { -prime[i*j]=true; -             } -         } in     } -     intn,m; to      while(SCANF (" %d%d%d", &n,&m,&k), n!=0&&m!=0&&k!=0) +     { -memset (CM,0,sizeof(cm)); thets=0; *         intUu=dfs (0, m-n+1, k,n,m); $         if(UU)Panax Notoginseng         { -printf"%d", tt[0]); the              for(i=1; i< (m-n+1); i++) +             { Aprintf",%d", Tt[i]); the             } +printf"\ n"); -         } $         Elseprintf"No anti-prime sequence exists.\n"); $     } - } - BOOLCheckintNintm) the { -     inti,j;Wuyi  the  -LL sum=Tt[m]; Wu          for(i=m-1; I>=max (N,0); i--) -         { Aboutsum+=Tt[i]; $             if(!Prime[sum]) -                 return false; -         } -         return true; A } + intDfsintNintMintDintKkintpp) the { -     inti; $     if(TS)return 1; the     if(n==m) the     { the  the             BOOLCc=check (n-d,m-1); -             if(!cc) in             { the                 return 0; the             } Aboutts=1; the         return 1; the     } the     Else +     { -         BOOLCc=check (n-d,n-1); the         if(CC)Bayi         { the              for(I=KK; i<=pp; i++) the             { -                 if(TS)return 1; -                 if(!Cm[i]) the                 { thett[n]=i; thecm[i]=true; the                     intUu=dfs (n+1, m,d,kk,pp); -cm[i]=false; the                     if(UU)return 1; the                 } the             }94         } the         Else return 0; the     } the     return 0;98}

Anti-prime sequences

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.