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 解题报告的更多相关文章

  1. USACO Section1.5 Prime Palindromes 解题报告

    pprime解题报告 —— icedream61 博客园(转载请注明出处)--------------------------------------------------------------- ...

  2. USACO Section1.5 Number Triangles 解题报告

    numtri解题报告 —— icedream61 博客园(转载请注明出处)--------------------------------------------------------------- ...

  3. USACO Section1.4 Arithmetic Progressions 解题报告

    ariprog解题报告 —— icedream61 博客园(转载请注明出处)-------------------------------------------------------------- ...

  4. USACO Section1.3 Combination Lock 解题报告

    combo解题报告 —— icedream61 博客园(转载请注明出处)---------------------------------------------------------------- ...

  5. USACO Section1.3 Prime Cryptarithm 解题报告

    crypt1解题报告 —— icedream61 博客园(转载请注明出处)--------------------------------------------------------------- ...

  6. USACO Section1.3 Barn Repair 解题报告

    barn1解题报告 —— icedream61 博客园(转载请注明出处)---------------------------------------------------------------- ...

  7. USACO Section1.3 Mixing Milk 解题报告

    milk解题报告 —— icedream61 博客园(转载请注明出处)----------------------------------------------------------------- ...

  8. USACO Section1.2 Palindromic Squares 解题报告

    palsquare解题报告 —— icedream61 博客园(转载请注明出处)------------------------------------------------------------ ...

  9. USACO Section1.2 Dual Palindromes 解题报告

    dualpal解题报告 —— icedream61 博客园(转载请注明出处)-------------------------------------------------------------- ...

随机推荐

  1. 从windows CMD 命令行(CMD promp)运行Docker

    英文原帖 Running Docker from Windows CMD prompt https://medium.com/@neil.avery_68603/running-docker-from ...

  2. server 2008 64位安装Rational错误

    Administrator has detected that this is a terminal server session.Administrator does not support run ...

  3. frame、window和dialog区别

    属性 Window Frame Dialog 模式化 不是 不是 不是(可设置) 可调大小 不可 可 可 标题栏 无 有 有 边界 无 有 有 标题 无 有 有 菜单栏 无 有 无 焦点管理器 有 有 ...

  4. 332. Reconstruct Itinerary (leetcode)

    1. build the graph and then dfs -- graph <String, List<String>>,  (the value is sorted a ...

  5. IOS 运行循环

    . 运行循环========================================在iOS的应用程序中,应用程序启动之后,系统即会创建一个运行循环监听用户的交互. 以下代码其本质是在运行循环 ...

  6. IOS 自定义代理delegate方法

    创建一个自定义代理 @class MJTgFooterView; /** 1.协议名称: 控件类名 + Delegate 2.代理方法普遍都是@optional 3. */ @protocol MJT ...

  7. IOS xcode常用的快捷键

    新建 shift + cmd + n     新建项目 cmd + n             新建文件 视图 option + cmd + 回车 打开助理编辑器 cmd + 回车           ...

  8. 深搜,四色定理,(POJ1129)

    题目链接:http://poj.org/problem?id=1129 解题报告: #include<iostream> #include<cstdio> #include&l ...

  9. 第23章 I2C—读写EEPROM—零死角玩转STM32-F429系列

    第23章     I2C—读写EEPROM 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/f ...

  10. Webpack4 学习笔记四 暴露全局变量、externals

    前言 此内容是个人学习笔记,以便日后翻阅.非教程,如有错误还请指出 webpack 暴露全局变量 通过 expose-loader 内联配置 在 webpack中配置 每个模块通过注入的方式 通过CD ...