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
题目描述 农民约翰的母牛总是产生最好的肋骨.你能通过农民约翰和美国农业部标记在每根肋骨上的数字认出它们.农民约翰确定他卖给买方的是真正的质数肋骨,是因为从右边开始切下肋骨,每次还剩下的肋骨上的数字都组 ...
随机推荐
- html中DTD使用小结
DTD 是一套关于标记符的语法规则.它是XML1.0版规格得一部分,是html文件的验证机制,属于html文件组成的一部分. DTD:三种文档类型:S(Strict).T(Transitional). ...
- Python编程Web框架 :Django 从入门到精通
Django是一个高级别的Python Web框架,它鼓励快速开发和干净实用的设计. 现在我们开始学习它. Django学习之 第一章:Django介绍 Django学习之 第二章:Django快速上 ...
- Django 中的 csrf_token 与单元测试
Django 中的 csrf_token 与单元测试 在<Python Web开发:测试驱动方法>一书中作者使用的 Django 版本是 1.7,而我使用的是1.9.7版(官网已经更新到1 ...
- win 2016 添加系统组件注册表,
Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\ServerManager\ServicingS ...
- win系统如何在桌面显示我的电脑
如果是在Windows Server 2012本地控制台下,直接按Win(键盘上的微软徽标键)+R,输入: rundll32.exe shell32.dll,Control_RunDLL desk.c ...
- PhotoZoom放大的图片效果怎么样?清不清晰?
PhotoZoom是一款使用了革命性技术.效果最好的图像无损放大工具.它可以对图片进行放大而没有锯齿,不会失真,让您无与伦比完美放大图像质量. PhotoZoom Pro使用了S-Spline Max ...
- re模块findall函数用法
title: Python subtitle: 1.re模块findall函数用法 date: 2018-12-13 10:17:28 --- Python re 模块 findall 函数用法简述 ...
- ZJOI2015 幻想乡战略游戏 动态点分治_树链剖分_未调完
Description 傲娇少女幽香正在玩一个非常有趣的战略类游戏,本来这个游戏的地图其实还不算太大,幽香还能管得过来,但是不知道为什么现在的网游厂商把游戏的地图越做越大,以至于幽香一眼根本看不过来, ...
- [POI2005]AUT-The Bus 树状数组维护最大前缀和
#include<cstdio> #include<algorithm> using namespace std; const int N=100000+3; int x[N] ...
- Xpath--使用Xpath爬取糗事百科成人版图片
#!usr/bin/env python#-*- coding:utf-8 _*-"""@author:Hurrican@file: 爬取糗事百科.py@time: 20 ...