UVA

来源:互联网 发布:淘宝联华文具 编辑:程序博客网 时间:2024/06/09 19:15

题解:

求区间[l,r]内这个数不是素数且素因子只有一个的个数。用筛法求素数将素数筛出来,然后然后将一个素数累乘存入容器中,然后用二分求区间内的个数。

Almost prime numbers are the non-prime numbers which are divisible by only a single prime number.In this problem your job is to write a program which finds out the number of almost prime numberswithin a certain range.

Input

First line of the input file contains an integerN (N600) which indicates how many sets of inputsare there. Each of the nextN lines make a single set of input. Each set contains two integer numberslowand high(0 < lowhigh <1012).

Output

For each line of input except the first line you should produce one line of output. This line containsa single integer, which indicates how many almost prime numbers are within the range (inclusive)low...high.

Sample Input

3
1 101 2015

Sample Output

341 

#include <iostream>#include <stdio.h>#include <cmath>#include <algorithm>#include <iomanip>#include <cstdlib>#include <string>#include <memory.h>#include <vector>#include <queue>#include <stack>#include <map>#include <set>#include <ctype.h>#include<time.h>#define INF 1000000using namespace std;bool _prime[1000010];long long primes[78500];vector<long long> ans;int cnt=0;void make_prime(){    memset(_prime,false,sizeof(_prime));    for(int i=2;i<=1000;i++)    {        if(!_prime[i])        {            for(int j=i*i;j<=1000005;j+=i)            {                _prime[j]=true;            }        }    }    for(int i=2;i<=1000005;i++)    {        if(!_prime[i])            primes[++cnt]=i;    }}long long find(long long n,int a,int b){    if(n<ans[0]) return 0;    if(n>ans[ans.size()-1]) return ans.size();    int mid=(a+b)/2;    if(ans[mid]==n) return mid;    if(a+1==b) return mid+1;    if(n>ans[mid]) return find(n,mid,b);    else return find(n,a,mid);}int main(){    make_prime();    for(int i=1;i<=cnt;i++){        long long tmp=primes[i]*primes[i];        while(tmp<=1000000000000LL){            ans.push_back(tmp);            tmp*=primes[i];        }    }    sort(ans.begin(),ans.end());    int t;    cin>>t;    long long L,R;    while(t--){        cin>>L>>R;        int l1=find(L,0,ans.size());        int r1=find(R,0,ans.size());        cout<<r1-l1<<endl;    }    return 0;}