# Congruence theorem inverse element Chinese remainder theorem Fermat small theorem

## Congruence theorem

Congruence theorem is an important concept in number theory. Given a positive integer m, if two integers a and B satisfy (a-b) and can be divided by m, that is, (a-b) / M obtains an integer, then the integers a and B are called modulo m congruence and recorded as a ≡ b(mod m).

If the remainder of two integers a and b divided by the integer m is equal, then a and b are said to be modulo m congruent or a congruent to b modulo M

### congruence properties

• Reflexivity: a ≡ a (mod m)
• Symmetry: if a ≡ b(mod m), then b ≡ a(mod m)
• Transitivity: if a ≡ b(mod m), b ≡ c(mod m), then a ≡ c(mod m)
• Addition of congruence formula: if a ≡ b(mod m), b ≡ c(mod m), then a ± c ≡ b ± d(mod m)
• Congruence multiplication: if a ≡ b(mod m), b ≡ c(mod m), then ac ≡ bd(mod m)
• Linear operation: if a ≡ b(mod m), c ≡ d(mod m), then a ± c ≡ B ± d(mod m), and a * c ≡ b * d(mod m)
• Division: if ac ≡ bc (mod m) c ≠ 0, a ≡ b (mod m/gcd(c,m)) where gcd(c,m) represents the maximum common divisor of C and m. In particular, gcd(c,m)=1, then a ≡ b (mod m)
• Power operation: if a ≡ b (mod m), then a^n ≡ b^n (mod m)
• If a ≡ b (mod m), n|m, then a ≡ b (mod n)
• If a ≡ b (mod mi) (i=1,2... n), a ≡ b (mod [m1,m2,... mn]), where [m1,m2,... mn] represents the least common multiple of m1,m2,... mn

### Correlation theorem

• Euler theorem
• Fermat's small theorem
• Chinese Remainder Theorem (Sun Tzu theorem)

## Inverse element

( a / b ) % p = ( a ∗ b of inverse element ) % p (A / b) \% P = (inverse of a * b) \% p (A / b)% P = (inverse of a * b)% p

Inverse element of Fermat's theorem:

Inverse element of b= b p − 2 b^{p-2} bp−2

Linear recurrence

#include<bits/stdc++.h>
#define N 3000010
typedef long long ll;
using namespace std;
int inv[N],n,p;
int f=1,x=0;
char ch;
do {
ch=getchar();
if(ch=='-')f=-1;
} while(ch<'0'||ch>'9');
do {
x=x*10+ch-'0';
ch=getchar();
} while(ch>='0'&&ch<='9');
return f*x;
}
int main() {
inv[1]=1;
puts("1");
for(int i= 2; i <= n; i++) {
inv[i] = (ll)(p - p / i) * inv[p % i] % p;
printf("%d\n", inv[i]);
}
}


## Chinese remainder theorem

In Sun Tzu Suan Jing, there is such a question: "today, there are things that don't know their number, two out of three (divided by 3-2), three out of five (divided by 5-3), and two out of seven (divided by 7-2). Ask the geometry of things?" this question is called "Sun Tzu problem", and the general solution of this problem is internationally known as "China residue theorem". The specific solution is divided into three steps:

1. Find out three numbers: find the minimum 15 divided by 7 from the common multiples of 3 and 5, find the minimum 21 divided by 5 from the common multiples of 3 and 7, and finally find the minimum 70 divided by 3 and 1 from the common multiples of 5 and 7.
2. Multiply 15 by 2 (2 is the remainder of the final result divided by 7), multiply 21 by 3 (3 is the remainder of the final result divided by 5), similarly, multiply 70 by 2 (2 is the remainder of the final result divided by 3), and then add the three products to 15 * 2 + 21 * 3 + 70 * 2 to get the sum 233.
3. Divide 233 by the least common multiple 105 of 3, 5 and 7 to obtain the remainder 23, that is, 233% 105 = 23. This remainder 23 is the qualified minimum.

It's that simple. While lamenting the magic, we can't help wondering how the ancients thought of this method. Is there any basic mathematical basis?

### expand

Solving Congruence Equations

/*
https://www.luogu.com.cn/problem/P4777
*/

#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std;
typedef long long lt;

{
lt f=1,x=0;
char ss=getchar();
while(ss<'0'||ss>'9'){if(ss=='-')f=-1;ss=getchar();}
while(ss>='0'&&ss<='9'){x=x*10+ss-'0';ss=getchar();}
return f*x;
}

const int maxn=100010;
int n;
lt ai[maxn],bi[maxn];

lt mul(lt a,lt b,lt mod) // Turtle speed ride
{
lt res=0;
while(b>0)
{
if(b&1) res=(res+a)%mod;
a=(a+a)%mod;
b>>=1;
}
return res;
}

lt exgcd(lt a,lt b,lt &x,lt &y)
{
if(b==0){x=1;y=0;return a;}
lt gcd=exgcd(b,a%b,x,y);
lt tp=x;
x=y; y=tp-a/b*y;
return gcd;
}

lt excrt()
{
lt x,y,k;
lt M=bi[1],ans=ai[1];//The solution of the first equation judges ans as the general solution of the first k - 1 equations
for(int i=2;i<=n;i++)
{
lt a=M,b=bi[i],c=(ai[i]-ans%b+b)%b;//ax≡c(mod b)
lt gcd=exgcd(a,b,x,y),bg=b/gcd;
if(c%gcd!=0) return -1; //Judge whether there is no solution, but this problem is not needed
x=mul(x, c/gcd, bg);
ans+=x*M;//Update the answers of the first k equations
M*=bg;//M is the lcm of the first k M
ans=(ans%M+M)%M;
}
return (ans%M+M)%M;
}

int main()
{
for(int i=1;i<=n;++i)
printf("%lld",excrt());
return 0;
}


## Euler theorem

Euler's theorem shows that if n and a are positive integers and mutually prime, then
a ψ ( n ) ≡ 1 ( m o d   n ) a^{\psi(n)} \equiv 1(mod\,n) aψ(n)≡1(modn)

## Fermat's small theorem decreasing power

Premise of use:
If P is a prime number and gcd(a, p) = 1, then a^(p-1) ≡ 1 (mod p)
seek ( a N )   m o d ( p ) Find (a^N)\,mod(p) Find (aN)mod(p)
a p − 1 ≡ 1   ( m o d   p ) a^{p-1} \equiv1\,(mod\,p) ap−1≡1(modp)
k = N % ( p − 1 ) k=N\%(p-1) k=N%(p−1)
( a N ) m o d ( p ) = ( a k ) m o d ( p ) (a^N)mod(p)=(a^k)mod(p) (aN)mod(p)=(ak)mod(p)

Posted on Sun, 28 Nov 2021 04:33:15 -0500 by Monk3h