//相邻的 2.3......d 之和都要不为素数
# include <algorithm>
# include <stdio.h>
using namespace std;
int num[1010],vis[1010];
int n,m,d,cot;
int flag[10010];
void init()//素数打表
{
int i,j;
for(i=2;i<10010;i++)
{
if(!flag[i])
for(j=i*i;j<10010;j=j+i)
flag[j]=true;
}
flag[1]=true;
flag[0]=true;
}
bool judge()
{
int sum,i,k;
sum=num[cot-1];
if(cot-d>=0)
k=cot-d;
else
k=0;
for(i=cot-2;i>=k;i--)
{
sum=sum+num[i];
if(!flag[sum])//为素数
return true;
}
return false;
}
bool dfs()
{
int i;
if(cot>=2)
{
if(judge())
return false;
}
if(cot==m-n+1)
return true;
for(i=n;i<=m;i++)
{
if(!vis[i])
{
num[cot++]=i;
vis[i]=true;
if(dfs())
return true;
vis[i]=false;
cot--;
}
}
return false;
}
int main()
{
int i;
init();//初推断是否为素数
while(~scanf("%d%d%d",&n,&m,&d),n+m+d)
{
memset(vis,0,sizeof(vis));
memset(num,0,sizeof(num));
cot=0;
if(dfs())
{
for(i=0;i<cot;i++)
{
if(i==cot-1)
printf("%d\n",num[i]);
else
printf("%d,",num[i]);
}
}
else
printf("No anti-prime sequence exists.\n");
}
return 0;
}

poj 2034 Anti-prime Sequences(dfs)的更多相关文章

  1. POJ 1321-棋盘问题(DFS 递归)

    POJ 1321-棋盘问题 K - DFS Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I6 ...

  2. 【POJ】1811 Prime Test

    http://poj.org/problem?id=1811 题意:求n最小素因子.(n<=2^54) #include <cstdio> #include <cstring& ...

  3. POJ 3321 Apple Tree(DFS序+线段树单点修改区间查询)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 25904   Accepted: 7682 Descr ...

  4. POJ 1979 Red and Black dfs 难度:0

    http://poj.org/problem?id=1979 #include <cstdio> #include <cstring> using namespace std; ...

  5. POJ 2378 Tree Cutting (DFS)

    题目链接:http://poj.org/problem?id=2378 一棵树,去掉一个点剩下的每棵子树节点数不超过n/2.问有哪些这样的点,并按照顺序输出. dfs回溯即可. //#pragma c ...

  6. poj 1659 Frogs' Neighborhood (DFS)

    http://poj.org/problem?id=1659 Frogs' Neighborhood Time Limit: 5000MS   Memory Limit: 10000K Total S ...

  7. poj 2531 Network Saboteur( dfs )

    题目:http://poj.org/problem?id=2531 题意:一个矩阵,分成两个集合,求最大的 阻碍量 改的 一位大神的代码,比较简洁 #include<stdio.h> #i ...

  8. poj 3009 Curling 2.0( dfs )

    题目:http://poj.org/problem?id=3009 参考博客:http://www.cnblogs.com/LK1994/ #include <iostream> #inc ...

  9. POJ 1129 Channel Allocation 四色定理dfs

    题目: http://poj.org/problem?id=1129 开始没读懂题,看discuss的做法,都是循环枚举的,很麻烦.然后我就决定dfs,调试了半天终于0ms A了. #include ...

随机推荐

  1. Linux学习(十九)软件安装与卸载(二)更换yum源

    一.简介 系统自带的源数量有限,而且是国外的源,速度肯定不如国内的.而断网的时候,本地源就可以派得上用处.而RPMForge源是传说中规模最大的一个源.那么接下来我们就来分别配一下本地源,国内源,RP ...

  2. 学习笔记TF061:分布式TensorFlow,分布式原理、最佳实践

    分布式TensorFlow由高性能gRPC库底层技术支持.Martin Abadi.Ashish Agarwal.Paul Barham论文<TensorFlow:Large-Scale Mac ...

  3. 28.Linux-IIC驱动(详解)

    上一节 我们学习了: IIC接口下的24C02 驱动分析: http://www.cnblogs.com/lifexy/p/7793686.html 接下来本节, 学习Linux下如何利用linux下 ...

  4. "use strict"详解

    参考:http://www.ruanyifeng.com/blog/2013/01/javascript_strict_mode.html 目的: 消除JavaScript语法的一些不合理.不严谨之处 ...

  5. linq中group by

    本文导读:LINQ定义了大约40个查询操作符,如select.from.in.where.group 以及order by,借助于LINQ技术,我们可以使用一种类似SQL的语法来查询任何形式的数据.L ...

  6. [转载] linux中文件描述符fd和文件指针flip的理解

    转载自http://www.cnblogs.com/Jezze/archive/2011/12/23/2299861.html 简单归纳:fd只是一个整数,在open时产生.起到一个索引的作用,进程通 ...

  7. curl安装

    问题1: curl: error while loading shared libraries: libcurl.so.4: cannot open shared object file: No su ...

  8. 最全Jenkins+SVN+iOS+cocoapods环境搭建及其错误汇总

    前言 持续集成是敏捷开发中重要的一部分,为保证新功能的开发,又保证旧功能的维护,从一个冲刺到下个冲刺.持续集成工具是我们保证开发和维护并行的护航者,现在流行的集成工具有很多,例如: 1.Jenkins ...

  9. Python环境变量搭建

    1.首先下载相对应的Python版本,安装后在系统环境变量的path路径下加入安装的默认路径: 2.测试:dos命令下输入python.回车,然后测试,exit()退出来,测试完成.

  10. Android WebView 上传各种文件(包括拍照 录像 录音 文件 音乐 等,用到图片或拍照的,可以参考下)

    我也是从网上扒下来的,经过多次实验,找到了个好用的.网上能搜到最多的也就是这个解决方案,我英文不好,也没仔细研究,但大多数都是出自这: http://stackoverflow.com/questio ...