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
題目分析:剛看到這個題目時,就想按照排列演算法,把所有情況遍曆一遍,找到滿足條件就停止遍曆。但是逾時超的嚴重,特別是在沒有序列的情況下。最佳化了一天還是逾時,最後求助於網上的大神http://blog.csdn.net/ChinaCzy/article/details/5673174
原來這個題目是用深度優先搜尋做,仔細想了想,的確完美。裡面有一個“卡時”的概念,雖然沒有證明出來本題利用卡時的正確,但還是結果還是正確了。下面的代碼是利用人家的思想,默寫了一遍,應該算是大神的代碼:
#include<iostream>#include<fstream>#include<vector>#include<string>#include<algorithm>#include<cmath>#include<string.h>using namespace std;const long size=10000;bool com[size];bool used[1010];int x[1010];bool ok;int n,m,d,t;void initComposite(){//質數是falseint tmp=sqrt((double)size);for(long i=2;i<=tmp;i++){if(com[i]==true)continue;for(long j=i;i*j<size;j++)com[i*j]=true;}}void print(){for(int i=0;i<m-n;i++)cout<<x[i]<<',';cout<<x[m-n]<<endl;}bool check(int deep){if(deep<2) return true;int tmpD=2;int i,flag=0;while(tmpD<=d){for(i=0;i<=deep-tmpD;i++){int sum=0;vector<int>::size_type j;for(j=0;j<tmpD;j++){sum+=x[i+j];}//end forif(!com[sum]){flag=1;break;}//end if}//end fortmpD++;if(flag==1)return false;}return true;}void dfs(int deep){if(++t>4000)return ;if(ok)return ;if(!check(deep))return ;if(deep==m-n+1){ok=true;return;}for(int i=n;i<=m&&!ok;++i){if(used[i]) continue;used[i]=true;x[deep]=i;dfs(deep+1);used[i]=false;}//end for}int main(){initComposite();while(cin>>n>>m>>d && n!=0){ok=false;t=0;memset(used,0,sizeof(used));memset(x,0,sizeof(x));dfs(0);if(ok)print();else cout<<"No anti-prime sequence exists."<<endl;}//end while}