The Euler function(欧拉函数)

本文介绍了一种基于欧拉函数的求和算法,通过预处理得到一系列数值的欧拉函数值,进而快速计算任意两个整数之间的欧拉函数之和。文章详细解释了欧拉函数的概念及其性质,并给出了具体的实现代码。

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

Problem Description

  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


题意:输入a,b输出a~~b欧拉函数之和

题解:欧拉函数模板题

定义: 对于正整数n,φ(n)是小于或等于n的正整数中,与n互质的数的数目;

  例如: φ(8) = 4, 因为1,3,5,7均和8互质。

  性质: 1. 若p是质数,φ(p)= p-1.

  2. 若n是质数p的k次幂,φ(n)= (p-1)p^(k-1)

  因为除了p的倍数都与n互质

  3. 欧拉函数是积性函数,若m,n互质,φ(mn)= φ(m)φ(n)

  根据这3条性质我们就可以退出一个整数的欧拉函数的公式,因为一个数总可以一些质数的乘积的形式。

  E(k) = (p1-1)(p2-1)…(pi-1)*(p1^(a1-1))(p2^(a2-1))…(pi^(ai-1))

  = k*(p1-1)(p2-1)…(pi-1)/(p1*p2*…pi)

  = k*(1-1/p1)*(1-1/p2)…(1-1/pk)

  在程序中利用欧拉函数如下性质,可以快速求出欧拉函数的值(a为N的质因素)

  若(N%a==0 && (N/a)%a==0) 则有:E(N)=E(N/a)*a;

  若(N%a==0 && (N/a)%a!=0) 则有:E(N)=E(N/a)*(a-1);

打表求解

#include<iostream>
using namespace std;
__int64 phi[3000001];
int main()
{

  int i,j;
  for(i=1;i<3000001;i++) ph[i]=i;
  for(i=2;i<3000001;i+=2) ph[i]/=2;
  for(i=3;i<3000001;i+=2)
  if(ph[i]==i)
  {
  for(j=i;j<3000001;j+=i)
  phi[j]=phi[j]/i*(i-1);
  }
  int a,b;
  __int64 sum;
  while(cin>>a>>b)
  {
  sum=0;
  for(i=a;i<=b;i++)
  sum+=phi;
  printf("%I64d\n",sum);
  }
  return 0;
  }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值