Skip to main content

Light OJ -- 1197(Help Hanzo)

Bismillahir Rahmanir Rahim
///Author: Tanvir Ahmmad
///CSE,Islamic University,Bangladesh

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<sstream>
#include<cmath>
#include<cstring>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<vector>
#include<iterator>
#include <functional> ///sort(arr,arr+n,greater<int>()) for decrement of array
/*every external angle sum=360 degree
  angle find using polygon hand(n) ((n-2)*180)/n*/
///Floor[Log(b)  N] + 1 = the number of digits when any number is represented in base b
using namespace std;
typedef long long ll;

vector<int>prime;
#define mx 100009
void seive()
{
    bool flag[mx+1];
    memset(flag,true,sizeof flag);
    for(ll i=4; i<=mx; i+=2)
        flag[i]=false;
    prime.push_back(2);
    for(ll i=3; i<=mx; i++)
    {
        if(flag[i]==true)
        {
            prime.push_back(i);
            for(ll j=i+i; j<=mx; j+=i)
                flag[j]=false;
        }
    }
}
int segment_seive(ll low,ll high)
{
    bool isPrime[high-low+1];
    memset(isPrime,true,sizeof isPrime);
    if(low==1)
        isPrime[0]=false;
    for(ll i=0; prime[i]*prime[i]<=high && i<prime.size(); i++)
    {
        ll curPrime=prime[i];
        ll base=curPrime*curPrime;
        if(base<low)
            base=floor((low+curPrime-1)/curPrime)*curPrime;
        for(ll j=base; j<=high; j+=curPrime)
            isPrime[j-low]=false;
    }
    int cnt=0;
    for(ll i=0; i<=high-low; i++)
    {
        if(isPrime[i]==true)
            cnt++;
    }
    return cnt;
}
int main()
{
    seive();
    ll low,high,tst,i;
    scanf("%lld",&tst);
    for(i=1;i<=tst;i++)
    {
        scanf("%lld %lld",&low,&high);
        printf("Case %lld: %d\n",i,segment_seive(low,high));
    }
    return 0;
}

Problem Link: https://lightoj.com/problem/help-hanzo


Comments

Popular posts from this blog

Codeforces round 1676(A. Lucky?)

Just count the  first three  &  last three  number ///Bismillahir Rahmanir Rahim ///Author: Tanvir Ahmmad ///CSE,Islamic University,Bangladesh #include< iostream > #include< cstdio > #include< algorithm > #include< string > #include< cstring > #include< sstream > #include< cmath > #include< cstring > #include< vector > #include< queue > #include< map > #include< set > #include< stack > #include< vector > #include< iterator > #include   < functional >   ///sort(arr,arr+n,greater<int>()) for decrement of array /*every external angle sum=360 degree   angle find using polygon hand(n) ((n-2)*180)/n*/ ///Floor[Log(b)  N] + 1 = the number of digits when any number is repres...

Codeforces round 1661(B. Getting Zero)

using   second operation  the answer is maximum is  15  because  2 15 =32768 so we need to  pre-calculate all the illegible input 0 to 32768   using  second operation Then  just increment 1(using first operation)  from input check is it  minimum is not ! ///Bismillahir Rahmanir Rahim ///Author: Tanvir Ahmmad ///CSE,Islamic University,Bangladesh #include< iostream > #include< cstdio > #include< algorithm > #include< string > #include< cstring > #include< sstream > #include< cmath > #include< cstring > #include< vector > #include< queue > #include< map > #include< set > #include< stack > #include< vector > #include< iterator > #include   < functional >   ///sort(arr,arr+n,greater<int...

Codeforces round 1676(E. Eating Queries)

Just use the lower bound & check is it greater than the sum or not Lower bound:https://www.geeksforgeeks.org/upper_bound-and-lower_bound- for -vector- in -cpp-stl/ ///Bismillahir Rahmanir Rahim ///Author: Tanvir Ahmmad ///CSE,Islamic University,Bangladesh #include< iostream > #include< cstdio > #include< algorithm > #include< string > #include< cstring > #include< sstream > #include< cmath > #include< cstring > #include< vector > #include< queue > #include< map > #include< set > #include< stack > #include< vector > #include< iterator > #include   < functional >   ///sort(arr,arr+n,greater<int>()) for decrement of array /*every external angle sum=360 degree   angle find using polygon hand(n) ((n-2)*180...