杭电(hdu)ACM 4548 美素数

美素数

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)

Total Submission(s): 4482    Accepted Submission(s): 1524

Problem 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>
#include <cstring>
using namespace std;

bool prime[1000001];
bool beauty[1000001];
int dp[1000001];

void findprime()
{
    memset(prime,true,sizeof(prime));
    memset(beauty,true,sizeof(beauty));
    prime[0]=prime[1]=false;
    beauty[0]=beauty[1]=false;
    for(int i=2;i<=1000;i++)                   //素数筛选法核心代码区
    {
        for(int j=2*i;j<=1000000;j+=i)
        {
            prime[j]=false;
            beauty[j]=false;
        }
    }                                         //素数筛选法核心代码区
}

void beautyprime()
{
    int sum,k;
    for(int i=2;i<=1000000;i++)
    {
        sum=0;k=i;
        if(prime[i]==true)
        {
            while(k!=0)
            {
                sum+=k%10;
                k/=10;
            }
            if(prime[sum]!=true)
            {
                beauty[i]=false;
            }
        }
    }
}

void DP()                               //动态规划思想,dp[i]即1到i素数的个数
{
    memset(dp,0,sizeof(dp));
    for(int i=1;i<=1000000;i++)
    {
        if(beauty[i]==true)
        {
            dp[i]=dp[i-1]+1;
        }
        else dp[i]=dp[i-1];
    }
}

int main()
{
    int a,b,T,_count;
    findprime();
    beautyprime();
    DP();
    while(scanf("%d",&T)!=EOF)
    {
        _count=0;
        while(T--)
        {
            _count++;
            scanf("%d%d",&a,&b);
            printf("Case #%d: %d\n",_count,dp[b]-dp[a-1]);
        }
    }
    return 0;
}

时间: 2024-10-09 11:48:49

杭电(hdu)ACM 4548 美素数的相关文章

杭电 HDU ACM 1397 Goldbach&#39;s Conjecture

Goldbach's Conjecture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4976    Accepted Submission(s): 1901 Problem Description Goldbach's Conjecture: For any even number n greater than or equal

杭电 HDU ACM 5186 zhx&#39;s submissions

zhx's submissions Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1892    Accepted Submission(s): 507 Problem Description As one of the most powerful brushes, zhx submits a lot of code on many

杭电 HDU ACM 1025 Constructing Roads In JGShining&#39;s Kingdom

Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 17732    Accepted Submission(s): 5023 Problem Description JGShining's kingdom consists of 2n(n is no mo

杭电HDU ACM Uncle Tom&#39;s Inherited Land*(二分图匹配 建模)

Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2496    Accepted Submission(s): 1028 Special Judge Problem Description Your old uncle Tom inherited a piece of land f

杭电 HDU ACM 圆桌会议

圆桌会议 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3356    Accepted Submission(s): 2351 Problem Description HDU ACM集训队的队员在暑假集训时经常要讨论自己在做题中遇到的问题.每当面临自己解决不了的问题时,他们就会围坐在一张圆形的桌子旁进行交流,经过大家的讨论后一般没有

杭电 HDU ACM 1046 Tempter of the Bone

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 83458    Accepted Submission(s): 22740 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

杭电 HDU ACM 1496 Equations

Equations Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6065    Accepted Submission(s): 2455 Problem Description Consider equations having the following form: a*x1^2+b*x2^2+c*x3^2+d*x4^2=0 a,

杭电 HDU ACM 1283 最简单的计算机

最简单的计算机 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5238    Accepted Submission(s): 2967 Problem Description 一个名叫是PigHeadThree的研究组织设计了一台实验用的计算机,命名为PpMm.PpMm只能执行简单的六种命令A,B,C,D,E,F:只有二个内存M1,M

杭电 HDU ACM 1213 How Many Tables

How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 17049    Accepted Submission(s): 8349 Problem Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinn