1. 程式人生 > >LightOJ-1197 -Help Hanzo (素數)

LightOJ-1197 -Help Hanzo (素數)

原題連結:
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;
}

歡迎評論!