码迷,mamicode.com
首页 > 其他好文 > 详细

USACO 1.5 Superprime Rib

时间:2015-11-28 15:07:18      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:

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的倍数就不是质数了),这样就可以很快得到答案。


USACO section 1 结束了,大概就是各种模拟加普通的搜索,感觉没什么难度,继续努力。

USACO 1.5 Superprime Rib

标签:

原文地址:http://www.cnblogs.com/xuwangzihao/p/5002627.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!