USACO Section1.5 Superprime Rib 解题报告
sprime解题报告 —— icedream61 博客园(转载请注明出处)
------------------------------------------------------------------------------------------------------------------------------------------------
【题目】
列出所有N位的超级素数。
所谓超级素数,即指其任意位前缀均为素数。例如7、73、733、7331均为素数,故而7331为超级素数。
【数据范围】
1<=N<=8
【输入样例】
4
【输出样例】
2333
2339
2393
2399
2939
3119
3137
3733
3739
3793
3797
5939
7193
7331
7333
7393
------------------------------------------------------------------------------------------------------------------------------------------------
【分析】
逐位枚举+筛法+试除法。
这道题需要逐位枚举,具体过程描述如下:
1.定义0位的素数只有0(为了编程方便),调用函数go(0,0);
2.函数go(k,x)主体:x是k位的超级素数,枚举y=x*10+i(i=0~9),由筛法所得数组判断,若y为超级素数则调用go(k+1,y);
3.函数go(k,x)边界:当k==N时,输出x并return;
4.函数go(k,x)特判(置于边界判断后):当k==7&&N==8时,枚举y=x*10+i(i=0~9),由试除法判断,若y为超级素数则调用go(k+1,y);
值得一提的是,这里的特判可以大大提高运行效率,大约从2s缩减到0.2s,性价比很高。
------------------------------------------------------------------------------------------------------------------------------------------------
【总结】
又是同样的问题,好奇怪!为什么100,000,000过不了?求解答啊!
我的处理方法是:令max=10,000,000,对于N==8的情况,在枚举到第8位的时候采用试除法判素数。这样便可以过了。
不过这样的处理倒是很不错的,因为这样一来筛法的时间变成了十分之一,而到8位可枚举的数已经少之又少,所以程序的运行效率提高极为显著。原来代码运行时间是2s+,现在只有不到0.2s了。学习了!
------------------------------------------------------------------------------------------------------------------------------------------------
【代码】
/*
ID: icedrea1
PROB: sprime
LANG: C++
*/ #include <iostream>
#include <fstream>
#include <cmath>
using namespace std; const int maxd = ;
bool d[+maxd]; int N; bool isPrime(int x)
{
for(int i=;i<=sqrt(x);++i)
if(x%i==) return false;
return true;
} void go(int k,int x,ofstream &out)
{
if(k==N) { out<<x<<endl; return; }
x*=;
if(N== && k==)
{
for(int i=;i<=;++i)
if(isPrime(x+i)) go(k+,x+i,out);
return;
}
for(int i=;i<=;++i)
if(!d[x+i]) go(k+,x+i,out);
} int main()
{
ifstream in("sprime.in");
ofstream out("sprime.out"); d[]=d[]=true;
for(int i=;i<=;++i)
if(!d[i])
for(int j=i+i;j<=maxd;j+=i) d[j]=true; in>>N; go(,,out); in.close();
out.close();
return ;
}
USACO Section1.5 Superprime Rib 解题报告的更多相关文章
- USACO Section1.5 Prime Palindromes 解题报告
pprime解题报告 —— icedream61 博客园(转载请注明出处)--------------------------------------------------------------- ...
- USACO Section1.5 Number Triangles 解题报告
numtri解题报告 —— icedream61 博客园(转载请注明出处)--------------------------------------------------------------- ...
- USACO Section1.4 Arithmetic Progressions 解题报告
ariprog解题报告 —— icedream61 博客园(转载请注明出处)-------------------------------------------------------------- ...
- USACO Section1.3 Combination Lock 解题报告
combo解题报告 —— icedream61 博客园(转载请注明出处)---------------------------------------------------------------- ...
- USACO Section1.3 Prime Cryptarithm 解题报告
crypt1解题报告 —— icedream61 博客园(转载请注明出处)--------------------------------------------------------------- ...
- USACO Section1.3 Barn Repair 解题报告
barn1解题报告 —— icedream61 博客园(转载请注明出处)---------------------------------------------------------------- ...
- USACO Section1.3 Mixing Milk 解题报告
milk解题报告 —— icedream61 博客园(转载请注明出处)----------------------------------------------------------------- ...
- USACO Section1.2 Palindromic Squares 解题报告
palsquare解题报告 —— icedream61 博客园(转载请注明出处)------------------------------------------------------------ ...
- USACO Section1.2 Dual Palindromes 解题报告
dualpal解题报告 —— icedream61 博客园(转载请注明出处)-------------------------------------------------------------- ...
随机推荐
- 【转载】#330 - Derived Classes Do Not Inherit Constructors
A derived class inherits all of the members of a base class except for its constructors. You must de ...
- matlab所需插件
1
- Python 爬虫的工具列表
这个列表包含与网页抓取和数据处理的Python库 网络 通用 urllib -网络库(stdlib). requests -网络库. grab - 网络库(基于pycurl). pycurl - 网络 ...
- apache单ip配置多端口多站点
1.修改 /etc/httpd/conf/httpd.conf 添加一个Listen,如: Listen 80 Listen 8001 Listen 8002 2.添加一个VirtualHost #v ...
- 8、SpringBoot+Mybatis整合------参数取值方式
前言: 我们知道,在mybatis中,参数取值方式有两种: #{ } 和 ${ } 下面,我们来探讨下#{ }与${ }不同. 一.#{ } 例: select * from student wher ...
- 【例题收藏】◇例题·II◇ Berland and the Shortest Paths
◇例题·II◇ Berland and the Shortest Paths 题目来源:Codeforce 1005F +传送门+ ◆ 简单题意 给定一个n个点.m条边的无向图.保证图是连通的,且m≥ ...
- lintcode_69_二叉树的层次遍历
二叉树的层次遍历 描述 笔记 数据 评测 给出一棵二叉树,返回其节点值的层次遍历(逐层从左往右访问) 您在真实的面试中是否遇到过这个题? Yes 哪家公司问你的这个题? LinkedIn Airb ...
- HTML5一些标签和属性
<bdo> 元素 可以覆盖默认文本的方向 根据dir 属性来控制文字的排序方向 属性:dir="rtl" ...
- dts--tests(三)
sample_built.py """ DPDK Test suite. Test sample_built. """ import uti ...
- 小程序里面使用wxParse解析富文本导致页面空白等
在部分安卓手机上会出现白屏的情况且有些ios手机上图文混排上,图片显示不出问题 解决:把插件里面的console.dir去掉即可(原因在于安卓手机无法解析console.dir) 有些图片解析出来下面 ...