素数筛选 素数分解

来源:互联网 发布:去日本带mac好吗 编辑:程序博客网 时间:2024/05/22 06:17
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].
Example

inputoutput

3 7
4
2 2
1
77 1010
924

/*题目收获:这道题就是反向求一个数是个合数,并且他的因子的个数是素数,那么这道题首先要解决的就是如何求一个合数的因子个数这里我们有一个现成的简单方法可以使用:我们知道任何一个合数都可以被素数分解,也就是可以被分解成素数相乘的形式,并且这个形式是唯一的,那么我们可以假设C=p1^a*p2^b*p3^c.....,(p1,p2,p3...都是素数),那么C的因子个数为(a+1)*(b+1)*(c+1)...,题目要求是因子数目是素数,那么也就是(a+1)*(b+1)*(c+1)...是素数,但从这个公式我们就可以很清楚的看到,只要有两个分式相乘那么就已经不是素数了,所以这里必须只有一个分式,也就是(a+1),(b+1),(c+1)...只能出现一个,也就是说C=p1^a*p2^b*p3^c.....只能有一个p才满足题目条件,也就是说,C=p^n,注意这里n!=1,这个就不用解释了。这个C的范围是L-R,R的极限是10^12,因为n>=2,所以p<=10^6,所以在这里我们只需要在2-10^6区间里素数打表即可。题目至此就可以顺利解决。*/#include<bits/stdc++.h>#define ll long longusing namespace std;const int maxn=1000010; ll p[maxn]; //这个数组存储2-10^6的素数ll prim[maxn]; //prim[i]==1代表i是个合数。ll tot=1,L,R;void prime();ll solve(ll s){ll res=s;int i,j;for(i=1;i<tot;i++){ll t=(ll)p[i]*p[i];ll k=2;for(;t<=s;t*=p[i],k++){if(!prim[k+1])res--;}}return res;}int main(){prime();while(~scanf("%lld %lld",&L,&R)){ll ans=solve(R)-solve(L-1);  L-1小技巧 printf("%lld\n",ans);} return 0; }   void prime(){ll i,j;tot=1;memset(p,0,sizeof(p));memset(prim,0,sizeof(prim));for(i=2;i<=maxn;i++){if(prim[i]==0){p[tot++]=i;for(j=i*2;j<=maxn;j+=i){prim[j]=1;}}} }   


0 0