UVA - 10539 Almost Prime Numbers (几乎是素数)

Posted SomnusMistletoe

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA - 10539 Almost Prime Numbers (几乎是素数)相关的知识,希望对你有一定的参考价值。

题意:输入两个正整数L、U(L<=U<1012),统计区间[L,U]的整数中有多少个数满足:它本身不是素数,但只有一个素因子。

分析:

1、满足条件的数是素数的倍数。

2、枚举所有的素数,以及其倍数,将满足条件且小于等于n的个数计算出来,solve(u) - solve(l - 1)即可。

#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
const double eps = 1e-8;
inline int dcmp(double a, double b) {
    if(fabs(a - b) < eps)  return 0;
    return a < b ? -1 : 1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 1e6 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int vis[MAXN];
vector<LL> prime;
void init(){
    for(int i = 2; i < MAXN; ++i){
        if(!vis[i]){
            prime.push_back(i);
            for(int j = 2 * i; j < MAXN; j += i){
                vis[j] = 1;
            }
        }
    }
}
LL solve(LL n){
    LL ans = 0;
    int len = prime.size();
    for(int i = 0; i < len; ++i){
        LL tmp = prime[i] * prime[i];
        if(tmp > n) break;
        while(tmp <= n){
            ++ans;
            tmp *= prime[i];
        }
    }
    return ans;
}
int main(){
    init();
    int T;
    scanf("%d", &T);
    while(T--){
        LL l, u;
        scanf("%lld%lld", &l, &u);
        printf("%lld\n", solve(u) - solve(l - 1));
    }
    return 0;
}

  

以上是关于UVA - 10539 Almost Prime Numbers (几乎是素数)的主要内容,如果未能解决你的问题,请参考以下文章

UVa10539

uva10539

UVA-10539 打表+二分

UVA - 11987 Almost Union-Find[并查集 删除]

UVA11987--Almost Union-Find

UVA11987 Almost Union-Find