URAL2070Interesting Numbers(数学题)

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=2070


2070. Interesting Numbers

Time limit: 2.0 second
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 [ 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].

Samples

input output
3 7
4
2 2
1
77 1010
924


分析:

素数是满足两个条件的数,因为素数的约数只有本身和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;
}











评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值