[Swust OJ 799]--Superprime Rib(DFS)
题目链接:http://acm.swust.edu.cn/problem/799/
Description
Butchering Farmer John's cows always yields the best prime rib. You can tell prime ribs by looking at the digits lovingly stamped across them, one by one, by FJ and the USDA. Farmer John ensures that a purchaser of his prime ribs gets really prime ribs because when sliced from the right, the numbers on the ribs continue to stay prime right down to the last rib, e.g.:
7 3 3 1
The set of ribs denoted by 7331 is prime; the three ribs 733 are prime; the two ribs 73 are prime, and, of course, the last rib, 7, is prime. The number 7331 is called a superprime of length 4.
Write a program that accepts a number N 1 <=N<=8 of ribs and prints all the superprimes of that length.
The number 1 (by itself) is not a prime number.
A single line with the number N.
The superprime ribs of length N, printed in ascending order one per line.
Sample Input
| 4 |
Sample Output
|
2333
2339
2393
2399
2939
3119
3137
3733
3739
3793
3797
5939
7193
7331
7333
7393
|
题目大意:农民约翰的母牛总是生产出最好的肋骨。你能通过农民约翰和美国农业部标记在每根肋骨上的数字认出它们。
农民约翰确定他卖给买方的是真正的质数肋骨, 是因为从右边开始切下肋骨, 每次还剩下的肋骨上的数字都组成一个质数,
举例来说:7331,全部肋骨上的数字 7331是质数; 三根肋骨 733是质数; 二根肋骨 73 是质数; 当然, 最后一根肋骨 7 也是质数。
7331 被叫做长度 4 的特殊质数。
写一个程序对给定的肋骨的数目 N(1 <= N <= 8), 求出所有的特殊质数。数字1不被看作一个质数。
解题思路:由于分割了每一位都是素数,那么首字母只能是2 3 5 7 dfs搜索即可,我打素数表存储素数,居然Runtime Error也是够了
就原始方法判断素数吧Orz~~~
代码如下:
#include <iostream>
#include <cmath>
using namespace std; int n;
//#define maxn 100000010
//int n, prime[maxn] = { 1, 1, 0 };
//void init(){
// int i, j;
// for (i = 2; i <= maxn; i++){
// if (!prime[i]){
// for (j = 2; i*j <= maxn; j++)
// prime[i*j] = 1;
// }
// }
//}
bool isprime(int num)//判断质数
{
if (num == )return true;
if (num & ){
int n = (int)sqrt(num);
for (int i = ; i <= n; i += )
if (!(num%i))
return false;
return true;
}
return false;
}
void dfs(int num, int limit){
int ans;
if (n == limit){
cout << num << endl;
return;
}
for (int i = ; i <= ; i += ){
ans = num * + i;
if (isprime(ans))//if (!prime[ans])
dfs(ans, limit + );
}
}
int main()
{
//init();
cin >> n;
dfs(, );
dfs(, );
dfs(, );
dfs(, );
return ;
}
[Swust OJ 799]--Superprime Rib(DFS)的更多相关文章
- 洛谷P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib
P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib 284通过 425提交 题目提供者该用户不存在 标签USACO 难度普及- 提交 讨论 题解 最新讨论 超时怎么办? ...
- USACO 1.5 Superprime Rib
Superprime Rib Butchering Farmer John's cows always yields the best prime rib. You can tell prime ri ...
- 洛谷 P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib
P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib 题目描述 农民约翰的母牛总是产生最好的肋骨.你能通过农民约翰和美国农业部标记在每根肋骨上的数字认出它们.农民约翰确定他卖给 ...
- 洛谷P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib 使用四种算法
洛谷P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib 水题一道…… 题目描述 农民约翰的母牛总是产生最好的肋骨.你能通过农民约翰和美国农业部标记在每根肋骨上的数字认出它们. ...
- [Swust OJ 404]--最小代价树(动态规划)
题目链接:http://acm.swust.edu.cn/problem/code/745255/ Time limit(ms): 1000 Memory limit(kb): 65535 Des ...
- [Swust OJ 649]--NBA Finals(dp,后台略(hen)坑)
题目链接:http://acm.swust.edu.cn/problem/649/ Time limit(ms): 1000 Memory limit(kb): 65535 Consider two ...
- USACO Superprime Rib
洛谷 P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib 洛谷传送门 JDOJ 1673: Superprime Rib JDOJ传送门 题目描述 农民约翰的母牛总是产生最好 ...
- SWUST OJ NBA Finals(0649)
NBA Finals(0649) Time limit(ms): 1000 Memory limit(kb): 65535 Submission: 404 Accepted: 128 Descri ...
- [Swust OJ 465]--吴奶奶买鱼(0-1背包+dfs)
题目链接:http://acm.swust.edu.cn/problem/465/ 还有一道题只是描述不一样,方法一模一样(http://acm.swust.edu.cn/problem/644/) ...
随机推荐
- 富文本编辑器ckeditor继承
新建一个web项目ckfinder,导入lib包 加入java包,编码格式UTF-8 在WebRoot下添加ckedtior以及ckfinder两个文件夹,将config.xml拷入WEB-INF中 ...
- Vue.jsbrowserify项目模板
Vue.js——60分钟browserify项目模板快速入门 概述 在之前的一系列vue.js文章,我们都是用传统模式引用vue.js以及其他的js文件的,这在开发时会产生一些问题. 首先,这限定 ...
- 服务启动项 Start类型详解
注册表的服务启动项 Start类型详解 HKLM\SYSTEM\CurrentControlSet\services\ 下的服务项.不论有没有在services.msc服务管理控制台中显示,在注册表中 ...
- MySQL 关闭子表的外键约束检察
准备: 定义一个教师表.一个学生表:在学生表中引用教师表ID create table teachers(teacherID int not null auto_increment primary k ...
- JAVA排序(一) Comparable接口
昨天接到一个实习公司的电话面试,来的很突然,没有准备. 由于以前没用过,在被他问及是否用过JAVA的排序工具Comparable与Comparator时,没有回答上来,只能实话实说没有用过. 感觉太丢 ...
- Database(Mysql)发版控制二
author:skate time:2014/08/18 Database(Mysql)发版控制 The Liquibase Tool related Database 一.Installation ...
- uploadify 使用 详细 说明
简单 常用的 时间 有点 紧 先写 这点 , 有时间 在更新 下面有 例子 链接 不过要 自己 摘 一下 很简单的啦 <script type="text/javascrip ...
- BZOJAC400题留念
BZOJAC400题了...
- C#中网站根路径、应用根路径、物理路径、绝对路径,虚拟路径的区别
C#中网站根路径,请站点的最外一层 /表示 应用根路径 ~/表示,有时候C#程序路径并不是网站路径 物理路径 server.mappath("~/") 是指应用程序放在服务器硬盘的 ...
- windows 下提取目录下所有文件的文件名
tree D:/dir /f >D:/filenames.txt 提取D盘dir目录下所有文件名,写入文件filenames.txt