首页 > 代码库 > uva 10539 - Almost Prime Numbers(数论)
uva 10539 - Almost Prime Numbers(数论)
题目链接:uva 10539 - Almost Prime Numbers
题目大意:给出范围low~high,问说在这个范围内有多少个数满足n=pb,(p为素数).
解题思路:首先处理出1e6以内的素数,然后对于每个范围,用solve(high)?solve(low?1),solve(n)用来处理小于n的满足要求的数的个数。枚举素数,判断即可。
#include <cstdio>
#include <cstring>
typedef long long ll;
const int maxn = 1e6;
ll lower, up, prime[maxn];
int cp, v[maxn];
void primeTable (int n) {
cp = 0;
memset(v, 0, sizeof(v));
for (int i = 2; i <= n; i++) {
if (v[i])
continue;
prime[cp++] = i;
for (int j = 2 * i; j <= n; j += i)
v[j] = 1;
}
}
ll solve (ll n) {
ll ans = 0;
for (int i = 0; i < cp; i++) {
ll u = prime[i] * prime[i];
if (u > n)
break;
while (u <= n) {
u *= prime[i];
ans++;
}
}
return ans;
}
int main () {
primeTable(maxn);
int cas;
scanf("%d", &cas);
while (cas--) {
scanf("%lld%lld", &lower, &up);
printf("%lld\n", solve(up) - solve(lower-1));
}
return 0;
}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。