题目链接:https://vjudge.net/problem/UVA-10990
![](https://i-blog.csdnimg.cn/blog_migrate/ea92d30903952ab0092e5ab91e325d76.png)
定义欧拉函数深度:
ϕ(13) = 12...step1
ϕ(12) = 4...step2
ϕ(4) = 2...step3
ϕ(2) = 1...step4
ϕ(13)深度为4
ϕ(13)深度为4
输入多组数据,每组数据包含两个数,形成一个区间,求这个区间里所有数的欧拉函数深度之和
数据不大,先打表求出每个数的欧拉函数值,暴力求出每个数的深度,抱着TLE的心情交了一发,竟然过了!
#include<bits/stdc++.h>
#define LL long long
#define maxn 2000010
using namespace std;
LL phi[maxn];
LL depth[maxn];
int Find_depth(LL n)
{
LL temp=n;
int cnt=0;
while(temp)
{
temp=phi[temp];
cnt++;
}
return depth[n]=--cnt;
}
int main()
{
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
memset(depth,-1,sizeof(depth));
for(int i=2; i<maxn; i++)
{
phi[i]=i;
}
for(int i=2; i<maxn; i++)
{
if(phi[i]==i)
{
for(int j=i; j<maxn; j+=i)
{
phi[j]=phi[j]/i*(i-1);
}
}
}
int T;
while(cin>>T)
{
LL l,r;
while(cin>>l>>r)
{
LL sum=0;
for(int i=l; i<=r; i++)
{
if(depth[i]!=-1)
sum+=depth[i];
else
sum+=Find_depth(i);
}
cout<<sum<<endl;
}
}
return 0;
}