zoukankan      html  css  js  c++  java
  • 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 < 231, 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, ...

     
    分析:b最大为2^31-1,但闭区间[a,b]范围比较小,因此可以先筛出2^16次方的素数,
    因为小于b的合数必有小于√b 的素因子,可以简单证明一下,假设合数x<b,x有素因子k>√b,
    那么x/k<√b,即x/k必有小于√b 的素因子。
     
    #include<cstdio>
    #include<cstring>
    int p[46342],cnt=1;
    int c[46342];
    void prime()
    {
        for(int i=4;i<46342;i+=2) c[i]=1;
        p[0]=2;
        for(int i=3;i<46342;i++)
        {
            if(!c[i])
            {
                p[cnt++]=i;
                for(int j=i+i;j<46342;j+=i)
                c[j]=1;
            }
        }
    }
    
    int s[100011];
    int main()
    {
        int T;
        int cas=0;long long a,b;
        scanf("%d",&T);
        prime();
        while(T--)
        {
            scanf("%lld%lld",&a,&b);
            memset(s,0,sizeof(s));
            //把合数筛出来
            for(int i=0;i<cnt&&p[i]*p[i]<=b;i++)
            {
                long long k=a/p[i];
                long long temp=k*p[i];
                if(temp<a) temp+=p[i];
                if(temp==p[i]) temp+=p[i];
                while(temp<=b)
                {
                    s[temp-a]++;
                    temp+=p[i];
                }
            }
            int ans=0;
            for(int i=0;i<=b-a;i++)
            if(!s[i]) ans++;
            if(a<=1) ans--;//1不是素数
            printf("Case %d: %d
    ",++cas,ans);
        }
        return 0;
    }
    View Code
     
     
     
     
     
  • 相关阅读:
    Pandas: 如何将一列中的文本拆分为多行? | Python
    Python项目实战:福布斯系列之数据采集
    Python: Pandas运算的效率探讨以及如何选择高效的运算方式
    Pandas数据处理实战:福布斯全球上市企业排行榜数据整理
    从历史来看印度的裂痕和隐忧 | 阅览群书
    Python库:序列化和反序列化模块pickle介绍
    无法加载 DLL“SQLite.Interop.DLL”: 找不到指定的模块。 (异常来自 HRESULT:0x8007007E)。
    spring 问题笔记
    java 大量数据处理问题记录
    spring mvc 整合swagger
  • 原文地址:https://www.cnblogs.com/ACRykl/p/8654098.html
Copyright © 2011-2022 走看看