费马小定理&逆元&欧拉定理(函数)

 

 

目录

 

1.逆元:

2.欧拉函数:

3.欧拉定理:

4.费马小定理:

CODE(A):

CODE(B):

CODE(F+打表):



1.逆元:

\mathbf{a\cdot x \equiv 1(mod \ m)}

\mathbf{A.\ gcd(a,m) \ = \ 1: \Rightarrow a\cdot x \ + \ y\cdot m \ = \ 1 }

\mathbf{B.m \ is \ a \ prime:\Rightarrow x=a^{\phi (m)-1}=a^{m-2}}//\phi (m)=m-1


2.欧拉函数:

\mathbf{\phi (n)}

\mathbf{C.\ n \ is \ prime\Rightarrow \phi (n) = n-1}

\mathbf{D. \ if \ n = p\cdot q\Rightarrow \phi (n) = \phi (p)\cdot \phi (q)}

\mathbf{E.\ if \ n = p^k \Rightarrow \phi (n) = p^k - p^{k-1} =p^k(1-\frac{1}{p})}

\mathbf{F. \ \phi (n) = n\cdot \prod_{i=1}^{k}(1-\frac{1}{p_i})}


3.欧拉定理:

\mathbf{gcd(a,b) \ = \ 1 \Rightarrow a^{\phi (b) } \ mod \ b \ = \ 1}


4.费马小定理:

费马小定理是欧拉函数的一种特殊情况:

\mathbf{p \ is \ prime \ AND \ gcd(a,p) = 1 \Rightarrow a^{p-1} \ mod \ p = 1}


CODE(A):

#include 
using namespace std;

typdef long long ll;

ll a,p;
ll exgcd(ll a,ll b,ll& d ,ll& x,ll& y)
{
	if(!b)	{d=a;x=1;y=0;}
	else	{;exgcd(b,a%b,d,y,x);y-=x*(a/b);}
}

ll inverse(ll a,ll n)
{
	ll d,x,y;
	exgcd(a,n,d,x,y);
	return d==1?(x+n)%n:-1;
}

int main()
{
	cin>>a>>p;
	ll inverse_=inverse(a,p);
	cout<

CODE(B):

#include 
using namespace std;
ll a,p;

ll quick_mul(ll a,ll b,ll c)
{
	ll ans=0;
	while(b)
	{
		if(b&1)	ans=(ans+a)%c;
		a=(a+a)%c;
		b>>=1;
	}
	return ans;
}

ll quick_pow(ll a,ll b,ll c)
{
	ll ans=1;
	while(b)
	{
		if(b&1)	ans=quick_mul(ans,a,c)%c;
		a=quick_mul(a,a,c)%c;
		b>>=1;
	}
	return ans;
}

int main()
{
	cin>>a>>p;
	ll ans=quick_pow(a,p-2,p);
	cout<

CODE(F+打表):

void phi_table(int n){//欧拉打表 
    for (int i = 2;i <= n;i++) phi[i] = 0;
        phi[1] = 1;
    for (int i = 2;i <= n;i++) if (!phi[i])
    for (int j = i;j <= n;j += i){
        if (!phi[j]) phi[j] = j;
        phi[j] = phi[j] / i * (i - 1);
    }
}

 

你可能感兴趣的:(数论,数学)