The Euler function 数论(筛法求欧拉函数)+前缀和

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

InputOutput
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);
    }

}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值