传送阵:NEFU2022-Euler's totient function - Virtual Judge
代码:
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<math.h>
#include<map>
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
#define per(i,a,b) for(int i=a;i<=b;i++)
#define ber(i,a,b) for(int i=a;i>=b;i--)
const int N = 3e6 + 3;
const long long mod = 10000000033;
int n, a, b;
int vis[N + 100], su[N + 100];
int phi[N+100],cn;
void into()
{
phi[1] = 1;
vis[1] = vis[0] = 1;
for (int i = 2; i <= N; i++)
{
if (!vis[i])
{
su[++cn] = i;
phi[i] = i - 1;
}
for (int j = 1; j <= cn && (LL)su[j]*i <= N; j++)
{
int m = su[j] * i;
vis[m] = 1;
if (i % su[j] == 0)
{
phi[m] = phi[i] * su[j];
break;
}
else
phi[m] = (su[j] - 1) * phi[i];
}
}
}
int main()
{
ios::sync_with_stdio(false);
into();
while (scanf("%d%d",&a,&b)!=EOF)
{
LL ans = 0;
for (int i = a; i <= b; i++)
ans += phi[i];
cout << ans << endl;
}
return 0;
}