1002.Anti-prime Sequences

来源:互联网 发布:桃之卵淘宝 编辑:程序博客网 时间:2024/04/30 01:03


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 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 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
看题第一感觉,应该是全排列,然后不断回朔,并且找到一个答案便停止搜索,Code如下:
#include <stdio.h>#include <stdlib.h>int flag=0;void findanswer(int *,int *,int ,int ,int);int judge(int );int main(){    int N,M,D;    int *first,*second,i;    while (scanf("%d%d%d",&N,&M,&D)==3 && !(N==0 && M==0 && D==0))    {              getchar();              first=(int *)malloc((M-N+1)*sizeof(int));              second=(int *)malloc((M-N+1)*sizeof(int));              for (i=0;i<M-N+1;i++)              {                  second[i]=N+i;                  first[i]=0;              }              findanswer(first,second,M-N+1,M-N+1,D);              if (flag==0) printf("No anti-prime sequence exists.\n");              free(first);              free(second);              flag=0;    }    return 0;}void findanswer(int *first,int *second,int left,int lenth,int degree){    int i,n,tmp_num,degree_now;    if (flag==1) return ;    if ((lenth-left)>=2)    {        for (degree_now=2;degree_now<=degree;degree_now++)            {             for (i=lenth-left-1,n=degree_now,tmp_num=0;n>0;i--,n--)  tmp_num+=first[i];             if (judge(tmp_num)==0) return ;             }    }    if (left==0)       {           for (i=0;i<lenth-1;i++) printf("%d,",first[i]);           printf("%d\n",first[lenth-1]);           flag=1;           return ;       }    else        {            for (i=0;i<lenth;i++)                {                    if (second[i]!=0)                       {                           int *first_now,*second_now;                           first_now=(int *)malloc(lenth*sizeof(int));                           second_now=(int *)malloc(lenth*sizeof(int));                           for (n=0;n<lenth;n++)                               {                                   first_now[n]=first[n];                                   second_now[n]=second[n];                               }                           first_now[lenth-left]=second[i];                           second_now[i]=0;                           findanswer(first_now,second_now,left-1,lenth,degree);                           free(first_now);                           free(second_now);                       }                }            return ;        }   }int judge(int num){    int i;    for (i=2;i<=sqrt((double)num);i++)        if (num%i==0) return 1;    return 0;}

剪枝,当前面出现不符合序列时,立即退出

这是网上的一段代码,可以参考:

  1. #include <iostream>  
  2. #include <cstring>  
  3. using namespace std;  
  4. const int N = 10000;  
  5. int n, m, d;  
  6. int ans[1100];  //保存答案  
  7. bool prime[N];  //素数表  
  8. bool vis[1100]; //访问标识  
  9. bool f; //标记是否找到答案  
  10. void get_prime()  
  11. {  
  12.     memset(prime, truesizeof(prime));  
  13.     for (int i = 2; i <= 100; i++) {  
  14.         if (prime[i]) {           
  15.             for (int j = 2; j*i <= N; j++)  
  16.                 prime[j*i] = false;  
  17.         }  
  18.     }  
  19. }  
  20.   
  21. bool check_prime(int cnt)  
  22. {  
  23.     int sum = ans[cnt];  
  24.     int j = cnt - 1;  
  25.     for (int i = 2; i <= d && j >= 0; i++, j--) {  
  26.         sum += ans[j];  
  27.         if (prime[sum])  
  28.             return false;  
  29.     }  
  30.     return true;  
  31. }  
  32. void dfs(int cnt)  
  33. {     
  34.     if (cnt == m-n+1) {  
  35.         f = true;  
  36.         return ;  
  37.     }  
  38.     for (int i = n; i <= m; i++) {  
  39.         if (vis[i])  
  40.             continue;  
  41.         ans[cnt] = i;         
  42.         if (check_prime(cnt))  
  43.         {             
  44.             vis[i] = true;            
  45.             dfs(cnt+1);  
  46.             if (f) {  //已经找到答案就直接返回!  
  47.                 return;  
  48.             }  
  49.             vis[i] = false;  
  50.         }  
  51.     }  
  52. }  
  53. int main()   
  54. {  
  55.     get_prime();  
  56.     while (cin >> n >> m >> d) {  
  57.         if (n == 0)  
  58.             break;  
  59.         int cnt = 0;  
  60.         f = false;  
  61.         memset(vis, falsesizeof(vis));  
  62.         dfs(cnt);  
  63.         if (f) {  
  64.             for (int i = 0; i < m-n; i++) {  
  65.                 cout << ans[i] << ',';  
  66.             }  
  67.             cout << ans[m-n] << endl;  
  68.         }  
  69.         else   
  70.             cout << "No anti-prime sequence exists.\n";  
  71.     }  
  72.     return 0;  
  73. }     

0 0
原创粉丝点击