欧拉函数+费马小定理拓展

欧拉函数

定义

欧拉函数ϕ(n)是不超过n且和n互质的正整数的个数。欧拉函数φ(n)的作用就是转化,从而简化运算(小性质:n的所有质因子之和=eular(n)*n/2);

下面直观地看看欧拉函数:

n 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
φ(n) 1 1 2 2 4 2 6 4 6 4 10 4 12 6 8

定理

  • 定理1 算术函数f如果满足对于任意两个互质的正整数m和n,均有f(mn)=f(m)f(n),就称f为积性函数(或乘性函数)。如果对于任意两个正整数m和n,均有f(mn)=f(m)f(n),就称为完全积性函数。

  • 定理2 若m、n互质,ϕ(mn)=ϕ(m)ϕ(n),所以欧拉函数是积性函数。因为mn互质,和m互质的数乘上和n互质的数就会和mn互质。

  • 定理3              

  •  例:输入a,p,n,求large {color{Red} a^{p}\, mod\, n}

  • LARGE {color{Red} {color{Red} a^{^{p}}mod\, n=a^{varphi left ( n 
ight )ast x+y}mod\, n=(a^{varphi left ( n 
ight )}mod: n)^{^{x}}*a^{y}mod\, n{color{Red} {color{Red} }}}=1^{x}*(a\, mod\, n)^{y}\, mod\, n}
#include<iostream>
#define ll long long
using namespace std;
int eular(int n)
{
        int res=n;
        for(int i=2;i*i<=n;i++){
                if(res%i==0){
                        n/=i;
                        res=res/i*(i-1);//res(1-1/i)
                        while(n%i==0)
                                n/=i;
                }
        }
        return n>1?res/n*(n-1):res;//最后一个质数不为1的情况
}
int main()
{
        int n,a,p;
        while(~scanf("%d %d %d",&a,&p,&n)){//2 100 13
                int en=eular(n);
                int aa=a%n;
                int y=p%en;
                int sum=y?1:0;
                for(int i=1;i<=y;i++)//sum=a^y mod n;
                        sum=(sum*aa)%n;
                printf("%d^%d mod%d=%d
",a,p,n,sum);//2^100 mod 13=3
        }
        return 0;
}
原文地址:https://www.cnblogs.com/aeipyuan/p/10182222.html