lightoj 1197 奇怪筛法

奇怪筛法

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

Submit Status

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看成0,,作为起始点,只筛一个区间的素数个数。

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <sstream>
#define N 100090
using namespace std;
typedef long long ll;

ll pri[N];
int vis[N];
int cnt;
int f[N];

void eulerpri()
{
    cnt=0;
    for(ll i=2;i<=70000;i++)
    {
        if(vis[i]==0) pri[cnt++]=i;
        for(int j=0;j<cnt && i*pri[j]<=70000;j++)
        {
            vis[i*pri[j]]=1;
            if(i%pri[j]==0) break;
        }
    }
}

int main()
{

    //cout<<(77086800/209475)<<endl;
    ll a,b;
    int T;

    eulerpri();

   scanf("%d",&T);

   for(int ca=1;ca<=T;ca++)
   {
       scanf("%lld%lld",&a,&b);
       printf("Case %d: ",ca);

//       for(int i=0;i<=50;i++)
//       cout<<pri[i]<<" "<<endl;

       int n=b-a;
       memset(f,0,sizeof f);//由于a,b比较大,为了方便存储,把a看成0位置

       for(int i=0;pri[i]*pri[i]<=b && i<cnt;i++)
       {
           int j=0;//用来存标号

          j=(a/pri[i])*pri[i];//找a到b中第一个要被标记的数
          if(j<a) j+=pri[i];//如果小于a,那么在前进一个素数周期
          j-=a;//存的是下标

           for(;j<=n;j+=pri[i])
           {
               if(j+a==pri[i])continue;//如果第一个数是该素数本身,那么不标记
               f[j]=1;
           }
       }

       int ans=0;
       for(int i=0;i<=n;i++)
       {
           if(f[i]==0)
           {
               ans++;
           }
       }

        if(a==1)
        ans--;
        printf("%d\n",ans);

   }

    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-01 22:45:22

lightoj 1197 奇怪筛法的相关文章

LightOJ 1197 Help Hanzo(区间素数筛法)

#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<cmath> using namespace std; #define maxn 50000 int vis[maxn], isprime[5200], num[100005], k; void prime()//只需要把[1,sqrt(2^31)]之间的素数筛选出来就ok了. {

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

LightOJ 1197 Help Hanzo(区间素数筛选)

水题 有t组数据 每组数据有a b 两个数 求a b之间有多少个素数 打表筛选即可 思路:先打一个素数表 找到第一个大于a的素数的倍数j 从j开始筛 筛到b为止 用flag标记 然后筛第二个素数 一直筛到sqrt(b)为止 剩下的就都是素数了 水题 #include <iostream> #include <math.h> #include <stdio.h> #include <string.h> #include <algorithm> #i

LightOj 1197 Help Hanzo (区间素数筛选)

题目大意: 给出T个实例,T<=200,给出[a,b]区间,问这个区间里面有多少个素数?(1 ≤ a ≤ b < 231, b - a ≤ 100000) 解题思路: 由于a,b的取值范围比较大,无法把这个区间内的所以素数全部筛选出来,但是b-a这个区间比较小,所以可以用区间素数筛选的办法解决这个题目. 代码: 1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<

B - Help Hanzo (LightOJ - 1197)

- 题目大意 在一个区间中去寻找素数的个数. - 解题思路 由于a,b的取值范围比较大,无法把这个区间内的所以素数全部筛选出来,但是b-a这个区间比较小,所以可以用区间素数筛选的办法解决这个题目. - 代码 #include<iostream> #include<cmath> #include<cstring> using namespace std; const int MAX = 1e6 + 5; bool vis[MAX],vis2[MAX]; int cnt =

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

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

kuangbin 带你飞 数学基础

模版整理: 晒素数 void init() { cas = 0; for (int i = 0 ; i < MAXD ; i++) is_prime[i] = true; is_prime[0] = is_prime[1] = false; for (int i = 2 ; i < MAXD ; i++) { if (is_prime[i]) { prime[cas++] = i; for (int j = i + i ; j < MAXD ; j += i) is_prime[j] =

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