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的更多相关文章

  1. USACO Section1.5 Superprime Rib 解题报告

    sprime解题报告 —— icedream61 博客园(转载请注明出处)--------------------------------------------------------------- ...

  2. USACO Superprime Rib

    洛谷 P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib 洛谷传送门 JDOJ 1673: Superprime Rib JDOJ传送门 题目描述 农民约翰的母牛总是产生最好 ...

  3. 洛谷P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib

    P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib 284通过 425提交 题目提供者该用户不存在 标签USACO 难度普及- 提交  讨论  题解 最新讨论 超时怎么办? ...

  4. 洛谷 P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib

    P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib 题目描述 农民约翰的母牛总是产生最好的肋骨.你能通过农民约翰和美国农业部标记在每根肋骨上的数字认出它们.农民约翰确定他卖给 ...

  5. 洛谷P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib 使用四种算法

    洛谷P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib 水题一道…… 题目描述 农民约翰的母牛总是产生最好的肋骨.你能通过农民约翰和美国农业部标记在每根肋骨上的数字认出它们. ...

  6. 【USACO 1.5】SuperPrime Rib

    /* TASK: sprime LANG: C++ SOLVE: dfs,后面每增加一位,判断当前是否为素数. 第一位不能为0 */ #include<cstdio> int n; voi ...

  7. USACO Section 1.5 Superprime Rib 解题报告

    题目 题目描述 超级素数的定义如下:如果有个素数我们从右往左依次去掉一位数,每次去掉一位数剩下的数仍然是素数,那么我们称这个数是超级素数.例如7331,这是一个素数,从右往左依次去掉一位数733, 7 ...

  8. [Swust OJ 799]--Superprime Rib(DFS)

    题目链接:http://acm.swust.edu.cn/problem/799/ Time limit(ms): 1000 Memory limit(kb): 10000   Description ...

  9. P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib

    题目描述 农民约翰的母牛总是产生最好的肋骨.你能通过农民约翰和美国农业部标记在每根肋骨上的数字认出它们.农民约翰确定他卖给买方的是真正的质数肋骨,是因为从右边开始切下肋骨,每次还剩下的肋骨上的数字都组 ...

随机推荐

  1. P3808 【模版】AC自动机(简单版)

    题目背景 这是一道简单的AC自动机模版题. 用于检测正确性以及算法常数. 为了防止卡OJ,在保证正确的基础上只有两组数据,请不要恶意提交. 题目描述 给定n个模式串和1个文本串,求有多少个模式串在文本 ...

  2. DB2 char长度问题

    问题:发现用char转换了后的值长度都变为了11,更长的变为了254

  3. java函数式编程之lambda表达式

    作为比较老牌的面向对象的编程语言java,在对函数式编程的支持上一直不温不火. 认为面向对象式编程就应该纯粹的面向对象,于是经常看到这样的写法:如果你想写一个方法,那么就必须把它放到一个类里面,然后n ...

  4. BOW模型在ANN框架下的解释

    原文链接:http://blog.csdn.net/jwh_bupt/article/details/17540561 作者的视野好,赞一个. 哥德尔第一完备性定理,始终是没有能看完完整的证明,艹!看 ...

  5. 时空上下文视觉跟踪(STC)

    论文的关键点是对时空上下文(Spatio-Temporal Context)信息的利用.主要思想是通过贝叶斯框架对要跟踪的目标和它的局部上下文区域的时空关系进行建模,得到目标和其周围区域低级特征的统计 ...

  6. 人工智能 Python 入门视频

    Python, 是一种面向对象.解释型计算机程序设计语言,由Guido van Rossum于1989年发明,第一个公开发行版发行于1991年.       Python是纯粹的自由软件, 源代码和解 ...

  7. 实现数组类(C++ 拷贝构造函数、拷贝函数)要判断赋值左右对象不相等,坑惨了

    #include <iostream> using namespace std; class ArrayIndexOutOfBoundsException{ // 异常类 public: ...

  8. Vue2实例中的data属性三种写法与作用

    <script src="https://unpkg.com/vue/dist/vue.js"></script> <div id="app ...

  9. timeval的时间转换成毫秒之后多大的数据类型可以装下

    struct timeval { long tv_sec; /*秒*/ long tv_usec; /*微秒*/ }; 秒的定义为long,为了防止溢出,转换成毫秒之后保存在long long中

  10. kali 安装openvas

    因为Kali Linux上没有默认安装OpenVas,因此只好自己摸索着安装了一遍. 如果没有设置过源(/etc/apt/sources.list),设置如下: deb http://http.kal ...