Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
半年前ac过这题,重做,居然被卡了2小时#include<stdio.h> #include<string.h> #include<iostream> using namespace std; const int maxn = 1005; int n,m,d; bool success,vis[maxn],tb[maxn*10]; int x[maxn]; void init(){ success = 0; memset(vis,0,sizeof(vis)); } void get_prime_table(){ for (int i = 2; i < maxn*10; i++) for (int j = 2; i*j < maxn*10; j++) tb[i*j] = 1; } void dfs(int t){ if(t==m+1){ success = 1; return; } for(int i=n;i<=m;++i){ if(!vis[i]){ if(t>n){ int sum = i; int bit = t-1; while(bit>=n&&bit>t-d&&tb[sum+=x[bit]])--bit; if(bit!=n-1&&bit!=t-d)continue; } x[t] = i; vis[i] = 1; dfs(t+1); if(success)return; vis[i] = 0; } } } int main(){ memset(tb,0,sizeof(tb)); get_prime_table(); while(cin>>n>>m>>d&&(n||m||d)){ init(); dfs(n); if(success){ for(int i=n;i<m;++i){ // printf("%d,",x[i]); cout<<x[i]<<','; } // printf("%d\n",x[m]); cout<<x[m]<<endl; }else{ printf("No anti-prime sequence exists.\n"); } } return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator