GCD - Extreme 欧拉函数

本文探讨了GCD(最大公约数)与欧拉函数的关系,通过分类和递推公式阐述了如何计算G(n)。欧拉函数φ(n)表示小于n且与n互质的正整数数量,其在数论和环论中有重要应用。通过筛法预处理φ[x]表可以降低算法的时间复杂度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Given the value of N, you will have to find the value of G. The definition of G is given below:
G =
i<N

i=1
j

≤N
j=i+1
GCD(i, j)
Here GCD(i, j) means the greatest common divisor of integer i and integer j.
For those who have trouble understanding summation notation, the meaning of G is given in the
following code:
G=0;
for(i=1;i<N;i++)
for(j=i+1;j<=N;j++)
{
G+=gcd(i,j);
}
/*Here gcd() is a function that finds
the greatest common divisor of the two
input numbers*/
Input
The input file contains at most 100 lines of inputs. Each line contains an integer N (1 < N < 4000001).
The meaning of N is given in the problem statement. Input is terminated by a line containing a single
zero.
Output
For each line of input produce one line of output. This line contains the value of G for the corresponding
N. The value of G will fit in a 64-bit signed integer.
Sample Input
10
100
200000
0
Sample Output
67
13015

143295493160


1.设G(n)=gcd(1,n)+gcd(2,n)+……+gcd(n-1,n)。

gcd(x,n)=i是n的约数(x<n),按照这个约数进行分类。设满足gcd(x,n)=i的有ph(n/i)个,则有G(n)=sum(i*ph(n/i))。

ph(x)为欧拉函数。(代码下面有详细说明)

2.建立递推关系,G(n)=G(n-1)+gcd(1,n)+gcd(2,n)+……+gcd(n-1,n);

3.降低时间复杂度。用筛法预处理phi[x]表

用筛法预处理f(x)->枚举因数,更新其所有倍数求解

代码:

#include<cstdio>
using namespace std;
const int N=4000005;
typedef long long ll;
int ph[N];
ll G[N];
void init()
{
    for(int i=2;i<N;i++)
        ph[i]=i;
    for(int i=2;i<N;i++)
    {
        if(ph[i]==i)
          for(int j=i;j<N;j+=i)
             ph[j]=ph[j]/i*(i-1);//欧拉函数
        for(int j=1;j*i<N;++j)
            G[j*i]+=(ll)j*ph[i];
    }
    for(int i=3;i<N;i++)
        G[i]+=G[i-1];
}

int main()
{
   init();
   int n;
   while(scanf("%d",&n),n)
      printf("%lld\n",G[n]);
   return 0;
}
欧拉函数:

数论,对正整数n,欧拉函数是小于n的正整数中与n互质的数的数目(φ(1)=1)。此函数以其首名研究者欧拉命名(Euler'so totient function),它又称为Euler's totient function、φ函数、欧拉商数等。 例如φ(8)=4,因为1,3,5,7均和8互质。 从欧拉函数引伸出来在环论方面的事实和拉格朗日定理构成了欧拉定理的证明。

通式:
 
其中p1, p2……pn为x的所有质因数,x是不为0的整数。
φ(1)=1(唯一和1 互质的数(小于等于1)就是1本身)。
注意:每种质因数只一个。 比如12=2*2*3那么φ(12)=12*(1-1/2)*(1-1/3)=4
若n是质数p的k次幂,
   
,因为除了p的倍数外,其他数都跟n互质。
设n为正整数,以 φ(n)表示不超过n且与n互素的正整数的个数,称为n的欧拉函数值
φ:N→N,n→φ(n)称为欧拉函数。
欧拉函数是 积性函数——若m,n互质,
 
特殊性质:当n为奇数时,
   
, 证明与上述类似。
若n为质数则
 




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值