UVA2070-Interesting Numbers

来源:互联网 发布:ncut算法优劣 编辑:程序博客网 时间:2024/05/21 07:10


Description

Nikolay and Asya investigate integers together in their spare time. Nikolay thinks an integer is interesting if it is a prime number. However, Asya thinks an integer is interesting if the amount of its positive divisors is a prime number (e.g., number 1 has one divisor and number 10 has four divisors).
Nikolay and Asya are happy when their tastes about some integer are common. On the other hand, they are really upset when their tastes differ. They call an integer satisfying if they both consider or do not consider this integer to be interesting. Nikolay and Asya are going to investigate numbers from segment [ LR] this weekend. So they ask you to calculate the number of satisfying integers from this segment.

Input

In the only line there are two integers L and R (2 ≤ L ≤ R ≤ 10 12).

Output

In the only line output one integer — the number of satisfying integers from segment [ LR].

Sample Input

inputoutput
3 7
4
2 2
1
77 1010
924

题意:[ L , R ]区间内,既不是质数,因数个数也不为质数的数的个数

比如10的因数有1 2 5 10

可以考虑约数个数定理

对于一个大于1正整数n可以分解质因数:
 
则n的正约数的个数就是
  
因此可以知道,如果因数个数为质数,那么这个数肯定可以被分解为一个且仅一个质数的n次幂,并且n+1为质数
怎么找这个数呢?
利用筛法打素数类似的想法,用筛法把质数的(质数-1)次幂都打出来(时间久了,我也不太记得了,应该是这样的,如果错了,欢迎指正)
#include<iostream>#include<cstdio>#include<cmath>#include<algorithm>#include<cstring>#define setbitzero(a) (isprime[(a)>>5]&=(~(1<<((a)&31))))#define setbitone(a) (isprime[(a)>>5]|=(1<<((a)&31)))#define ISPRIME(a) (isprime[(a)>>5]&(1<<((a)&31)))using namespace std;const long long maxn = 10000005;int isprime[(maxn>>5)+1];//int prime[maxn]={2,3,5,7,9,11,13,17,19,23,29,31,37,41,43,47,53,59};long long prime[1000005];long long pcount;void initprime(){    int i,j,m;    int t = (maxn>>5)+1;    for(i = 0 ; i <t;i++)        isprime[i] = 2863311530;    prime[0] = 2;    setbitone(2);    setbitzero(1);    m = (int)sqrt(maxn);    for(pcount =1 ,i = 3;i<=m;i+=2)    {        if(ISPRIME(i))        {            for(prime[pcount++]=i,j=i<<1;j<=maxn;j+=i)                setbitzero(j);        }    }    if(!(i&1))++i;    for(;i<=maxn;i+=2)        if(ISPRIME(i))        prime[pcount++] = i;}int f(long long  l,long long r){    int ans = 0;    for(int i = 0 ; i < pcount&&prime[i]<=r;i++)    {        long long tmp = prime[i];        for(int j = 1;j<pcount&&tmp<=r;j++)        {            tmp = prime[i];            for(int k = 0;k<prime[j]-2;k++)            {                tmp *= prime[i];                //cout<<tmp<<"k"<<k<<endl;            }            if(tmp>=l&&tmp<=r)            {//                int flag =1;//                for(int kk = 2;kk<=sqrt(tmp);kk++)//                {//                    if(tmp%kk==0)//                    {//                         flag = 0;break;//                    }//                }//                if(!flag)//                {                      ans++;//               cout<<tmp<<" ";            }        }    }    return ans;}int main(){//    memset()    long long  L,R;    //while(cin>>L>>R)    cin>>L>>R;        initprime();//        for(int i = 0;i<20;i++)//            cout<<prime[i]<<" "<<endl;        cout<<R-L+1-f(L,R)<<endl;    return 0;}







0 0
原创粉丝点击