poj 2034 Anti-prime Sequences(dfs)
//相邻的 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)的更多相关文章
- POJ 1321-棋盘问题(DFS 递归)
POJ 1321-棋盘问题 K - DFS Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I6 ...
- 【POJ】1811 Prime Test
http://poj.org/problem?id=1811 题意:求n最小素因子.(n<=2^54) #include <cstdio> #include <cstring& ...
- POJ 3321 Apple Tree(DFS序+线段树单点修改区间查询)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 25904 Accepted: 7682 Descr ...
- POJ 1979 Red and Black dfs 难度:0
http://poj.org/problem?id=1979 #include <cstdio> #include <cstring> using namespace std; ...
- POJ 2378 Tree Cutting (DFS)
题目链接:http://poj.org/problem?id=2378 一棵树,去掉一个点剩下的每棵子树节点数不超过n/2.问有哪些这样的点,并按照顺序输出. dfs回溯即可. //#pragma c ...
- poj 1659 Frogs' Neighborhood (DFS)
http://poj.org/problem?id=1659 Frogs' Neighborhood Time Limit: 5000MS Memory Limit: 10000K Total S ...
- poj 2531 Network Saboteur( dfs )
题目:http://poj.org/problem?id=2531 题意:一个矩阵,分成两个集合,求最大的 阻碍量 改的 一位大神的代码,比较简洁 #include<stdio.h> #i ...
- poj 3009 Curling 2.0( dfs )
题目:http://poj.org/problem?id=3009 参考博客:http://www.cnblogs.com/LK1994/ #include <iostream> #inc ...
- POJ 1129 Channel Allocation 四色定理dfs
题目: http://poj.org/problem?id=1129 开始没读懂题,看discuss的做法,都是循环枚举的,很麻烦.然后我就决定dfs,调试了半天终于0ms A了. #include ...
随机推荐
- 必须先将 ContentLength 字节写入请求流,然后再调用 [Begin]GetResponse。解决方法
当在后台实现POST请求的时候,出现如下错误: 必须先将 ContentLength 字节写入请求流,然后再调用 [Begin]GetResponse. 或者是如下错误: 上述是因为由于我们使用的是代 ...
- 几种移动app API调用认证方案浅析
最近做的金融项目,app调用的接口需要做一个身份认证,所以找了下目前API services验证的几种方式.之前翻译的一篇文章--[译]移动API安全终极指南中,主要提出了API服务调用验证的问题,通 ...
- Spring IOC容器分析(3) -- DefaultListableBeanFactory
上一节介绍了封装bean对象的BeanDefinition接口.从前面小结对BeanFactory的介绍中,我们知道bean对象是存储在map中,通过调用getBean方法可以得到bean对象.在接口 ...
- ab使用命令
ab使用-A auth-username:password 向服务器提供基本认证信息.用户名和密码之间":"分割,以base64编码形式发送.无论服务器是否需要(即是否发送了 ...
- CentOS配置上网
CentOS设置: 进入CentOS命令模式: Centos7更改默认启动桌面(或命令行)模式 vi /etc/inittab:查看启动文件,在该文件中存在两种方式: multi-user.tar ...
- MySQL原理相关
1.索引 http://blog.codinglabs.org/articles/theory-of-mysql-index.html
- shell的EOF用法
将命令输出的结果给一个循环处理,常用的方式如下: [root@etch171 guosong]# ls |while read line;do echo $line;done processlist ...
- python win32 简单操作
源由 刚开始是帮朋友做一个按键精灵操作旺信的脚本,写完后各种不稳定:后来看到python可以操作win32相关的api,恰好这一段时间正在学习python,感觉练手的时候到了~~~ 下载 要注意Pyt ...
- WebWorker实战使用
总体来说webworker解决了阻塞主线程问题,但是还没解决高性能计算的问题 WebWorker整体介绍 https://developer.mozilla.org/zh-CN/docs/Web/AP ...
- Android’s HTTP Clients (httpClient 和 httpURLConnect 区别)
来源自:http://android-developers.blogspot.jp/2011/09/androids-http-clients.html Most network-connected ...