题目链接:http://acm.timus.ru/problem.aspx?space=1&num=2070
2070. Interesting Numbers
Time limit: 2.0 second
Memory limit: 64 MB
Memory limit: 64 MB
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 [
L;
R] 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 [
L;
R].
Samples
input | output |
---|---|
3 7 | 4 |
2 2 | 1 |
77 1010 | 924 |
题意:
在[L, R]之间
求:
1、x是个素数
2、因子个数是素数
同时满足两个条件,或者同时不满足两个条件的数的个数!
分析:
素数是满足两个条件的数,因为素数的约数只有本身和1,所以约数个数为2,也是素数。偶数是同时不满足两个条件的数,因为它的约数个数为偶数个(不是由单素数组成的偶数即2的n次方)。剩下的只有奇数了。奇数中,如果一个数是某个素数的n次方,那么它的因子个数就为n+1,如果n+1是不为素数,那么这个奇数就是同时不满足两个条件的。
综合上述分析,满足题目条件的数就是在[L, R]之间的所有素数、偶数、以及值为素数的n次方且n+1不为素数的所有个数和。那么只要计算出奇数中不符合题目要求的就行,也就是素数的n次方,且n+1为素数的数的个数。
知识点:一个由多个质因数组合p1^a*p2^b*p3^c....组成的合数它的因子数(a+1)*(b+1)(c+1)。
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn = 1e6+10;
typedef long long ll;
ll prim[maxn], p[maxn], tol = 0;
void init()
{
for(ll i = 2; i <= 1e6; i++)
if(prim[i] == 0)
{
p[tol++] = i;
for(ll j = i*i; j <= 1e6; j += i)
prim[j] = 1;
}
}
ll solve(ll x)
{
ll cnt = 0;
for(int i = 0; i < tol; i++)
{
if(p[i]*p[i] > x)
break;
ll tmp = p[i]*p[i];
for(int j = 2; tmp <= x; j++)
{
if(!prim[j+1])
cnt++;
tmp *= p[i];
}
}
return x - cnt;
}
int main()
{
init();
ll l, r;
while(scanf("%I64d%I64d", &l, &r) != EOF)
{
ll ans = solve(r) - solve(l-1);
printf("%I64d\n", ans);
}
return 0;
}