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. king枚举帮助类

    可以方便的实现枚举 枚举 public enum DeptType { [Description("科室1")] Professional = , [Description(&qu ...

  2. chrome打开控制台状态下,没有人为打断点,自动进入断点模式的解决方法

    如下图所示:在控制台去掉Sources -> XHR/fetch Breakpoints -> Any XHR or fetch 的勾

  3. Codeforces Round #447

    QAQ #include<stdio.h> #include<string.h> #include<stdlib.h> #include<vector> ...

  4. C#关于VSHOST.EXE停止工作的解决办法,VS2008

    主要原因就是电脑系统系统32位和64位的问题在项目属性中修改下即可. 方法: 右击项目 - 属性 - 生成 - 目标平台 - Any CPU[改为x86] 虽然简单,但如不知原因却恼火的紧,贴出来如有 ...

  5. CreateFile打开文件或者打开目录

    一.打开目录 参数列表: lpFileName String 要打开的文件的名字 dwDesiredAccess Long 如果为 GENERIC_READ 表示允许对设备进行读访问:如果为 GENE ...

  6. MyBatis源码分析(各组件关系+底层原理

    MyBatis源码分析MyBatis流程图 下面将结合代码具体分析. MyBatis具体代码分析 SqlSessionFactoryBuilder根据XML文件流,或者Configuration类实例 ...

  7. 【airtest】iOS,Android 依托 jenkins 并行跑

    Airtest 只支持一台mac 连接一台iPhone,  以下方法是以“一台mac 连接一台iPhone”为基础,依托jenkins 统一管理多台iPhone. [mac] jenkins mast ...

  8. There are no packages available for installation. Sublime3解决方法

    最近在学习Vue,在配置sublime3的时候,想要高亮vue的语法,下载点插件 Package Control的时候,总报  There are no packages available for ...

  9. mysql 每个月创建新表

    1.CREATE DEFINER=`root`@`%` PROCEDURE `aa`()BEGIN SET @sqlstr = CONCAT('create table cdrpbx10_',DATE ...

  10. Tensorflow 之物体检测

    1)安装Protobuf TensorFlow内部使用Protocol Buffers,物体检测需要特别安装一下. # yum info protobuf protobuf-compiler 2.5. ...