HDU 2824 (欧拉函数)

本文介绍了一种基于欧拉函数的求和算法,用于计算从a到b范围内所有数的欧拉函数值之和。通过预处理和累积求和的方法,实现了高效计算。文章提供了详细的算法解释和C++实现代码。

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
3 100

Sample Output
3042

参考:https://blog.youkuaiyun.com/boliu147258/article/details/99246240

欧拉函数的定义:
在数论中,对于正整数N,少于或等于N ([1,N]),且与N互质的正整数(包括1)的个数,记作φ(n)。
φ函数的值:
φ(x)=x(1-1/p(1))(1-1/p(2))(1-1/p(3))(1-1/p(4))……(1-1/p(n)) 其中p(1),p(2)…p(n)为x的所有质因数;x是正整数; φ(1)=1(唯一和1互质的数,且小于等于1)。注意:每种质因数只有一个。

例如:
φ(10)=10×(1-1/2)×(1-1/5)=4;
φ(30)=30×(1-1/2)×(1-1/3)×(1-1/5)=8;
φ(49)=49×(1-1/7)=42;

代码如下:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
#define inf 3000010
using namespace std;
ll a[inf];
void oula(){
    for(ll i=0;i<inf;i++){
        a[i]=i;
    }
    for(ll i=2;i<inf;i++){
        if(i==a[i]){
            for(int j=i;j<inf;j+=i){
                a[j]=a[j]/i*(i-1);
            }
        }
    }
    for(ll i=2;i<inf;i++){
        a[i]+=a[i-1];
    }
}
int main(){
    ll l,r;
    oula();
    while(scanf("%lld %lld",&l,&r)!=EOF){
        printf("%lld\n",a[r]-a[l-1]);
    }
    return 0;
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值