洛谷P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib
P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib
- 284通过
- 425提交
- 题目提供者该用户不存在
- 标签USACO
- 难度普及-
提交 讨论 题解
最新讨论
- 超时怎么办?
题目描述
农民约翰的母牛总是产生最好的肋骨。你能通过农民约翰和美国农业部标记在每根肋骨上的数字认出它们。农民约翰确定他卖给买方的是真正的质数肋骨,是因为从右边开始切下肋骨,每次还剩下的肋骨上的数字都组成一个质数,举例来说: 7 3 3 1 全部肋骨上的数字 7331是质数;三根肋骨 733是质数;二根肋骨 73 是质数;当然,最后一根肋骨 7 也是质数。 7331 被叫做长度 4 的特殊质数。写一个程序对给定的肋骨的数目 N (1<=N<=8),求出所有的特殊质数。数字1不被看作一个质数。
输入输出格式
输入格式:
单独的一行包含N。
输出格式:
按顺序输出长度为 N 的特殊质数,每行一个。
输入输出样例
4
2333
2339
2393
2399
2939
3119
3137
3733
3739
3793
3797
5939
7193
7331
7333
7393
说明
题目翻译来自NOCOW。
USACO Training Section 1.5
分析:很自然的想到了枚举这个长度的数,不断地尝试,然后发现TLE,为什么呢?因为如果不断地删数字可能一开始是质数,但是到最后一个就不是质数了,所以考虑从低位向高位搜索,这样也可以保证答案是有序的.
60分算法:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; int n,ans[],num; int power(int x,int d)
{
for (int i = ; i <= x; i++)
d *= ;
return d;
} bool panduan(int x)
{
if (x == )
return false;
for (int i = ; i * i <= x; i++)
if (x % i == )
return false;
return true;
} bool check(int x)
{
while (x)
{
if (!panduan(x))
return false;
x /= ;
}
return true;
} int main()
{
scanf("%d", &n);
for (int i = power(n - , );i <= power(n - , ); i++)
{
if (check(i))
ans[++num] = i;
}
sort(ans + , ans + + num);
for (int i = ; i <= num; i++)
printf("%d\n", ans[i]); return ;
}
100分算法:
#include<iostream>
#include<cstdio>
using namespace std;
int n;
bool check(int x)
{
if (x == )
return ;
for (int i = ;i*i <= x;i++)
if (x%i == )
return ;
return ;
}
void dfs(int u, int fa)
{
for (int i = ;i <= ;i++)
if (check(fa * + i))
{
if (u == n)
printf("%d\n", fa * + i);
else
dfs(u + , fa * + i);
}
}
int main()
{
scanf("%d", &n);
dfs(, ); return ;
}
洛谷P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib的更多相关文章
- 洛谷P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib 使用四种算法
洛谷P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib 水题一道…… 题目描述 农民约翰的母牛总是产生最好的肋骨.你能通过农民约翰和美国农业部标记在每根肋骨上的数字认出它们. ...
- 洛谷 P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib
P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib 题目描述 农民约翰的母牛总是产生最好的肋骨.你能通过农民约翰和美国农业部标记在每根肋骨上的数字认出它们.农民约翰确定他卖给 ...
- P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib (数论—素数 + DFS)
这大概是我写的第一个DFS 题目描述 农民约翰的母牛总是产生最好的肋骨.你能通过农民约翰和美国农业部标记在每根肋骨上的数字认出它们.农民约翰确定他卖给买方的是真正的质数肋骨,是因为从右边开始切下肋骨, ...
- P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib
题目描述 农民约翰的母牛总是产生最好的肋骨.你能通过农民约翰和美国农业部标记在每根肋骨上的数字认出它们.农民约翰确定他卖给买方的是真正的质数肋骨,是因为从右边开始切下肋骨,每次还剩下的肋骨上的数字都组 ...
- 洛谷 P1217 [USACO1.5]回文质数 Prime Palindromes
P1217 [USACO1.5]回文质数 Prime Palindromes 题目描述 因为151既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 151 是回文质数. 写一个程序来找 ...
- 洛谷 P1217 [USACO1.5]回文质数 Prime Palindrome
嗯... 这道题对于蒟蒻的我来说实在是TQL... 先看一下题:(题目链接:https://www.luogu.org/problemnew/show/P1217) 然后说一下我的做题过程吧: 一看到 ...
- 洛谷 P1217 [USACO1.5]回文质数 Prime Palindromes【取回文数/数论/字符串】
题目描述 因为151既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 151 是回文质数. 写一个程序来找出范围[a,b](5 <= a < b <= 100,000 ...
- Java实现 洛谷 P1217 [USACO1.5]回文质数 Prime Palindromes
import java.util.Scanner; public class Main { private static Scanner cin; public static void main(St ...
- 洛谷P1214 [USACO1.4]等差数列 Arithmetic Progressions
P1214 [USACO1.4]等差数列 Arithmetic Progressions• o 156通过o 463提交• 题目提供者该用户不存在• 标签USACO• 难度普及+/提高 提交 讨论 题 ...
随机推荐
- 99. Recover Binary Search Tre
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- Apache+php在windows下的安装和配置
下载和配置php 下载php:http://windows.php.net/download/ php-5.4.16-Win32-VC9-x86.zip 下载apache: http://ht ...
- C# new 和 override.
http://www.dotblogs.com.tw/skychang/archive/2012/05/10/72114.aspx?fid=60865
- duplicate symbols
duplicate symbol _mCollecatView in: /Users/Rubert/Library/Developer/Xcode/DerivedData/ChengDuHidengD ...
- Spark on yarn配置项说明与优化整理
配置于spark-default.conf 1. #spark.yarn.applicationMaster.waitTries 5 用于applicationMaster等待Spark maste ...
- POJ 3311 Hie with the Pie(Floyd+状态压缩DP)
题是看了这位的博客之后理解的,只不过我是又加了点简单的注释. 链接:http://blog.csdn.net/chinaczy/article/details/5890768 我还加了一些注释代码,对 ...
- SCREAM:Error suppression ignored for
wamp报错SCREAM:Error suppression ignored for 问题:SCREAM:Error suppression ignored for 解决: 在php.ini最下面加入 ...
- 关于maven的一些常见用法
1: 查看插件的目标信息:mvn help:describe 2: 生成javadoc并指定编码:mvn javadoc:javadoc -Dencoding=UTF-8 -Dcharset=UTF- ...
- DirFile
using System; using System.Text; using System.IO; namespace MyListen { /// <summary> /// 文件操作夹 ...
- 【转】select和epoll模型的差异
http://www.cppblog.com/converse/archive/2008/10/12/63836.html epoll为什么这么快 epoll是多路复用IO(I/O Multiplex ...