The Euler function phi is an important kind of function in number theory, (n) represents the amount of the numbers which are smaller than n and coprime to n, and this function has a lot of beautiful characteristics. Here comes a very easy question: suppose you are given a, b, try to calculate (a)+ (a+1)+....+ (b)
Input
There are several test cases. Each line has two integers a, b (2<a<b<3000000).
Output
Output the result of (a)+ (a+1)+....+ (b)
Sample
Input | Output |
---|---|
3 100 | 3042 |
思路:
(n)代表的是1~n中与n互质的数的个数,那么我们可以用筛法求欧拉函数,找出两个端点的值,相减,此时我们发现左端点的数值被减掉了,那么我们还是减左端点-1,再利用前缀和的知识就可以解出来了
代码 :
#include<iostream>
using namespace std;
const int N = 3e6 + 5;
typedef long long ll;
int primes[N], cnt;
int phi[N];
bool st[N];
ll getola(int n)
{
phi[1] = 1;
for (int i = 2; i <= n; i++)
{
if (!st[i])
{
primes[cnt++] = i;
phi[i] = i - 1;
}
for (int j = 0; primes[j] <= n / i; j++)
{
st[primes[j] * i] = true;
if (i % primes[j] == 0)
{
phi[primes[j] * i] = phi[i] * primes[j];
break;
}
phi[primes[j] * i] = phi[i] * (primes[j] - 1);
}
}
ll res = 0;
for (int i = 1; i <= n; i++)res += phi[i];
return res;
}
int main()
{
int m, n;
while (~scanf("%d%d", &n, &m))
{
ll sum = getola(m) - getola(n-1);
printf("%lld\n", sum);
}
}