Description

  小明对数的研究比较热爱,一谈到数,脑子里就涌现出好多数的问题,今天,小明想考考你对素数的认识。 
  问题是这样的:一个十进制数,如果是素数,而且它的各位数字和也是素数,则称之为“美素数”,如29,本身是素数,而且2+9 = 11也是素数,所以它是美素数。 
  给定一个区间,你能计算出这个区间内有多少个美素数吗?
 

Input

第一行输入一个正整数T,表示总共有T组数据(T <= 10000)。 
接下来共T行,每行输入两个整数L,R(1<= L <= R <= 1000000),表示区间的左值和右值。
 

Output

对于每组数据,先输出Case数,然后输出区间内美素数的个数(包括端点值L,R)。 
每组数据占一行,具体输出格式参见样例。
 

Sample Input

3
1 100
2 2
3 19
 

Sample Output

Case #1: 14
Case #2: 1
Case #3: 4
 
如果不考虑时间限制的话,这道题是非常简单的。因此解决时间上的问题,这道题也就解出来了。
Eratosthenese筛法,这种方法是用来筛掉那些不是素数的方法,剩下的当然只有素数了,这样就能很好地建立一个素数表
筛法的思想很简单,对于不超过n的每个非负整数p,筛去2p,3p,4p,5p.......剩下的即为素数
 
#include"iostream"
#include"cstdio"
#include"cstring"
using namespace std;
const int maxn=+;
int ans[maxn+];
int noprime[maxn+]; void Prime()
{
memset(noprime,,sizeof(noprime));
noprime[]=;
noprime[]=;
for(int i=;i<=maxn;i++)
if(!(noprime[i]))
for(int j=i*;j<=maxn;j+=i)
noprime[j]=;
} int sum(int a)
{
int s=;
while(a/)
{
s+=a%;
a/=;
}
s+=a;
return s;
} int main()
{
Prime();
int T,ca=;
ans[]=;
ans[]=;
for(int i=;i<=maxn;i++)
{
if((!noprime[i])&&(!noprime[sum(i)]))
ans[i]=ans[i-]+; //这样写便于快速查询
else
ans[i]=ans[i-];
}
cin>>T;
while(T--)
{
int l,r;
cin>>l>>r;
printf("Case #%d: %d\n",ca++,ans[r]-ans[l-]);
}
return ;
}
 

集训第六周 数学概念与方法 数论 筛素数 H题的更多相关文章

  1. 集训第六周 数学概念与方法 数论 线性方程 I题

    Description The Sky is Sprite. The Birds is Fly in the Sky. The Wind is Wonderful. Blew Throw the Tr ...

  2. 集训第六周 数学概念与方法 概率 F题

    Submit Status Description Sometimes some mathematical results are hard to believe. One of the common ...

  3. 集训第六周 数学概念与方法 计数 排列 L题

    Description 大家常常感慨,要做好一件事情真的不容易,确实,失败比成功容易多了! 做好“一件”事情尚且不易,若想永远成功而总从不失败,那更是难上加难了,就像花钱总是比挣钱容易的道理一样. 话 ...

  4. 集训第六周 数学概念与方法 J题 数论,质因数分解

    Description Tomorrow is contest day, Are you all ready? We have been training for 45 days, and all g ...

  5. 集训第六周 数学概念与方法 概率 N题

    N - 概率 Time Limit:4000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status ...

  6. 集训第六周 数学概念与方法 概率 数论 最大公约数 G题

    Description There is a hill with n holes around. The holes are signed from 0 to n-1. A rabbit must h ...

  7. 集训第六周 数学概念与方法 UVA 11181 条件概率

    http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18546 题意:有n个人会去超市,其中只有r个人会买东西,每个人独自买东西的概 ...

  8. 集训第六周 数学概念与方法 UVA 11722 几何概型

    ---恢复内容开始--- http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=31471 题意,两辆火车,分别会在[t1,t2],[ ...

  9. 集训第六周 古典概型 期望 D题 Discovering Gold 期望

    Description You are in a cave, a long cave! The cave can be represented by a 1 x N grid. Each cell o ...

随机推荐

  1. Golang 入门 : 理解并发与并行

    Golang 的语法和运行时直接内置了对并发的支持.Golang 里的并发指的是能让某个函数独立于其他函数运行的能力.当一个函数创建为 goroutine 时,Golang 会将其视为一个独立的工作单 ...

  2. 在xampp集成环境下使用 thinkphp 连接oracle

    今天搞了大半天,终于成功了. 1. 首先需要让xampp支持oracle,直接按这个网页上说的做就行.http://nimal.info/blog/2009/activate-oracle-on-xa ...

  3. flask框架基础入门一

    首先:flask是一个基于Werkzeug,Jinja2的一个python的微服务框架. 安装flask框架: pip install flask 一个最小的最简单的flask应用: from fla ...

  4. [USACO 2012 Jan Silver] Bale Share【DP】

    传送门:http://www.usaco.org/index.php?page=viewproblem2&cpid=107 没想到太不应该了,真的不应该啊! f[i][j][k]表示前i个包, ...

  5. 桥接模式和php实现

    桥接模式(Bridge Pattern): 将抽象部分与它的实现部分分离,使它们都可以独立地变化.它是一种对象结构型模式,又称为柄体(Handle and Body)模式或接口(Interface)模 ...

  6. Python 设计模式--简单工厂模式

    简单工厂模式(Factory Pattern)是一种创建型的设计模式,像工厂一样根据要求生产对象实例. 特点:根据不同的条件,工厂实例化出合适的对象. <大话设计模式>中实例:四则运算计算 ...

  7. CSS3实现边框线条动画特效

    <div class="box-line"></div> CSS代码 .box-line, .box-line::before, .box-line::af ...

  8. 仿微信右滑关闭Activity

    SwipeBackLayout 1.AS添加依赖 compile 'me.imid.swipebacklayout.lib:library:1.0.0' eclipse 想办法下载库工程,以库工程形式 ...

  9. iOS Programming Web Services and UIWebView

    iOS Programming Web Services and UIWebView The work is divided into two parts. The first is connecti ...

  10. iOS programming UITableView and UITableViewController

    iOS programming  UITableView and UITableViewController A UITableView displays a single column of dat ...