Anti-prime Sequences
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.
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.
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. 40,41,43,42,44,46,45,47,48,50,55,53,52,60,56,49,51,59,58,57,54
Source
素数筛选加深搜。
代码如下:
#include<stdio.h>
#include<string.h>
int prime[10001];//数组至少要开到10000,因为数据范围是n,m:1~1000,d:2~10
int vis[10001];//标记是否访问过
int ans[10001];//存放答案的数组
int n;
int m;
int d;
int p;//p表示ans数组中已有的个数
void find_prime(void){//素数筛选函数
memset(prime,1,sizeof(prime));
prime[1]=0;
prime[2]=1;
for(int i=2;i<=10000;i++){
if(prime[i])
for (int j = i+i; j < 10000; j += i)//更好的写法是j=i*i,谨记
prime[j] = 0;
}
}
int dfs(void){
if(p>=2){//如果p>=2,判断和是否为素数,如果有一个不是素数就要返回0
int sum=ans[p-1];//倒着从后往前加
int j=p-d;//p-d表示的是在ans数组中要到达的位置
if(j<0)
j=0;
for(int i=p-2;i>=j;i--){
sum+=ans[i];
if(prime[sum])//如果sum不是素数,返回0
return 0;
}
}
if(p==m-n+1)//如果ans中已有答案的个数和给出的数字个数相同,说明找到了符合要求的答案,返回1
return 1;
for(int i=n;i<=m;i++)//这个循环是很经典的深搜模版,递归思想的调用十分浓厚,好好体会
if(!vis[i]){
vis[i]=1;
ans[p++]=i;
if(dfs())
return 1;
p--;
vis[i]=0;
}
return 0;
}
int main(void){
find_prime();//调用函数先筛选素数
while(scanf("%d%d%d",&n,&m,&d),n+m+d){//数据读入
p=0;
memset(vis,0,sizeof(vis));
if(dfs()){
for(int i=0;i<p-1;i++)//输出结果
printf("%d,",ans[i]);
printf("%d\n",ans[p-1]);
}
else
printf("No anti-prime sequence exists.\n");
}
return 0;
}