LightOJ-1197 -Help Hanzo (素数)

版权声明:欢迎评论与转载,转载时请注明出处! https://blog.csdn.net/wjl_zyl_1314/article/details/84701695

原题链接:
Amakusa, the evil spiritual leader has captured the beautiful princess Nakururu. The reason behind this is he had a little problem with Hanzo Hattori, the best ninja and the love of Nakururu. After hearing the news Hanzo got extremely angry. But he is clever and smart, so, he kept himself cool and made a plan to face Amakusa.
Before reaching Amakusa’s castle, Hanzo has to pass some territories. The territories are numbered as a, a+1, a+2, a+3 … b. But not all the territories are safe for Hanzo because there can be other fighters waiting for him. Actually he is not afraid of them, but as he is facing Amakusa, he has to save his stamina as much as possible.
He calculated that the territories which are primes are safe for him. Now given a and b he needs to know how many territories are safe for him. But he is busy with other plans, so he hired you to solve this small problem!
Input
Input starts with an integer T (≤ 200), denoting the number of test cases.
Each case contains a line containing two integers a and b (1 ≤ a ≤ b < 2^31, b - a ≤ 100000).
Output
For each case, print the case number and the number of safe territories.
Sample Input
3
2 36
3 73
3 11
Sample Output
Case 1: 11
Case 2: 20
Case 3: 4
Note
A number is said to be prime if it is divisible by exactly two different integers. So, first few primes are 2, 3, 5, 7, 11, 13, 17, …
题意:
输入一个区间(a,b),找出区间内素数的个数。
题解:
先素数打表,打在1-10^5,基本就够了,然后判断区间,如果区间在已经打表的范围里当然很好,可以直接在区间里找出素数的个数(upper_bound()函数以及lower_bound()函数作用),若是不在去已打表的范围内,则重新再新的区间里,利用已经大好的表,在去排除掉区间里的非素数。
附上AC代码:

#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define LL long long
const int N=1e6+5;
int prime[N/10];
bool vis[N];
int cnt=0;
void el()//素数打表
{
    int m=sqrt(N+0.5);
    memset(vis,0,sizeof(vis));
    for(int i=2;i<=m;i++)
    {
        if(!vis[i])
        for(int j=i*i;j<=N;j+=i)
        {
            vis[j]=1;
        }
    }
    for(int i=2;i<=N;i++)
    {
        if(!vis[i])
        {
            prime[cnt++]=i;
        }
    }
}
int main()
{
    el();
    int t;
    int a,b;
    scanf("%d",&t);
    for(int cas=1;cas<=t;cas++)
    {
        scanf("%d%d",&a,&b);
        int ans=0;
        if(b<N)//如果区间恰好在打表的范围内,则直接查找就行
        {
            int a1=upper_bound(prime,prime+cnt,b)-prime;//返回的是整型指针,指向第一个大于b的元素的地址
            int b1=lower_bound(prime,prime+cnt,a)-prime;//返回的是整型指针,指向第一个大于或者等于a的元素的地址
            ans=a1-b1;

        }
        else//如果不在打表范围里
        {
            memset(vis,0,sizeof(vis));//重置
            for(int i=0;i<cnt&&prime[i]<a;i++)//在这个区间内,排除掉所有不是素数的元素
            {
                LL k=a/prime[i];
                if(a%prime[i])k++;//找出区间里能被此素数整除的第一个位置
                for(LL j=k*prime[i];j<=b;j+=prime[i])//注意j要用长整型,否则会溢出
                {
                    vis[j-a]=1;//只用记录区间内的非素数
                }
            }
            for(int i=0;i<=b-a;i++)
            {
                if(!vis[i])
                    ans++;
            }
        }
        printf("Case %d: %d\n",cas,ans) ;
    }
    return 0;
}

欢迎评论!

猜你喜欢

转载自blog.csdn.net/wjl_zyl_1314/article/details/84701695