枚举2到 ,分别看 ,是否在 中,统计多少个这样的次方方在范围中即可。
#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;
}