紫书 习题 10-7 UVa 10539(long long + 素数筛)
注意要开long long
如果int * int会炸
那么久改成long long * int
#include<cstdio>
#include<vector>
#include<cstring>
#define REP(i, a, b) for(int i = (a); i < (b); i++)
using namespace std;
typedef long long ll;
const int MAXN = 1123456;
bool is_prime[MAXN];
vector<int> prime;
void init()
{
memset(is_prime, true, sizeof(is_prime));
is_prime[0] = is_prime[1] = false;
REP(i, 2, MAXN)
{
if(is_prime[i]) prime.push_back(i);
REP(j, 0, prime.size())
{
if(i * prime[j] >= MAXN) break;
is_prime[i * prime[j]] = false;
if(i % prime[j] == 0) break;
}
}
}
int sum(ll x, int p)
{
int ret = 0;
ll t = (ll)p * p; //注意这里
while(t <= x)
{
ret++;
t *= p;
}
return ret;
}
int f(ll x)
{
int ret = 0;
REP(i, 0, prime.size())
{
if((ll)prime[i] * prime[i] > x) break; //注意这里
ret += sum(x, prime[i]);
}
return ret;
}
int main()
{
init();
int n;
ll a, b;
scanf("%d", &n);
REP(i, 0, n)
{
scanf("%lld%lld", &a, &b);
printf("%d\n", f(b) - f(a - 1));
}
return 0;
}