Anti-prime Sequences
Time Limit: 3000MS   Memory Limit: 30000K
Total Submissions: 3355   Accepted: 1531

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
题意:在【2,d】长度的连续序列的和都要为合数。
思路:DFS。
 1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<stdlib.h>
5 #include<string.h>
6 #include<queue>
7 #include<stack>
8 #include<math.h>
9 using namespace std;
10 typedef long long LL;
11 bool prime[20000]= {0};
12 int tt[10000];
13 bool cm[1005];
14 int ts=0;
15 bool check(int n,int m);
16 int dfs(int n,int m,int d,int kk,int pp);
17 int main(void)
18 {
19 int i,j,k;
20 for(i=2; i<=1000; i++)
21 {
22 if(!prime[i])
23 {
24 for(j=i; (i*j)<=20000; j++)
25 {
26 prime[i*j]=true;
27 }
28 }
29 }
30 int n,m;
31 while(scanf("%d %d %d",&n,&m,&k),n!=0&&m!=0&&k!=0)
32 {
33 memset(cm,0,sizeof(cm));
34 ts=0;
35 int uu=dfs(0,m-n+1,k,n,m);
36 if(uu)
37 {
38 printf("%d",tt[0]);
39 for(i=1; i<(m-n+1); i++)
40 {
41 printf(",%d",tt[i]);
42 }
43 printf("\n");
44 }
45 else printf("No anti-prime sequence exists.\n");
46 }
47 }
48 bool check(int n,int m)
49 {
50 int i,j;
51
52
53 LL sum=tt[m];
54 for(i=m-1; i>=max(n,0); i--)
55 {
56 sum+=tt[i];
57 if(!prime[sum])
58 return false;
59 }
60 return true;
61 }
62 int dfs(int n,int m,int d,int kk,int pp)
63 {
64 int i;
65 if(ts)return 1;
66 if(n==m)
67 {
68
69 bool cc=check(n-d,m-1);
70 if(!cc)
71 {
72 return 0;
73 }
74 ts=1;
75 return 1;
76 }
77 else
78 {
79 bool cc=check(n-d,n-1);
80 if(cc)
81 {
82 for(i=kk; i<=pp; i++)
83 {
84 if(ts)return 1;
85 if(!cm[i])
86 {
87 tt[n]=i;
88 cm[i]=true;
89 int uu=dfs(n+1,m,d,kk,pp);
90 cm[i]=false;
91 if(uu)return 1;
92 }
93 }
94 }
95 else return 0;
96 }
97 return 0;
98 }

Anti-prime Sequences的更多相关文章

  1. Who Gets the Most Candies?(线段树 + 反素数 )

    Who Gets the Most Candies? Time Limit:5000MS     Memory Limit:131072KB     64bit IO Format:%I64d &am ...

  2. (Problem 49)Prime permutations

    The arithmetic sequence, 1487, 4817, 8147, in which each of the terms increases by 3330, is unusual ...

  3. DFS(8)——poj2034Anti-prime Sequences

    一.题目回顾 题目链接:Anti-prime Sequences Sample Input 1 10 2 1 10 3 1 10 5 40 60 7 0 0 0   Sample Output 1,3 ...

  4. 河南省第十届省赛 Binary to Prime

    题目描述: To facilitate the analysis of  a DNA sequence,  a DNA sequence is represented by a binary  num ...

  5. Farey sequences

    n阶的法里数列是0和1之间最简分数的数列,由小至大排列,每个分数的分母不大于n. Stern-Brocot树(SB Tree)可以生成这个序列 {0/1,1/1} {0/1,1/2,1/1} {0/1 ...

  6. Java 素数 prime numbers-LeetCode 204

    Description: Count the number of prime numbers less than a non-negative number, n click to show more ...

  7. Prime Generator

    Peter wants to generate some prime numbers for his cryptosystem. Help him! Your task is to generate ...

  8. ABP Zero示例项目登录报错“Empty or invalid anti forgery header token.”问题解决

    ABP Zero项目,登录时出现如图"Empty or invalid anti forgery header token."错误提示的解决方法: 在 WebModule.cs的P ...

  9. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

随机推荐

  1. 计算机网络-4-7-内部网关协议OSPF

    内部网关协议OSPF(开放最短路径优先) 出现的原因:为了克服RIP协议的缺点在1989年开发出来,**开放 表明OSPF协议不受任何厂家的限制.最短路径优先是因为使用了最短路径算法SPF**. OS ...

  2. javaSE高级篇4 — 反射机制( 含类加载器 ) — 更新完毕

    反射机制 1.反射机制是什么?----英文单词是:reflect.在java.lang包下---这才是java最牛逼的技术 首先提前知道一句话----在java中,有了对象,于是有了类,那么有了类之后 ...

  3. 大数据学习day11------hbase_day01----1. zk的监控机制,2动态感知服务上下线案例 3.HDFS-HA的高可用基本的工作原理 4. HDFS-HA的配置详解 5. HBASE(简介,安装,shell客户端,java客户端)

    1. ZK的监控机制 1.1 监听数据的变化  (1)监听一次 public class ChangeDataWacher { public static void main(String[] arg ...

  4. 零基础学习java------20---------反射

    1. 反射和动态代理 参考博文:https://blog.csdn.net/sinat_38259539/article/details/71799078 1.0 什么是Class: 我们都知道,对象 ...

  5. 【bfs】洛谷 P1443 马的遍历

    题目:P1443 马的遍历 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 记录一下第一道ac的bfs,原理是利用队列queue记录下一层的所有点,然后一层一层遍历: 其中: 1.p ...

  6. Linux基础命令---ftp

    ftp ftp指令可以用来登录远程ftp服务器. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.SUSE.openSUSE.Fedora.   1.语法       ftp [ ...

  7. VFL

    VFL 1. 概念 VFL全称是Visual Format Language,翻译过来是"可视化格式语言" VFL是苹果公司为了简化Autolayout的编码而推出的抽象语言 2. ...

  8. Thymeleaf标准表达式

    Thymeleaf的官网为: http://www.thymeleaf.org/ 一.变量表达式${-} 使用${-}括起来的表达式,称为变量表达式.该表达式的内容会显示在HTML标签体文本处. 该表 ...

  9. jQuery节点更新

    一.插入子节点 var $newNode1 = $("<p>我是p标签</p>"); 加入之后,原来的会删除. 二.插入兄弟节点 三.替换节点 1.HTML ...

  10. 团队协作项目——SVN的使用

    参考文献:https://www.cnblogs.com/rwh871212/p/6955489.html 老师接了一个新项目,需要团队共同完成开发任务,因此需要SVN.SVN是C/S架构: 1.服务 ...