欧拉函数算法

一、欧拉函数值

欧拉函数又称为Phi函数
欧拉函数的定义为:对于正整数n,他的欧拉函数值是不大于n的正整数中与n互质的正整数的个数(互质:除1外没有其他最大公约数)。
据此,可以得到求某个数欧拉值的代码:

法一:暴力方法求欧拉函数值:

int gcd(int a, int b)
{
    return b == 0 ? a : gcd(b, a % b);
}
int Phi(int x)
{
    int ans = 0;
    for (int i = 1; i <= x; i++)
    {
        if (gcd(i, x) == 1)
            ans++;
    }
    return ans;
}

这种方法时间复杂度为O(nlogn)

法二、公式法求欧拉函值

公式

p1,p2…pn是n的质因数
比如8,8只有一个质因数2,4是其因数但不是质因数12的质因数为2和3,2的倍数都不是其质因数,有1/2是2的倍数,1/2不是2的倍数;3的倍数也不是其质因数,3的倍数不是其质因数,有1/3,所以2/3是其质因数所以12的质因数有121/22/3 = 4;

1.原理:利用欧拉函数公式 1与任何整数都互质。

#include<bits/stdc++.h>
using namespace std;
int Phi(int n){
    int ans = n;
    for(int i = 2; i * i <= n; i++){
        if(n % i == 0){
            ans = ans /i*(i - 1);
        }
        while(n % i == 0){
            n = n /i;
        }
    }
    if(n > 1) ans = ans /n*(n -1);
    return ans;
     
}
int main(){
    int n;
    cin >> n;
    cout << Phi(n);
}

有时候我们不仅仅求某一个数的欧拉函数值,而是要求欧拉函数根据欧拉函数值的定义以及欧拉函数使用筛法求出欧拉函数表
**

二、利用挨氏筛求欧拉函数表⭐⭐⭐

**

原理:挨氏筛求欧拉函数表,利用欧拉函数公式,时间复杂度为O(nlogn)只需要开一个phi数组phi数组具有两个最用,标记是不是素数,同时在程序运行过程中求出欧拉函数值

#include<bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int phi[N]; //phi[i] 为0时是素数
void GetPhi(int maxn){
    memset(phi,0,sizeof(phi));
    phi[1] = 1;
    for(int i = 2; i < maxn; i++){
        if(!phi[i]){
            for(int j = i; j < maxn; j = j + i){  //把i的倍数全处理了
                if(!phi[j]) phi[j] = j;
                phi[j] = phi[j]/i * (i - 1);
            }
             
        }
    }
    return;
}
int main(){
    int x;
    cin >> x;
    GetPhi(100);
    cout << phi[x];
 
}

三、利用欧拉筛求欧拉函数表

线性筛求欧拉函数表的原理是利用欧拉函数递推式,时间复杂度为O(n)

#include<bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
bool isprime[N];
int primes[N],pn;
int phi[N];
void FastPhi(int maxn){
    memset(isprime,true,sizeof(isprime));
    isprime[0] = isprime[1] = false;
    phi[1] = 1;
    pn = 0;
    for(int i = 2; i < maxn; i ++){
        if(isprime[i]){
            primes[pn++] = i;
            phi[i] = i - 1;
        }
        for(int j = 0; j < pn && i * primes[j] < maxn; j++){
            isprime[i * primes[j]] = false;
            if(i % primes[j] == 0){
                phi[i * primes[j]] = phi[i] * primes[j];break;
            }
            phi[i * primes[j]] = phi[i] * (primes[j] - 1);
        }
    }
    return;
}
int main(){
 
}

例题

Farey Sequence (POJ 2478) Description

The Farey Sequence Fn for any integer n with n >= 2 is the set of
irreducible rational numbers a/b with 0 < a < b <= n and gcd(a,b) = 1
arranged in increasing order. The first few are F2 = {1/2} F3 = {1/3,
1/2, 2/3} F4 = {1/4, 1/3, 1/2, 2/3, 3/4} F5 = {1/5, 1/4, 1/3, 2/5,
1/2, 3/5, 2/3, 3/4, 4/5}

You task is to calculate the number of terms in the Farey sequence Fn.
Input There are several test cases. Each test case has only one line, which contains a positive integer n (2 <= n <= 106). There are
no blank lines between cases. A line with a single 0 terminates the
input.
Output For each test case, you should output one line, which contains N(n) ---- the number of terms in the Farey sequence Fn.
Sample Input 2 3 4 5 0 Sample Output 1 3 5 9

思路
求解小于N的互质组数,相当于求欧拉函数值的和在(2<=i<=n),直接用打表法算出欧拉函数表。

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int N = 1e6 + 100;
int phi[N]; //phi[i] 为0时是素数
void getPhi(int maxn){
    memset(phi,0,sizeof(phi));
    phi[1] = 1;
    for(int i = 2; i < maxn; ++i){
        if(!phi[i]) //满足该条件为素数
        {
            for(int j = i ; j < maxn; j = j + i){
                if(!phi[j]) phi[j] = j;
                phi[j] = phi[j] /i * (i - 1);
            }
        }
    }
    return;
}
int main(){
  getPhi(N);
   int n;

  while(cin >> n){
      if(n == 0) break;
      long long  sum = 0;
      for(int i = 2; i <=n; i++){
          sum += phi[i];
      }
      cout << sum <<endl;
  }  

}

注意sum的取值范围,用int的话可能导致结果不正确,需要使用int,或者是—__int64 sum = 0;在提交POJ时不能使用万能头文件

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

释怀°Believe

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值