UVA10539

在这里插入图片描述

枚举2到$\sqrt{high}$,分别看$i^2,i^3\cdots i^n$,是否在$[left,right]$中,统计多少个这样的次方方在范围中即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <cstdio>
#include <cstring>
using namespace std;


const int MAXN = 1e7;
bool IsPrime[MAXN + 1];
vector<int> PTable;

void InitPTable() {
for (int i = 2; i <= MAXN; ++i) {
if (IsPrime[i] == false) {
PTable.push_back(i);
for (int j = 2 * i; j <= MAXN; j += i) {
IsPrime[j] = true;
}
}
}
}

int main() {

int T;
InitPTable();
scanf("%d", &T);
long long Left, Right;
while (T--) {
scanf("%lld%lld", &Left, &Right);
int MAX = ceil(sqrt(static_cast<double>(Right)));
int Ans = 0;
for (const int& Prime : PTable) {
long long i = static_cast<long long>(Prime) * Prime;
if (i > Right) {
break;
}
while (i < Left) {
i *= Prime;
}
if (i <= Right) {
++Ans;
}
while (true) {
i *= Prime;
if (i > Right) {
break;
}
++Ans;
}
}
printf("%d\n", Ans);
}

return 0;

}