light_oj 1197 区间素数筛

light_oj 1197 区间素数筛

M - Help Hanzo

Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

Submit Status Practice LightOJ 1197

Description

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

题意:给定a,b,求区间[a,b]中的素数个数。

思路:先常规的素数筛法筛出[1,10^6]的素数,再根据[1,10^6]的素数筛出[a,b]中的素数。

一开始用map超时了,改成set也超时了。。。最后改成常规的bool数组就过了,看来map和set的常数还是有点大。。像这种下标较大但下标范围较小的还是用常规的数组使下标加上一个常数比较好。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<string>
#include<math.h>
#include<cctype>

using namespace std;

typedef long long ll;
const int maxn=1000100;
const int INF=(1<<29);
const double EPS=0.0000000001;
const double Pi=acos(-1.0);

int T;
ll a,b;
bool isprime[maxn];
vector<ll> prime;
bool isP[maxn];

void play_prime()
{
    memset(isprime,1,sizeof(isprime));
    isprime[1]=0;
    for(int i=2;i<maxn;i++){
        if(!isprime[i]) continue;
        for(int j=i*2;j<maxn;j+=i){
            isprime[j]=0;
        }
    }
    for(int i=1;i<maxn;i++){
        if(isprime[i]) prime.push_back(i);
    }
}

int main()
{
    cin>>T;
    int tag=1;
    play_prime();
    while(T--){
        scanf("%lld%lld",&a,&b);
        if(a<=1) a=2;
        memset(isP,1,sizeof(isP));
        for(int i=0;i<prime.size()&&prime[i]*prime[i]<=b;i++){
            ll p=prime[i];
            for(ll j=a+(p-a%p)%p;j<=b;j+=p){
                if(j!=p) isP[j-a]=0;
            }
        }
        int ans=0;
        for(ll i=a;i<=b;i++){
            if(isP[i-a]) ans++;
        }
        printf("Case %d: %d\n",tag++,ans);
    }
    return 0;
}

时间: 2024-10-09 19:18:39

light_oj 1197 区间素数筛的相关文章

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 angr

区间素数筛模版

区间素数筛模版 筛出区间[a,b]的素数.(b-a<=10000,1<=a<=b<=2^31) 存在P中,素数个数即为P的size(). ll a,b; bool isprime[maxn]; vector<ll> prime; bool isP[maxn]; vector<ll> P; void play_prime() { memset(isprime,1,sizeof(isprime)); isprime[1]=0; for(int i=2;i<

区间素数筛

题目描述 A positive integer is called a "prime-factor prime" when the number of its prime factors is prime. For example, 12 is a prime-factor prime because the number of prime factors of 12=2×2×3 is 3, which is prime. On the other hand, 210 is not a

poj 2689 区间素数筛

The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured the interest of number theoreticians for thousands of years is the question of primality. A prime number is a number that is has no prop

lightoj1197区间素数筛

模板题,不过好像有点问题,当a==1的时候,答案把一也算进去了,要减去 #include<map> #include<set> #include<cmath> #include<queue> #include<stack> #include<vector> #include<cstdio> #include<cassert> #include<iomanip> #include<cstdlib

Light oj 1197 - Help Hanzo (素数筛技巧)

题目链接:http://lightoj.com/volume_showproblem.php?problem=1197 给你a和b求a到b之间的素数个数. 先在小区间素数筛,大区间就用类似素数筛的想法,把a到b之间不是素数的标记出来.因为b-a最多1e5的大小,所以每组数据的时间复杂度最多就o(1e5 log1e5). 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using names

埃氏筛法(素数筛)

埃式筛法:给定一个正整数n(n<=10^6),问n以内有多少个素数? 做法:做法其实很简单,首先将2到n范围内的整数写下来,其中2是最小的素数.将表中所有的2的倍数划去,表中剩下的最小的数字就是3,他不能被更小的数整除,所以3是素数.再将表中所有的3的倍数划去……以此类推,如果表中剩余的最小的数是m,那么m就是素数.然后将表中所有m的倍数划去,像这样反复操作,就能依次枚举n以内的素数,这样的时间复杂度是O(nloglogn). 题解:如果要是按照一个一个判断是否是素数然后把ans+1,时间复杂度

浅谈线性素数筛

素数筛的用处还是蛮多的,有很多和素数有关的题都要用到素数筛,所以有一个高效的筛法自然是非常好的吖,普通筛法(暴力筛法)就不说了,因为有了高效的也没人在会用普通筛法了吧. 线性素数筛是用每一个合数的最小的质因数筛掉它,所以时间复杂度保证是线性的. 模板:https://www.luogu.org/problemnew/show/P3383 代码: 1 #include<iostream> 2 #include<cstdio> 3 using namespace std; 4 int

LightOJ 1197 LightOJ 1197(大区间素数筛选)

http://lightoj.com/volume_showproblem.php?problem=1197 题目大意: 就是给你一个区间[a,b]让你求这个区间素数的个数 但a.b的值太大没法直接进行素数筛选(没法开那么大的数组),我们可以将a当做0,将b当做b-a 这样求[a,b]之间就变成了求[0, b - a]之间,这样就可以开数组来筛选 下图是代码式子j = j + prime[i] - a % prime[i]的由来 #include<stdio.h> #include<ma