深搜,添加数字后如果仍为质数,继续递归。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <cassert>
#include <stack>
#define mkp make_pair
using namespace std;
const double EPS=1e-;
typedef long long lon;
const lon SZ=,INF=0x7FFFFFFF;
vector<int> pri,res;
bool isp[SZ]; bool isprime(int x)
{
for(int i=;i<pri.size();++i)
{
if(x!=pri[i]&&x%pri[i]==)return ;
}
return ;
} void dfs(int x,int n,int val)
{
if(x==n)
{
res.push_back(val);
return;
}
val*=;
for(int i=;i<;i+=)
{
val+=i;
if(isprime(val))
{
dfs(x+,n,val);
}
val-=i;
}
} void init()
{
memset(isp,,sizeof(isp));
isp[]=;
for(int i=;i*i<SZ;++i)
{
if(isp[i])
for(int j=i*i;j<SZ;j+=i)
{
isp[j]=;
}
}
for(int i=;i<SZ;++i)
{
if(isp[i])
{
pri.push_back(i);
}
}
} void work(int n)
{
init();
for(int i=;i<;++i)
{
dfs(,n,pri[i]);
}
} int main()
{
std::ios::sync_with_stdio();
//freopen("d:\\1.txt","r",stdin);
lon casenum;
//cin>>casenum;
//for(lon time=1;time<=casenum;++time)
{
int n;
cin>>n;
work(n);
for(int i=;i<res.size();++i)
{
cout<<res[i]<<endl;
}
}
return ;
}

p1218 Superprime Rib的更多相关文章

  1. 洛谷P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib

    P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib 284通过 425提交 题目提供者该用户不存在 标签USACO 难度普及- 提交  讨论  题解 最新讨论 超时怎么办? ...

  2. 洛谷 P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib

    P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib 题目描述 农民约翰的母牛总是产生最好的肋骨.你能通过农民约翰和美国农业部标记在每根肋骨上的数字认出它们.农民约翰确定他卖给 ...

  3. 洛谷P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib 使用四种算法

    洛谷P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib 水题一道…… 题目描述 农民约翰的母牛总是产生最好的肋骨.你能通过农民约翰和美国农业部标记在每根肋骨上的数字认出它们. ...

  4. USACO Superprime Rib

    洛谷 P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib 洛谷传送门 JDOJ 1673: Superprime Rib JDOJ传送门 题目描述 农民约翰的母牛总是产生最好 ...

  5. USACO 1.5 Superprime Rib

    Superprime Rib Butchering Farmer John's cows always yields the best prime rib. You can tell prime ri ...

  6. P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib (数论—素数 + DFS)

    这大概是我写的第一个DFS 题目描述 农民约翰的母牛总是产生最好的肋骨.你能通过农民约翰和美国农业部标记在每根肋骨上的数字认出它们.农民约翰确定他卖给买方的是真正的质数肋骨,是因为从右边开始切下肋骨, ...

  7. P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib

    题目描述 农民约翰的母牛总是产生最好的肋骨.你能通过农民约翰和美国农业部标记在每根肋骨上的数字认出它们.农民约翰确定他卖给买方的是真正的质数肋骨,是因为从右边开始切下肋骨,每次还剩下的肋骨上的数字都组 ...

  8. [Swust OJ 799]--Superprime Rib(DFS)

    题目链接:http://acm.swust.edu.cn/problem/799/ Time limit(ms): 1000 Memory limit(kb): 10000   Description ...

  9. 【USACO 1.5】SuperPrime Rib

    /* TASK: sprime LANG: C++ SOLVE: dfs,后面每增加一位,判断当前是否为素数. 第一位不能为0 */ #include<cstdio> int n; voi ...

随机推荐

  1. python --- 19 判断对象所属,区分函数和对象, 反射

    一.判断对象所属 isinstance, type , issubclass 1.issubclass(x,y)    判断x是否是y 的子类 2.type(x)  精准返回x 的数据类型 3.isi ...

  2. Class DesiredCapabilities

    public class DesiredCapabilities extends java.lang.Object implements java.io.Serializable, Capabilit ...

  3. imx6ul linux4.1.15 LED驱动配置及heartbeat源码分析【转】

    本文转载自:https://blog.csdn.net/u010444107/article/details/78328807 1)查看内核配置wujun@wj-vBox:~/freescale/li ...

  4. tp剩余未验证内容-5

    经过实践, ie678是不能正确显示解析bs的,所以要用ff和chrome浏览器. page-header类是有特殊样式的 在标题下有一条浅色的细线条,源代码中有: border-bottom: 1p ...

  5. word如何替换行首?

    在替换窗口, 要使用通配符 要使用 替换中的 分组, 高级替换 表示行首的符号, 使用 (<*) 表示 单词开始的位置, 使用 <, 表示单词结束的位置, 使用 > 替换的示例:

  6. bzoj 1735: [Usaco2005 jan]Muddy Fields 泥泞的牧场 最小点覆盖

    链接 1735: [Usaco2005 jan]Muddy Fields 泥泞的牧场 思路 这就是个上一篇的稍微麻烦版(是变脸版,其实没麻烦) 用边长为1的模板覆盖地图上的没有长草的土地,不能覆盖草地 ...

  7. 常用for循环和for in 以及for of 的区别

    用Es6对象扩展运算符(…)与rest运算符说明 function test(first,...a){ for(let val=0; val<a.length;val++){ console.l ...

  8. JavaScript基本内容

    注释: /*多行 注释*/ //单行注释 变量: //变量均为对象,常用类型:String.Number.Boolean.Array.Object var value = "hello&qu ...

  9. HTTP Status 404 - No result defined for action com.ouyang.action.GreetingAction and result success 错误解决办法

    1.原来设置的包声明: <package name="myPackage" extends="struts-default"> <!-- 定义 ...

  10. Jedis使用

    Jedis是java调用Redis的接口. 一.Maven中的jedis依赖包 <!-- https://mvnrepository.com/artifact/redis.clients/jed ...