Uva 10539 - Almost Prime Numbers Solution



Problem Link : 10539 - Almost Prime Numbers 

Solution in C++: 

///La ilaha illellahu muhammadur rasulullah
///******Bismillahir-Rahmanir-Rahim******///
///Abul Hasnat  Tonmoy
///Department of CSE,23rd batch
///Islamic University,Bangladesh
///**********ALLAH IS ALMIGHTY************///
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mx 10000009
ll prime[mx],i,j;
void sieve()
{
    memset(prime,truesizeof(prime));
    for(ll i = 4; i <mx; i += 2)
    {
        prime[i] = false;
    }
    prime[0] = prime[1] = false;
    for(ll i = 3; i * i < mx; i+=2)
    {
        if(prime[i])
        {
            for(ll j = i * i; j < mx; j += (2 * i))
            {
                prime[j] = false;
            }
        }
    }
}
vector<ll>v;
void preCal()
{
    for(ll i = 2; i < mx; i++)
    {
        if(prime[i])
        {
            for(ll j = i * i; j <= 1000000000000; j*=i)
            {
                v.push_back(j);
            }
        }
    }
    sort(v.begin(),v.end());
}

int main()
{
    sieve();
    preCal();
     ll t,a,b,x,y,r;
     cin>>t;
    while(t--)
    {
        cin>>a>>b;
        x=lower_bound(v.begin(),v.end(),a)-v.begin();
        y=lower_bound(v.begin(),v.end(),b)-v.begin();
        cout<<y-x<<endl;
    }
}




No comments

Most View Post

Recent post

Codeforces Round 971 (Div. 4) 2009C. The Legend of Freya the Frog Solution

  Problem Link    https://codeforces.com/contest/2009/problem/C S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. h &g...

Powered by Blogger.