第六周H题 - 数论,晒素数

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

 

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
 
题解:数据过大,打表处理一下
#include <iostream>
#include <cstdio>
using namespace std;
int a[];
int b[];
int juge(int m)
{
for(int i=; i*i<=m; i++)
if(m%i==)
return ;
return ;
}
int tongji()
{
int total=;
for(int i=; i<; i++)
{
if(juge(i))
{
a[i]=;
int f=i,num=;
while(f>)
{
num+=f%;
f/=; }
if(a[num])
total++;
}
b[i]=total; //前i个数的含有的美素数
}
}
int main()
{
tongji();
int l,r,t,k=;
cin>>t;
while(t--)
{
cin>>l>>r;
printf("Case #%d: %d\n",k++,b[r]-b[l-]); //必须是l-1,不然2 2这个案例会错
}
}

还有紫书上的Eratosthenes筛法

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=+;
int a[maxn+];
int f[maxn+];
void juge1()
{
memset(f,,sizeof(f));
for(int i=;i<=maxn;i++)
if((f[i]))
for(int j=i*;j<=maxn;j+=i)
f[j]=;
} int sum(int a)
{
int s=;
while(a/)
{
s+=a%;
a/=;
}
s+=a;
return s;
} int main()
{
juge1();
int l,r,T,k=;
for(int i=;i<=maxn;i++)
{
if((f[i])&&(f[sum(i)]))
a[i]=a[i-]+; //记录前i个数的美素数个数
else
a[i]=a[i-];
}
cin>>T;
while(T--)
{ cin>>l>>r;
printf("Case #%d: %d\n",k++,a[r]-a[l-]);
}
return ;
}
 

hdu 4548 第六周H题(美素数)的更多相关文章

  1. HDU 1465 第六周L题

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

  2. HDU 2669 第六周 I题

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

  3. HDU 1405 第六周 J题

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

  4. 程序设计入门—Java语言 第六周编程题 1 单词长度(4分)

    第六周编程题 依照学术诚信条款,我保证此作业是本人独立完成的. 1 单词长度(4分) 题目内容: 你的程序要读入一行文本,其中以空格分隔为若干个单词,以'.'结束.你要输出这行文本中每个单词的长度.这 ...

  5. 数学概念——H 最美素数

    H - 数论,晒素数 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit S ...

  6. Codeforces 559A 第六周 O题

    Description Gerald got a very curious hexagon for his birthday. The boy found out that all the angle ...

  7. 第六周 E题 期望.....

    Description Given a dice with n sides, you have to find the expected number of times you have to thr ...

  8. 第六周 N题

    Description As Harry Potter series is over, Harry has no job. Since he wants to make quick money, (h ...

  9. HDU1465 第六周L题(错排组合数)

    L - 计数,排列 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   Descrip ...

随机推荐

  1. Change ugly fonts in Firefox (KDE)

    When you use KDE the fonts in Firefox are ugly. This is the fix: Open with Kate: /home/yourusername/ ...

  2. div+css模式编写html静态网页例子_仿照网页制作

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. [已解决问题] An error occurred while automatically activating bundle com.android.ide.eclipse.adt

    可以参见stackoverflow的解决方案:http://stackoverflow.com/questions/16974349/an-error-occurred-while-automatic ...

  4. lightoj 1004 dp:数字三角形

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1004 #include <cstdio> #include <cst ...

  5. windows下python安装paramiko

    Python中使用SSH需要用到OpenSSH,而OpenSSH依赖于paramiko模块,而paramiko模块又依赖于pycrypto模块,因此要在Python中使用SSH,则需要先安装模块顺序是 ...

  6. inux常用命令大全

    系统信息 arch 显示机器的处理器架构(1) uname -m 显示机器的处理器架构(2) uname -r 显示正在使用的内核版本 dmidecode -q 显示硬件系统部件 - (SMBIOS ...

  7. memkeys 安装时遇到的问题及解决办法

    某天由于某需要,安装tumblr的开源工具memkeys .但还是一如既往地不是一帆风顺. 在./configure 时出现如下错误信息: configure.in:14: error: possib ...

  8. C#修饰符

    声明类的顺序: 访问修饰符+类修饰符 +class+类名 { 成员修饰符+ 成员类型 +成员名称; } C#中类及类型成员权限访问修饰符有以下四类:public,private,protected,i ...

  9. uva:10340 - All in All(字符串匹配)

    题目:10340 - All in All 题目大意:给出字符串s和t,问s是否是t的子串.s若去掉某些字符能和t一样,那么t是s的子串. 解题思路:匹配字符.t的每一个字符和s中的字符匹配.注意这里 ...

  10. [PWA] 17. Cache the photo

    To cache photo, You need to spreate cache db to save the photo. So in wittr example, we cache the te ...