1002. Anti-prime Sequences
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 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
题意解释一下。寻找n到m间的反质数数列。
反质数数列就是数列中的一个数与他前面p-1,p-2,p-3等等个数字的和都是合数。
#include<iostream>
#include<cstring>
#include <stdio.h>
using namespace std;
bool prime[10100];
int list[1001];
bool used[1001];
int n,m,p;
void primex()//将[2,10100)中的所有质数标记为0,合数标记为1.
{
for(int i=2;i<10100;i++)
{
if(prime[i]==0)
{
for(int j=i+i;j<10100;j=j+i)
{
prime[j]=1;
}
}
}
}
bool check(int k)//check list[k]加上前面的数字的和是否为合数(p,p-1,p-2....1):题目要求这几个和都为合数
{
int num=list[k];
int po=k-1;
int flag=p-1;
while(flag>0)
{
num+=list[po--];
flag--;
if(prime[num]==0) return false;
}
return true;
}
bool listed(int k)//此处使用了递归。(最开先k==1.)
{
if(k==n-m+2)//k是list下标,从1开始。
{
for(int i=1;i<n-m+1;i++)
cout<<list[i]<<",";
cout<<list[n-m+1]<<endl;
return true;
}
for(int i=m;i<=n;i++)//这个循环遍历n到m间的数字,看是否符合要求,若符合要求则填入list数组。
{
if(used[i]==0)//要求i是未使用过的数字
{
list[k]=i;//现暂时将i填入list
if(k>=2)
{
if(check(k)==0)//检查i是否符合要求,不符合则跳过,检测下一个i
continue;
}
used[i]=1;//若i符合要求,则将used[i]=1,表示已经使用过。
if (listed(k+1)) //按照同样的方法排list[k+1].如果k+1排列成功,则返回true。
return true;//若排列list[k+1]失败,那么就是list[k]排列错误。则撤销list[k]的排列。将used[i]=0.
else used[i]=0;//这种情况是有可能发生的。当有多个i都符合check条件的时候,就会发生前面排列错误的情况。
}
}
return false;//如果经过上面的循环排列,还是没有return。那么就不存在反质数数列了。
}
int main()
{
primex();
while(cin>>m>>n>>p)
{
if(m==0&&n==0&&p==0) return 0;
memset(used,0,1001);
if(!listed(1))
cout<<"No anti-prime sequence exists.\n";
}
return 0;
}