Uva 10539 Almost Prime Numbers
/*
Uva 10539 - Almost Prime Number
Verdict :: Accepted
Time:: 0.066
Solving Tips :: http://psshi.blogspot.com/2015/10/almost-prime-numbers.html
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <string>
#include <vector>
#include <cmath>
#include <cctype>
#include <sstream>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <algorithm>
#define sf scanf
#define pf printf
#define sfint scanf ("%d %d",&a,&b)
#define sfl scanf ("%ld %ld",&a,&b)
#define sfll scanf ("%lld %lld",&a,&b)
#define sfd scanf ("%lf %lf",&a,&b)
#define sff scanf ("%f %f",&a,&b)
#define loop(i,n) for(i=0;i<n;i++)
#define LL long long
#define L long
#define nl puts("")
#define MX 10000005
#define N 100
#define MOD 10000000007
#define pb push_back
#define pi acos(-1.0)
#define sz size()
#define gc getchar ()
#define ps push
#define clr clear
#define bn begin()
#define ed end()
using namespace std;
bool flag[MX]; vector <LL> p; LL plen;
void prime()
{
LL i,j,qrt;
flag[0]=flag[1]=true;
for(i=4;i<=MX;i+=2) flag[i]=true;
qrt=(long) sqrt(double(MX));
p.pb(2);
for(i=3;i<=qrt;i+=2)
{
if(!flag[i])
{
for(j=i*i;j<=MX;j+=i+i)
{
flag[j]=true;
}
}
}
for(i=3;i<=MX;i+=2)
{
if(!flag[i])
{
p.pb(i);
}
}
plen=p.size();
}
int main()
{
LL i,j,cnt,x,y,t,z;
prime(); //loop(i,plen)cout << p[i] << " ";
vector<LL> almost;
for(i=0;i<MX;i++)
{
if(!flag[i])
{
//for(j=p[i]*p[i];j<MX;j*=p[i]) almost.pb(j);
for(j=i*i;j<1000000000005;j*=i) // **** Care about that range ! i have got so much WA for that..
{
almost.pb(j);
//st.insert(j);
}
}
}
sort(almost.begin(),almost.end());
LL almostlen=almost.size();
//loop(i,almostlen) cout << almost[i] << " ";
//for(it=st.begin(); it!=st.end();++it) cout << *it << " ";
sf("%lld",&t);
while(t--)
{
sf("%lld %lld",&x,&y);
cnt=0;i=0;
while(i < x)
{
if(almost[i]>=x)
{
z=i; break;
}
i++;
}
for(;z<almostlen and almost[z]<=y;z++)
{
//if(almost[z]<=y) cnt+=1;
//else break;
cnt+=1;
}
pf("%lld\n",cnt);
almost.clear();
}
return 0;
}
Uva 10539 - Almost Prime Number
Verdict :: Accepted
Time:: 0.066
Solving Tips :: http://psshi.blogspot.com/2015/10/almost-prime-numbers.html
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <string>
#include <vector>
#include <cmath>
#include <cctype>
#include <sstream>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <algorithm>
#define sf scanf
#define pf printf
#define sfint scanf ("%d %d",&a,&b)
#define sfl scanf ("%ld %ld",&a,&b)
#define sfll scanf ("%lld %lld",&a,&b)
#define sfd scanf ("%lf %lf",&a,&b)
#define sff scanf ("%f %f",&a,&b)
#define loop(i,n) for(i=0;i<n;i++)
#define LL long long
#define L long
#define nl puts("")
#define MX 10000005
#define N 100
#define MOD 10000000007
#define pb push_back
#define pi acos(-1.0)
#define sz size()
#define gc getchar ()
#define ps push
#define clr clear
#define bn begin()
#define ed end()
using namespace std;
bool flag[MX]; vector <LL> p; LL plen;
void prime()
{
LL i,j,qrt;
flag[0]=flag[1]=true;
for(i=4;i<=MX;i+=2) flag[i]=true;
qrt=(long) sqrt(double(MX));
p.pb(2);
for(i=3;i<=qrt;i+=2)
{
if(!flag[i])
{
for(j=i*i;j<=MX;j+=i+i)
{
flag[j]=true;
}
}
}
for(i=3;i<=MX;i+=2)
{
if(!flag[i])
{
p.pb(i);
}
}
plen=p.size();
}
int main()
{
LL i,j,cnt,x,y,t,z;
prime(); //loop(i,plen)cout << p[i] << " ";
vector<LL> almost;
for(i=0;i<MX;i++)
{
if(!flag[i])
{
//for(j=p[i]*p[i];j<MX;j*=p[i]) almost.pb(j);
for(j=i*i;j<1000000000005;j*=i) // **** Care about that range ! i have got so much WA for that..
{
almost.pb(j);
//st.insert(j);
}
}
}
sort(almost.begin(),almost.end());
LL almostlen=almost.size();
//loop(i,almostlen) cout << almost[i] << " ";
//for(it=st.begin(); it!=st.end();++it) cout << *it << " ";
sf("%lld",&t);
while(t--)
{
sf("%lld %lld",&x,&y);
cnt=0;i=0;
while(i < x)
{
if(almost[i]>=x)
{
z=i; break;
}
i++;
}
for(;z<almostlen and almost[z]<=y;z++)
{
//if(almost[z]<=y) cnt+=1;
//else break;
cnt+=1;
}
pf("%lld\n",cnt);
almost.clear();
}
return 0;
}
Comments
Post a Comment