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

被折叠的 条评论
为什么被折叠?



