标签:val ide proc std phi space lin sam pre
Input
Output
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
需要一个 l 到 r 的排列,使得任意一个长度是2,3,...,k的子串当中子串和都不是质数
直接搜就是了
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #define LL long long 5 using namespace std; 6 inline LL read() 7 { 8 LL x=0,f=1;char ch=getchar(); 9 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} 10 while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} 11 return x*f; 12 } 13 int l,r,d,haveans=0; 14 bool mk[100010]; 15 bool mrk[100010]; 16 int s[100010]; 17 int p[100010],len; 18 inline void getp() 19 { 20 for (int i=2;i<=10000;i++) 21 { 22 if (!mk[i]) 23 { 24 p[++len]=i; 25 for (int j=2*i;j<=10000;j+=i)mk[j]=1; 26 } 27 } 28 } 29 inline void dfs(int now) 30 { 31 if (now==r+1) 32 { 33 for (int i=l;i<r;i++)printf("%d,",s[i]); 34 printf("%d\n",s[r]); 35 haveans=1; 36 return; 37 } 38 for (int i=l;i<=r;i++) 39 { 40 if (mrk[i])continue; 41 int sum=i,mrk2=1; 42 for (int j=now-1;j>=max(l,now-d+1);j--) 43 { 44 sum+=s[j]; 45 if(!mk[sum]){mrk2=0;break;} 46 } 47 if (!mrk2)continue; 48 mrk[i]=1; 49 s[now]=i; 50 dfs(now+1); 51 if (haveans)return; 52 s[now]=0; 53 mrk[i]=0; 54 } 55 } 56 int main() 57 { 58 getp(); 59 while (~scanf("%d%d%d",&l,&r,&d)&&l+r+d) 60 { 61 memset(mrk,0,sizeof(mrk)); 62 haveans=0; 63 dfs(l); 64 if (!haveans)puts("No anti-prime sequence exists."); 65 } 66 }
[暑假集训--数论]poj2034 Anti-prime Sequences
标签:val ide proc std phi space lin sam pre
原文地址:http://www.cnblogs.com/zhber/p/7285462.html