[USACO1.5]Superprime Rib

## Superprime Rib 特殊的质数肋骨##

【题意】
给定位数n(1≤n≤8),要求找出n位数中的质数,并满足每次将最后一位去掉仍为质数。(如质数7331,733是质数,73是质数,7也是质数)

【输入格式】
单独的一行包含 n。

【输出格式】
按顺序输出长度为 n 的特殊质数,每行一个。

【输入样例】
4
【输出样例】
2333
2339
2393
2399
2939
3119
3137
3733
3739
3793
3797
5939
7193
7331
7333
7393

特别666的方法,正难则反。我们用一个dfs来构造一个n位数,每次构造时都判断他是否是素数(一个小剪枝:直接就从2,3,5,7开始,因为必须是素数嘛~~)

#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int n;
int sushu(int x)
{
    if(x==1)return 0;
    if(x%2==0&&x!=2)return 0;
    for(int i=3;i<=sqrt(x);i+=2)if(x%i==0)return 0;
    return 1;
}
int dfs(int k,int ans)
{
    if(k==n)
    {
        printf("%d\n",ans);
    }
    for(int i=1;i<=9;i++)
    {
        int t=ans*10+i;
        if(sushu(t)==1)dfs(k+1,t);
    }
}
int main()
{
    scanf("%d",&n);
    dfs(1,2);
    dfs(1,3);
    dfs(1,5);
    dfs(1,7);
    return 0;
}

原文地址:https://www.cnblogs.com/candy067/p/11401974.html

时间: 2024-08-21 16:57:14

[USACO1.5]Superprime Rib的相关文章

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

P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib 284通过 425提交 题目提供者该用户不存在 标签USACO 难度普及- 提交  讨论  题解 最新讨论 超时怎么办? 题目描述 农民约翰的母牛总是产生最好的肋骨.你能通过农民约翰和美国农业部标记在每根肋骨上的数字认出它们.农民约翰确定他卖给买方的是真正的质数肋骨,是因为从右边开始切下肋骨,每次还剩下的肋骨上的数字都组成一个质数,举例来说: 7 3 3 1 全部肋骨上的数字 7331是质数;三根肋骨 733是质数;二

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

P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib (数论—素数 + DFS)

这大概是我写的第一个DFS 题目描述 农民约翰的母牛总是产生最好的肋骨.你能通过农民约翰和美国农业部标记在每根肋骨上的数字认出它们.农民约翰确定他卖给买方的是真正的质数肋骨,是因为从右边开始切下肋骨,每次还剩下的肋骨上的数字都组成一个质数,举例来说: 7 3 3 1 全部肋骨上的数字 7331是质数;三根肋骨 733是质数;二根肋骨 73 是质数;当然,最后一根肋骨 7 也是质数. 7331 被叫做长度 4 的特殊质数.写一个程序对给定的肋骨的数目 N (1<=N<=8),求出所有的特殊质数.

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

题目描述 农民约翰的母牛总是产生最好的肋骨.你能通过农民约翰和美国农业部标记在每根肋骨上的数字认出它们.农民约翰确定他卖给买方的是真正的质数肋骨,是因为从右边开始切下肋骨,每次还剩下的肋骨上的数字都组成一个质数,举例来说: 7 3 3 1 全部肋骨上的数字 7331是质数;三根肋骨 733是质数;二根肋骨 73 是质数;当然,最后一根肋骨 7 也是质数. 7331 被叫做长度 4 的特殊质数.写一个程序对给定的肋骨的数目 N (1<=N<=8),求出所有的特殊质数.数字1不被看作一个质数. 输

USACO 1.5.3 SuperPrime Rib

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 rib

USACO Section1.5 Superprime Rib 解题报告

sprime解题报告 —— icedream61 博客园(转载请注明出处)------------------------------------------------------------------------------------------------------------------------------------------------[题目] 列出所有N位的超级素数. 所谓超级素数,即指其任意位前缀均为素数.例如7.73.733.7331均为素数,故而7331为超级素数

usaco Superprime Rib

题意是求长度为N,的且,前i(i为,1,2,3.....N-1)位数字构成的数字都是素数的数字 例如2333,其中2,23,233,2333,都是素数 /* ID: modengd1 PROG: sprime LANG: C++ */ #include <iostream> #include <stdio.h> using namespace std; bool IsPrime(int x) { if(x==1) return false; if(x==2) return true

Superprime Rib

链接 分析:满足题目条件的必然是1,2,3,5,7,9这几个数字的组合,DFS按位进行即可,边组合边判断是否合法. 1 /* 2 PROB:sprime 3 ID:wanghan 4 LANG:C++ 5 */ 6 #include "iostream" 7 #include "cstdio" 8 #include "cstring" 9 #include "string" 10 #include "vector&q

USACO Superprime Rib(dfs)

题目请点我 题解: 这道题其实很简单,dfs+从前往后判断就好了,每递归一层就进行一次判断. USACO第一章节,最后一发.也是放假回家前的最后一道题!! 代码实现: /* ID: eashion LANG: C++ TASK: sprime */ #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #define