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, …
题意
求区间[a,b]内的素数,标准模板题目。
用做参考模板
代码

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define LL long long
bool prim[100010];
bool prims[100010];
void segment_sieve(LL a,LL b)
{
    for(int i=0; (LL)i*i<=b; i++) prims[i]=1;
    for(int i=0; i<=b-a; i++) prim[i]=1;
    for(int i=2; (LL)i*i<=b; i++)
    {
        if(prims[i])
        {
            for(int j=2*i; (LL)j*j<=b; j+=i)
            {
                prims[j]=0;
            }
            for(LL j=max(2LL,(a+i-1)/i)*i; j<=b; j+=i)
            {
                prim[j-a]=0;
            }
        }
    }
}
int main()
{
    LL a,b;
    int t,i,sum,cas=1;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lld%lld",&a,&b);
        segment_sieve(a,b);
        sum=0;
        for(i=0; i<=b-a; i++)
        {
            if(prim[i])
                sum++;
        }
        if(a==1)sum--;  //特殊情况
        printf("Case %d: %d\n",cas++,sum);
    }
    return 0;
}

还有素数打表模板:
10^7可用

#include<stdio.h>
#include<string.h>
#include<algorithm>
#define ll long long
#define N 1000001
int f[N],prim[N];
int k=0;
void find_prim()    
{
    k=0;
    int c;
    memset(f,0,sizeof(f));
    f[0]=f[1]=1;
    for(int i=2; i<=1000000; i++)   //此处不要用N,用具体的数  
    {
        c=i;
        if(!f[i])
        {
            prim[k++]=c;
            for(ll j=i<<1; j<=1000000; j+=i)  //这里也是,还有要把N开的大点,防止这里越界
                f[j]=1;
        }
    }
}
int main()
{
    int i,j,n,m;
    find_prim();
    for( i=0;i<=100;i++)
        printf("%d\n",prim[i]);
}

素数打表,欧拉筛法
线性筛选法,高效
o(n)

#include<stdio.h>
#include<string.h>
#define N 100000000
int p[N+5],prim[N+5];
void prime()
{
    int i,j,k=0;
    memset(p,0,sizeof(p));
    p[0]=p[1]=1;
    for(i=2;i<=N;i++)
    {
        if(!p[i])
            prim[k++]=i;
        for(j=0; j<k&&i*prim[j]<=N ;j++)
        {
            p[i*prim[j]]=1;
            if(i%prim[j]==0)
                break;
        }
    }
}
int main()
{
    prime();
    for(int i=0;i<=100;i++)
        printf("%d\n",prim[i]);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值