[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/) ...
随机推荐
- lombk在IDEA中报ClassNotFoundException错误
今天接手了一个项目,用到了lombk,第一次用到,做为纯JAVA来说,确实不错. 不过在使用中碰到了一个问题,就是在IDEA中,可以在结构中看到getter和setter等都已经正确的生成了,但是运行 ...
- mock.js 使用教程
mock.js 数据模板 #查看模板生成的数据 var data = Mock.mock({ //list|1-10 数组元素个数随机范围, id|+2 属性值递增, age|20-30数值随机范围 ...
- Qt 如何处理密集型耗时的事情(频繁调用QApplication::processEvents)
有时候需要处理一些跟界面无关的但非常耗时的事情,这些事情跟界面在同一个线程中,由于时间太长,导致界面无法响应,处于“假死”状态.例如:在应用程序中保存文件到硬盘上,从开始保存直到文件保存完毕,程序不响 ...
- android studio 实现代码混淆
=======本文章属于转载==========原文章地址:http://my.oschina.net/aibenben/blog/370985 这篇文章等是跟大家分享一在Android studio ...
- Windows Azure 新上线网络相关服务
动态路由网关.点到站点(Point to Site)VPN正式商用 动态路由网关和点到站点VPN支持基于路由的VPN,并且允许用户将独立计算机连接到Azure上的虚拟网络.现在,虚拟网络中的动态 ...
- 如何解决Android 5.0中出现的警告:Service Intent must be explicit
有些时候我们使用Service的时需要采用隐私启动的方式,但是Android 5.0一出来后,其中有个特性就是Service Intent must be explitict,也就是说从Lollip ...
- asp.net 连接access数据库方法
在 Web.Config 中配置 Access 数据库驱动和数据库文件名称. 请看代码 <appSettings> <add key="DBDriver" val ...
- C# Best Practices - Define Proper Classes
Application Architecture Define the components appropriately for the application and create project ...
- jbpmAPI-6
第六章流程. 6.1. What is BPMN 2.0 业务流程模型和符号(BPMN)2.0规范是OMG规范,不仅定义了一个标准的业务流程的图形化表述(如BPMN 1. x),但现在还包括执行语义定 ...
- codeforces 609E. Minimum spanning tree for each edge 树链剖分
题目链接 给一个n个节点m条边的树, 每条边有权值, 输出m个数, 每个数代表包含这条边的最小生成树的值. 先将最小生成树求出来, 把树边都标记. 然后对标记的边的两个端点, 我们add(u, v), ...