链接
题解
对每个数求出其素约数个数(线性),然后求前缀和
代码
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define iinf 0x3f3f3f3f
#define linf (1ll<<60)
#define eps 1e-8
#define maxn 5000010
#define cl(x) memset(x,0,sizeof(x))
#define rep(_,__) for(_=1;_<=(__);_++)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
struct EasyMath
{
ll prime[maxn], f[maxn];
bool mark[maxn];
void get_prime(ll N)
{
ll i, j;
for(i=2;i<=N;i++)mark[i]=false;
*prime=0;
for(i=2;i<=N;i++)
{
if(!mark[i])prime[++*prime]=i, f[i]=1;
for(j=1;j<=*prime and i*prime[j]<=N;j++)
{
mark[i*prime[j]]=true;
f[i*prime[j]]=f[i]+1;
if(i%prime[j]==0)break;
}
}
}
}em;
ll f[maxn];
int main()
{
ll T, a, b, i, k;
em.get_prime(5000000);
rep(i,5000000)f[i]=f[i-1]+em.f[i];
scanf("%lld",&T);
while(T--)
{
scanf("%lld%lld",&a,&b);
printf("%lld\n",f[a]-f[b]);
}
return 0;
}