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
题目描述 农民约翰的母牛总是产生最好的肋骨.你能通过农民约翰和美国农业部标记在每根肋骨上的数字认出它们.农民约翰确定他卖给买方的是真正的质数肋骨,是因为从右边开始切下肋骨,每次还剩下的肋骨上的数字都组 ...
随机推荐
- C - Puzzles
Problem description The end of the school year is near and Ms. Manana, the teacher, will soon have t ...
- 带中横线的日期格式在iOS手机系统上 转换时间戳NaN问题
类似于 '2019-04-01 14:13:00' 这样的日期格式转换时间戳在iOS手机上是无法转换的,需要先处理日期格式成 '2019/04/01 14:13:00' var str = '2019 ...
- 派遣函数IRP
派遣函数是Windows驱动程序中的重要概念.驱动程序的主要功能是负责处理I/O请求,其中大部分I/O请求是在派遣函数中处理的. 用户模式下所有对驱动程序的I/O请求,全部由操作系统转换为一个叫做IR ...
- CI中的url相关函数以及路由设置和伪静态技术
当使用CI框架进行开发时,我们的一些数据传递的URL不应该写死,可以使用如下方法:比如说我们需要表单提交一个数据: 1.在controller控制器中我们需要先创建一个加载helper和视图的方法: ...
- poj3281 Dining 最大流(奇妙的构图)
我是按照图论500题的文档来刷题的,看了这题怎么也不觉得这是最大流的题目.这应该是题目做得太少的缘故. 什么是最大流问题?最大流有什么特点? 最大流的特点我觉得有一下几点: 1.只有一个起点.一个终点 ...
- 使用eclipse,对spring boot 进行springloader或者devtool热部署失败处理方法
确定配置进行依赖和配置没有错误后. 调整spring boot 的版本,因为新版本对老版本的spring boot 不能使用. 改为: <groupId>org.springframewo ...
- Eclipse中Git的基本使用
以下所有命令如没有特殊说明,均在命令行中完成(cmd窗口) 1.全局设定(需要告诉git自己是谁) git config --global user.name "你的名字或昵称&quo ...
- ionic3、Angular4 定时器的使用
// 声明变量 applicationInterval:any; // 定时器 // 使用定时器,每秒执行一次 ionViewDidEnter(){ let that = this; let appl ...
- 获取淘宝sessionkey 实时保存
<?php/* * To change this license header, choose License Headers in Project Properties. * To chang ...
- mysql修改时区的几种方法(转载自https://www.cnblogs.com/shiqiangqiang/p/8393662.html)
说明: 以下记录修改mysql时区的几种方法. 具体: 方法一:通过mysql命令行模式下动态修改 1.1 查看mysql当前时间,当前时区 select curtime(); #或select no ...