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
Post a Comment