题意,给定 \(l\) , \(r\)
求
\[ \sum_{i=l}^{r} \varphi(i) \]
欧拉函数筛
欧拉函数是积性函数,即 \(\varphi(a) \times \varphi(b)=\varphi(ab)\)
根据定义可得
\(\varphi(p)=p-1\) (\(p\)为质数)
\(\varphi(i \times p_j)=\varphi(i) \times (p_j-1)\) (\(p_j\)为质数且 \(i \ge 1\) 且 \(i \bmod p_j \ne 0\))
\(\varphi(i \times p_j)=\varphi(i) \times p_j\) \((p_j\)为质数且 \(i \ge 1\) 且 \(i \bmod p_j =0\))
于是预处理线性筛出范围内的每个数的欧拉函数,做一遍前缀和然后 \(O(1)\) 查询即可
时间复杂度 \(O(n)\)
然后你会发现你一会RE一会MLE
其实这题空间不够,然后还要开 unsigned long long
用 \(O(n\sqrt n)\) 的筛法就可以了
// This code writed by chtholly_micromaker(MicroMaker)
#include
#define reg register
#define int long long
#define U unsigned
using namespace std;
const int MaxN=5000050;
template inline void read(t &s)
{
s=0;
reg int f=1;
reg char c=getchar();
while(!isdigit(c))
{
if(c=='-')
f=-1;
c=getchar();
}
while(isdigit(c))
s=(s<<3)+(s<<1)+(c^48),c=getchar();
s*=f;
return;
}
U int phi[MaxN];
// ,p[MaxN];
// int pn;
// bool vis[MaxN];
/*
inline void Init_phi()
{
phi[1]=1;
for(int i=2;i=MaxN)
break;
vis[i*p[j]]=true;
if(!(i%p[j]))
{
phi[i*p[j]]=phi[i]*p[j];
break;
}
else
{
phi[i*p[j]]=phi[i]*(p[j]-1);
}
}
}
return;
}
*/
inline void Init_phi()
{
phi[1]=1;
for(int i=2;i>T;
reg int a,b;
for(int i=1;i<=T;++i)
{
scanf("%lld %lld",&a,&b);
printf("Case %lld: %llu\n",i,phi[b]-phi[a-1]);
}
return 0;
}