USACO 1.5 Superprime Rib
Superprime Rib
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.
PROGRAM NAME: sprime
INPUT FORMAT
A single line with the number N.
SAMPLE INPUT (file sprime.in)
4
OUTPUT FORMAT
The superprime ribs of length N, printed in ascending order one per line.
SAMPLE OUTPUT (file sprime.out)
2333
2339
2393
2399
2939
3119
3137
3733
3739
3793
3797
5939
7193
7331
7333
7393 题目大意:我们想要这样的数字,所有的前缀都是素数(质数),比如7193,从左边开始,7是质数,71是质数719是质数7193是质数,给定一个n(1到8),顺序输出长度为n的符合要求的所有的数。
思路:很暴力的题目呀,数据很水,直接暴力枚举,然后check就好。但是暴力也有剪枝策略,首位只能是2,3,5,7,中的一个(因为第一个前缀就是单独的第一个字符,要求是素数),其他位只能是1,3,7,9中的一个(因为一定会成为某个前缀的结尾,然而如果结尾是偶数或者5的话就是2或者5的倍数就不是质数了),这样就可以很快得到答案。
/*
ID:fffgrdc1
PROB:sprime
LANG:C++
*/
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
using namespace std;
int n;
int temp=;
bool bo[];
int cnt=,prime[];
const int maxn=;
void init()
{
memset(bo,,sizeof(bo));
bo[]=bo[]=;
for(int i=;i<maxn;i++)
{
if(!bo[i])
{
prime[++cnt]=i;
for(int j=;j*i<maxn;j++)
{
bo[i*j]=;
}
}
}
}
bool check(int x)
{
if(x<=maxn)return !bo[x];
int temp=sqrt(double(x));
for(int i=;i<=cnt&&prime[i]<=temp;i++)
{
if(!(x%prime[i]))return ;
}
return ;
}
void dfs(int x,int nown)
{
x++;
int temp;
if(x==)
{
dfs(x,);
dfs(x,);
dfs(x,);
dfs(x,);
}
else if(x==n)
{
temp=nown*+;
if(check(temp))
printf("%d\n",temp);
temp+=;//==3
if(check(temp))
printf("%d\n",temp);
temp+=;//==7
if(check(temp))
printf("%d\n",temp);
temp+=;//==9
if(check(temp))
printf("%d\n",temp);
return ;
}
else
{
temp=nown*+;
if(check(temp))
dfs(x,temp);
temp+=;//==3
if(check(temp))
dfs(x,temp);
temp+=;//==7
if(check(temp))
dfs(x,temp);
temp+=;//==9
if(check(temp))
dfs(x,temp);
}
}
int main()
{
freopen("sprime.in","r",stdin);
freopen("sprime.out","w",stdout);
init();
scanf("%d",&n);
if(n==)
{
printf("2\n3\n5\n7\n");
}
else
{
dfs(,);
}
return ;
}
对了,借此机会学习了一下线性筛法,附上代码
void init()
{
memset(bo,,sizeof(bo));
bo[]=bo[]=;
for(int i=;i<maxn;i++)
{
if(!bo[i])prime[++cnt]=i;
for(int j=;j<=cnt;j++)
{
if(prime[j]*i>=maxn)break;
bo[prime[j]*i]=;
if(!i%prime[j])break;
}
}
}
USACO section 1 结束了,大概就是各种模拟加普通的搜索,感觉没什么难度,继续努力。
USACO 1.5 Superprime Rib的更多相关文章
- USACO Section1.5 Superprime Rib 解题报告
sprime解题报告 —— icedream61 博客园(转载请注明出处)--------------------------------------------------------------- ...
- USACO Superprime Rib
洛谷 P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib 洛谷传送门 JDOJ 1673: Superprime Rib JDOJ传送门 题目描述 农民约翰的母牛总是产生最好 ...
- 洛谷P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib
P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib 284通过 425提交 题目提供者该用户不存在 标签USACO 难度普及- 提交 讨论 题解 最新讨论 超时怎么办? ...
- 洛谷 P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib
P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib 题目描述 农民约翰的母牛总是产生最好的肋骨.你能通过农民约翰和美国农业部标记在每根肋骨上的数字认出它们.农民约翰确定他卖给 ...
- 洛谷P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib 使用四种算法
洛谷P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib 水题一道…… 题目描述 农民约翰的母牛总是产生最好的肋骨.你能通过农民约翰和美国农业部标记在每根肋骨上的数字认出它们. ...
- 【USACO 1.5】SuperPrime Rib
/* TASK: sprime LANG: C++ SOLVE: dfs,后面每增加一位,判断当前是否为素数. 第一位不能为0 */ #include<cstdio> int n; voi ...
- USACO Section 1.5 Superprime Rib 解题报告
题目 题目描述 超级素数的定义如下:如果有个素数我们从右往左依次去掉一位数,每次去掉一位数剩下的数仍然是素数,那么我们称这个数是超级素数.例如7331,这是一个素数,从右往左依次去掉一位数733, 7 ...
- [Swust OJ 799]--Superprime Rib(DFS)
题目链接:http://acm.swust.edu.cn/problem/799/ Time limit(ms): 1000 Memory limit(kb): 10000 Description ...
- P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib
题目描述 农民约翰的母牛总是产生最好的肋骨.你能通过农民约翰和美国农业部标记在每根肋骨上的数字认出它们.农民约翰确定他卖给买方的是真正的质数肋骨,是因为从右边开始切下肋骨,每次还剩下的肋骨上的数字都组 ...
随机推荐
- java8 Stream 笔记
stream的定义:对一个源中的一系列元素进行聚合操作. 一系列元素:stream对一组有特定类型的元素提供了一个接口.但是stream并不真正存储元素,元素根据需求被计算出来. 源:stream可以 ...
- Three入门学习笔记整理
一.官方网站:https://threejs.org 二.关于Three.js 三.开始 四.实例 基本结构 结果 五.概念 坐标系 场景 相机 灯光 3D模型 六.简单动画 七.交互控制 结束 # ...
- 了解jQuery的$符号
$是什么? 可以使用typeof关键字来观察$的本质. console.log(type of $); //输出结果为function 因此可以得出结论,$其实就是一个函数.$(); 只是根据所给参数 ...
- 解决启动httpd报: apr_sockaddr_info_get() failed for错误
我测试库里 service httpd start 时报 下面错误 httpd: apr_sockaddr_info_get() failed for fengxin.wzjzt.centoshttp ...
- 采用tcp协议和UDP协议实现简单的聊天功能
Date: 2019-06-19 Author: Sun 一. Python3输出带颜色字体 实现过程: 终端的字符颜色是用转义序列控制的,是文本模式下的系统显示功能,和具体的语言无关. 转义 ...
- javase 异常处理
1.简述什么是异常.异常的继承体系? 异常就是java代码块在运行时出现的错误,有编译错误和运行错误, Throwable是所有异常的父类它包含了error和Exception两个子类. 其中e ...
- JS 马托货物
大马驮2石粮食,中马驮1石粮食,两头小马驮一石粮食,要用100匹马,驮100石粮食,该如何调配? <!DOCTYPE html> <html> <head> < ...
- BZOJ 1180 / 2843 LCT模板题_双倍经验
一大早上到机房想先拍一下模板,热热身. 结果....对照着染色敲的 LCT 竟然死活也调不过去(你说我抄都能抄错) 干脆自己重新敲了一遍,10min就敲完了....... 还是要相信自己 Code: ...
- CSS - Span 下的width设置不可用?
解决:Span 下的width设置不可用? 内联元素-span有根据内容自动伸缩的能力,当需要对其宽度设定时,出现无效的情况. Demo:http://jsfiddle.net/JSDavi/ad62 ...
- express get和post方法
把之前学习的一个小例子贴出来: 前提:需安装nodejs,可以在终端中输入node -v检查是否安装成功,安装成功后才可执行下面的步骤. 1.新建一个名称为“node”文件夹 2.进入node目录 ...