Poj 2034 Anti-prime Sequences (dfs), pojanti-prime
Http://poj.org/problem? Id = 2034
In this example, [n, m] is given, and the numbers of these intervals are arranged so that the sum of two and three adjacent numbers... d is not a prime number. The smallest Lexicographic Order is output.
Idea: bare dfs. TLE has been used for numerous times because the range of prime numbers in a table is too small. The maximum value should be 10000.
#include <stdio.h>#include <iostream>#include <map>#include <stack>#include <vector>#include <math.h>#include <string.h>#include <queue>#include <string>#include <stdlib.h>#include <algorithm>#define LL long long#define _LL __int64#define eps 1e-8#define PI acos(-1.0)using namespace std;const int maxn = 10010;bool prime[maxn];int vis[1010],ans[1010];int n,m,d;int ok;void init(){ memset(prime,true,sizeof(prime)); prime[0] = prime[1] = false; for(int i = 2; i <= 10000; i++) { if(prime[i] == true) { for(int j = i*i; j <= 10000; j += i) prime[j] = false; } }}void dfs(int dep){if(ok == 1)return;if(dep == m-n+2){ok = 1;for(int i = 1; i < dep-1; i++)printf("%d,",ans[i]);printf("%d\n",ans[dep-1]);return;}for(int i = n; i <= m; i++){if(!vis[i]){bool flag = 0;for(int k = 2; k <= d && dep-k>=0; k++){int sum = 0;for(int j = dep-k+1; j <= dep-1; j++)sum += ans[j];if(prime[sum + i] == true)flag = 1;}if(flag == 1)continue;ans[dep] = i;vis[i] = 1;dfs(dep+1);vis[i] = 0;}}}int main(){ init(); while(~scanf("%d %d %d",&n,&m,&d)) { if(n == 0 && m == 0 && d == 0) break; memset(vis,0,sizeof(vis)); ok = 0; dfs(1); if(ok == 0)printf("No anti-prime sequence exists.\n"); } return 0;}