UVA 10539 Almost Prime Numbers(唯一分解定理)

9 篇文章 0 订阅

题意:在区间[l, u]内找出有多少只有一个素因子的数, 且本身不是素数。

解题思路:直接使用唯一分解定理会导致超时, 所以可以将每个素因子累乘,在[l, u]内寻找有多少个,需要注意的是因为要用到int与long long 运算所以要把素数表开成long long

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 nds out the number of almost prime numbers
within a certain range.
Input
First line of the input le contains an integer N (N 600) which indicates how many sets of inputs
are there. Each of the next N lines make a single set of input. Each set contains two integer numbers
low and high (0 < low high < 1012).
Output
For each line of input except the rst line you should produce one line of output. This line contains
a single integer, which indicates how many almost prime numbers are within the range (inclusive)
low : : : high.
Sample Input
3
1 10
1 20
1 5
Sample Output
3
4
1


#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cctype>
#include<list>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>

using namespace std;

#define FOR(i, s, t) for(int i = (s) ; i <= (t) ; ++i)
#define REP(i, n) for(int i = 0 ; i < (n) ; ++i)

int buf[10];
inline long long read()
{
    long long x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-')f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}

inline void writenum(int i)
{
    int p = 0;
    if(i == 0) p++;
    else while(i)
        {
            buf[p++] = i % 10;
            i /= 10;
        }
    for(int j = p - 1 ; j >= 0 ; --j) putchar('0' + buf[j]);
}
/**************************************************************/
#define MAX_N 1000050
const int INF = 0x3f3f3f3f;
int isprime[MAX_N];
long long prime[MAX_N];
int cnt = 0;
long long l, u;
void make_prime()
{
    cnt = 0;
    memset(isprime, 1, sizeof(isprime));
    isprime[1] = 0;
    for(long long i = 2 ; i < MAX_N ; i++)
    {
        if(isprime[i])
        {
            prime[cnt++] = i;
        }
        for(long long j = i * i ; j < MAX_N ; j += i)
        {
            isprime[j] = 0;
        }
    }
}
int main()
{
    int t = read();
    make_prime();
    while(t--)
    {
        l = read();
        u = read();
        long long ans = 0;
        for(int i = 0 ; i < cnt ; i++)
        {
            for(long long j = prime[i] * prime[i] ; j <= u ; j *= prime[i])
            {
                if(j >= l)
                {
                    ans++;
                }
            }
        }
        printf("%lld\n", ans);
    }

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值