首页 > 其他分享 >欧拉函数

欧拉函数

时间:2022-08-15 19:15:12浏览次数:73  
标签:phi 函数 int res primes 欧拉

欧拉函数

acwing873.欧拉函数

定义

欧拉函数phi(n)表示1~n中互质的数的个数

(若n为质数,这phi(n) = n-1)

欧拉函数公式

已知image

则欧拉函数的公式:

\[\varphi (n) = N(1-\frac{1}{p_1}) (1-\frac{1}{p_2})... (1-\frac{1}{p_k}) \]

证明使用容斥原理,让N减去质数的倍数最后剩下的就是互质的数.

代码

先分解质因数,再用公式求即可

使用公式求的时候不用res*(1 - 1/p)而要使用res / p * (p - 1),因为c++中是整除 1/p 的结果就会是0

#include<iostream>

using namespace std;

int phi(int x)
{
    int res = x;
    // 试除法分解质因数
    for(int i = 2; i <= x/ i; i ++)
    {
        if(x % i == 0) 
        {
            res = res / i * (i - 1) ;
            while(x % i == 0) x /= i;
        }
    }
    if(x > 1) res = res / x * (x - 1); // 不要忘记漏乘,因为x有可能存在一个大于sqrt(x)的质因子
    
    return res ;
}


int main()
{
    int n;
    cin >> n;
    while(n --)
    {
        int x;
        cin >> x;
        cout << phi(x) << endl;
    }
    
    return 0;
}

线性筛法求欧拉函数

acwing874.线性筛法求欧拉函数

这种一般都是求1~n中所有数的欧拉函数值的和

这时我们要使用线性筛法筛质数去改写求欧拉函数,这样时间复杂度为O(n)

质数i的欧拉函数即为phi[i] = i - 1:1 ~ i−1 中i−1均与i互质,共i−1个。

phi[primes[j] * i]分为两种情况:
① i % primes[j] == 0时:
	primes[j]是i的最小质因子,也是primes[j] * i的最小质因子,因此
	1 - 1 / primes[j]这一项在phi[i]中计算过了,只需将基数N修正为p
	rimes[j]倍,最终结果为phi[i] * primes[j]。
② i % primes[j] != 0:
	primes[j]不是i的质因子,只是primes[j] * i的最小质因子,因此不
	仅需要将基数N修正为primes[j]倍,还需要补上1 - 1 / primes[j]这
	一项,因此最终结果phi[i] * (primes[j] - 1)。
	
	也可以这么想,primes[j]为质数,因此它有primes[j]-1个互质的数,
	而i有phis[i]个,并且primes[j]不是i的质因子,所以结果就相乘,是
	phi[i] * (primes[j] - 1)
代码
#include<iostream>

using namespace std;

typedef long long LL;

const int N = 1e6 + 10;

int primes[N],cnt;
int phis[N];
bool st[N];

void get_phis(int n)
{
    phis[1] = 1;
    for(int i = 2; i <= n; i ++)
    {
        if(!st[i])
        {
            phis[i] = i - 1; // i为最小质因子,phi[i] = i - 1
            primes[cnt ++] = i;
        }
        for(int j = 0; primes[j] * i <= n; j ++)
        {
            st[primes[j] * i] = true; // 将这个合数筛去
            if(i % primes[j] == 0) 
            {
                phis[primes[j] * i] = phis[i] * primes[j]; // primes[j]为i最小质因子,也为primes[j]*i的最小质因子,phis[i]中已经包括了(1-1/primes[j]),只需将N修正为原来的primes[j]倍即可
                break;
            }
            phis[primes[j] * i] = phis[i] * (primes[j] - 1); // primes[j]不是i质因子,不仅要将基数N改为原来的primes[j]倍,还要乘上(1 - 1/primes[j]),最终结果为phis[i] * (primes[j] - 1)
        }
    }
}

int main()
{
    int n;
    cin >> n;
    
    get_phis(n);
    
    LL res = 0;
    for(int i = 1; i <= n; i ++) res += phis[i];
    
    cout << res << endl;
    
    return 0;
}

标签:phi,函数,int,res,primes,欧拉
From: https://www.cnblogs.com/rdisheng/p/16588970.html

相关文章

  • Python-09函数基础、形参、实参
    Python3函数函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段。函数能提高应用的模块性,和代码的重复利用率。你已经知道Python提供了许多内建函数,比如print......
  • flask中偏函数的应用
    函数在执行时,要带上所有必要的参数进行调用。但是,有时参数可以在函数被调用之前提前获知。这种情况下,一个函数有一个或多个参数预先就能用上,以便函数能用更少的参数进行调......
  • pytest中文文档教程(五)pytest钩子函数大全
    前言​ 前几篇文章介绍了pytest点的基本使用,掌握前面pytest的基本使用已经插件开发,要开发pytest插件就离不开pytest的钩子函数,就可以满足工作中编写用例和进行自动化测试......
  • oracle中常用函数大全
    oracle中常用函数大全1、数值型函数函数说明样例显示ceil(n)大于或等于数值n的最小整数selectceil(10.6)fromdual;11floor(n)小......
  • python 中字典内置函数get()
     001、>>>dict1={"a":100,"b":200,"c":300,"d":400,"e":500}##测试字典>>>dict1{'a':100,'b':200,'c':300,'d':400,'e'......
  • 新的 CSS 伪类函数 :is() 和 :where()
    :is()和:where()标题中的<b>标签进行颜色调整:h1>b,h2>b,h3>b,h4>b,h5>b,h6>b{color:hotpink;}现在,我们可以使用:is()缩减代码并提高其......
  • ASP.NET Core依赖注入系统学习教程:容器对构造函数选择的策略
    .NETCore的依赖注入容器之所以能够为应用程序提供服务实例,这都归功于ServiceDescriptor对象提供的服务注册信息。另外,在ServiceDescriptor对象中,还为容器准备了3种提供服......
  • 原生js构造函数及其对象 学习总结
    js构造函数及其对象ES5functionPerson(age){ this.name='张三' this.age=age this.talk=function(){ alert('hello') }}首字母大写构造函数中的thi......
  • Python 函数运行时间统计
    fromfunctoolsimportwrapsimporttimedeffunc_time(f):@wraps(f)defwrapper(*args,**kwargs):start=time.time()result=f(*ar......
  • python | split函数时间复杂度
    源码while(maxcount-->0){while(i<str_len&&STRINGLIB_ISSPACE(str[i]))i++;if(i==str_len)break;j=i;i++;while(i<......