LightOJ - 1197 div1&&div2(区间素数筛 模板)

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]内素数的个数,a,b很大,但是b-a在可接受的范围之内,使用区间素数筛。

先筛出b-a个素数,然后对于每个筛出来的素数在[a,b]中第一次出现的位置向后筛,筛完后得到素数的个数。

(先贴个板子 待会回来加注释qwq) 我没有咕!

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int N=1e5+10;
bool vis[N];
int pri[N];
int tot;
bool ans[N];

void init()///埃式筛/欧拉筛 预处理出[1,b-a]中的素数 [a,b]中的合数不会超过[1,b-a]中最大素数的2倍
{
    memset(vis,0,sizeof(vis));
    vis[0]=vis[1]=1;
    tot=0;
    for(int i=2;i<N;i++)
    {
        if(!vis[i])
        {
            pri[++tot]=i;
            for(int j=i+i;j<N;j+=i)
                vis[j]=1;
        }
    }
}

int solve(ll a, ll b)
{
    memset(ans,0,sizeof(ans));
    for(int i=1; i<=tot && 1ll*pri[i]*pri[i] <= b; i++)
    {
        ll s = (a+pri[i]-1) / pri[i];///找到大于a的第一个该素数的倍数(a是pri[i]的几倍 向上取整)
        if(s < 2)///至少是素数的2倍
            s = 2;
        s *= pri[i];
        for(; s <= b; s += pri[i])
            ans[s - a]=1;///b很大 数组开不下 为了防止数组越界 进行下标偏移
    }
    int res = 0;
    for(int i = 0; i <= b-a; i++)///下标偏移后 [0,b-a]即对应[a,b]
    {
        if(!ans[i])///是素数
        {
            res++;
            cout<<i<<'\n';
        }
    }
    if (a == 1)///最小的素数是2 a是1的时候没有删掉1
        res--;
    return res;
}
int main()
{
    init();
    int t,kcase=0;
    ll a,b;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lld%lld",&a,&b);
        int res=solve(a,b);
        cout<<"Case "<<++kcase<<": "<<res<<'\n';
    }
    return 0;
}

 

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
# 春招至今,你的战绩如何? #
5015次浏览 47人参与
# 你的实习产出是真实的还是包装的? #
1103次浏览 27人参与
# 米连集团26产品管培生项目 #
4127次浏览 198人参与
# 军工所铁饭碗 vs 互联网高薪资,你会选谁 #
6907次浏览 37人参与
# 简历第一个项目做什么 #
31251次浏览 312人参与
# 当下环境,你会继续卷互联网,还是看其他行业机会 #
186349次浏览 1115人参与
# MiniMax求职进展汇总 #
22891次浏览 293人参与
# 面试紧张时你会有什么表现? #
30371次浏览 188人参与
# 简历中的项目经历要怎么写? #
309379次浏览 4152人参与
# 网易游戏笔试 #
6304次浏览 83人参与
# 职能管理面试记录 #
10687次浏览 59人参与
# 把自己当AI,现在最消耗你token的问题是什么? #
6850次浏览 154人参与
# 从哪些方向判断这个offer值不值得去? #
56698次浏览 357人参与
# 腾讯音乐求职进展汇总 #
160394次浏览 1105人参与
# 小红书求职进展汇总 #
226845次浏览 1356人参与
# AI时代,哪些岗位最容易被淘汰 #
62406次浏览 728人参与
# 你怎么看待AI面试 #
179254次浏览 1163人参与
# 正在春招的你,也参与了去年秋招吗? #
362517次浏览 2631人参与
# 你的房租占工资的比例是多少? #
92123次浏览 896人参与
# 机械求职避坑tips #
94396次浏览 567人参与
# 校招笔试 #
466168次浏览 2950人参与
# 面试官最爱问的 AI 问题是...... #
27111次浏览 834人参与
牛客网
牛客网在线编程
牛客网题解
牛客企业服务