二项式定理
定义
\((a+b)^k=\sum\limits_{k=0}^{n}C^k_na^kb^{n-k}\)。
证明
证:
\[显然 (a+b)^k 的项数为 k+1,由a^xb^y同类项组成(x,y为未知数且满足x+y=n)
\]
\[\because (a+b)^k=\underbrace{(a+b)(a+b)\dots(a+b)}_{k个(a+b)}
\]
\[\therefore 第 k 项的系数为 C_n^k
\]
\[\therefore (a+b)^k=\sum\limits_{k=0}^{n}C^k_na^kb^{n-k}
\]
证毕
裴蜀定理
定义
若 \(a,b\) 是不全为 \(0\) 的整数,则有 \(x,y\) 满足 \(a \times x+b \times y = \gcd(a,b)\)
证明
充分性证明
若 \(\gcd(a,b) | c\),则 \(a \times x + b \times y = c\) 有解。
证:
\[设: k 为 a,b 线性组合是最小非负解
\]
\[令:q=\frac{a}{k},则有 r = a \mod k = a - q \times k = a - q \times (a \times x + b \times y) = a \times (1 - q \times x) + b \times (1 - q \times y)
\]
\[r 也为线性组合 a,b 的解,且 0 \leq r \leq k
\]
\[\because k 为最小非负解
\]
\[\therefore k | a
\]
\[同理可得 k | b
\]
\[令:s | \gcd(a,b) 且 \gcd(a,b) \geq s
\]
\[有 \because \gcd(a,b) | a,\gcd(a,b) | b 且 s 为线性组合 a,b 的解
\]
\[\therefore \gcd(a,b) | s
\]
\[\because s > 0
\]
\[\therefore d = s
\]
\[\therefore \forall c = k \times \gcd(a,b),k \in \mathbb{Z} 是原方程的解
\]
证毕
必要性证明
若 \(a \times x + b \times y = c\) 有解,则 \(\gcd(a,b) | c\)
证:
\[\because a \times x + b \times y = c
\]
\[\therefore \gcd(a,b) | a \times x,\gcd(a,b) | b \times y
\]
\[\therefore \gcd(a,b) | a \times x + b \times y = c
\]
证毕
Code
AC Code of Luogu P4549 【模板】裴蜀定理
#include<bits/stdc++.h>
#define int long long
#define pii pair<int,int>
#define x first
#define y second
#define rep1(i,l,r) for(int i=l;i<=r;i++)
#define rep2(i,l,r) for(int i=l;i>=r;i--)
const int N=1e5+10;
using namespace std;
int n,ans;
inline int read()
{
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-') f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
x=(x<<1)+(x<<3)+(ch^48);
ch=getchar();
}
return f*x;
}
signed main()
{
n=read();
rep1(i,1,n)
{
int x=abs(read());//为负数就改为正数,因为gcd(a,b)=gcd(a,-b)
ans=__gcd(ans,x);//裴蜀定理
}
cout<<ans<<endl;//输出
return 0;
}
拓展欧几里得(exgcd)
作用
求解 \(ax+by=c\) 的整数解。
证明
必要性证明
证:
\[令:p=(a,b),设:a=a'p,b=b'p,则有(a',b')=1
\]
\[\therefore ax+by=c \Rrightarrow a'px+b'py=c \Rrightarrow p(a'x+b'y)=c
\]
\[\therefore p|c
\]
证毕
充分性证明
证:
\[令:p=(a,b),记欧几里得每次的参数为(a_1,b_1),(a_2,b_2)\dots(a_n,b_n),(a_1,b_1)为(a,b),(a_n,b_n)即为(p,0)
\]
\[对于(a_n,b_n)求解a_nx+b_ny=c的解
\]
\[\because a_n=p,b_n=0
\]
\[\therefore\begin{align*}
\begin{split}
\left\{
\begin{array}{ll}
x_n=\frac{c}{p}\\
\forall y_n \in \mathbb{Z}
\end{array}
\right.
\end{split}
\end{align*}
\]
\[\because 辗转相除
\]
\[\therefore\begin{align*}
\begin{split}
\left\{
\begin{array}{ll}
a_n=b_{n-1}\\
b_n=a_{n-1} \mod b_{n-1} \Rrightarrow b_n=a_{n-1}-\lfloor \frac{a_{n-1}}{b_{n-1}}\rfloor b_{n-1}
\end{array}
\right.
\end{split}
\end{align*}\]
\[\therefore\begin{align*}
\begin{split}
\left\{
\begin{array}{ll}
b_{n-1}x+(a_{n-1}-\lfloor \frac{a_{n-1}}{b_{n-1}}\rfloor b_{n-1})y=c\\
b_{n-1}x_n+(a_{n-1}-\lfloor \frac{a_{n-1}}{b_{n-1}}\rfloor b_{n-1})y_n=c\Rrightarrow a_{n-1}y_n+b_{n-1}(x_n-\lfloor\frac{a_{n-1}}{b_{n-1}}\rfloor y_n)
\end{array}
\right.
\end{split}
\end{align*}\]
\[\therefore\begin{align*}
\begin{split}
\left\{
\begin{array}{ll}
x_{n-1}=y_n\\
y_{n-1}=x_n-\lfloor\frac{a_{n-1}}{b_{n-1}}\rfloor y_n
\end{array}
\right.
\end{split}
\end{align*}\]
\[\therefore\begin{align*}
\begin{split}
\left\{
\begin{array}{ll}
x_{i-1}=y_i\\
y_{i-1}=x_i-\lfloor\frac{a_{i-1}}{b_{i-1}}\rfloor y_i
\end{array}
\right.
\end{split}
\end{align*}\]
\[由此可得 a_1x+b_1y=c的解 (x_1,y_1)
\]
\[又\because a_nx+b_ny=c的一组解(x_n,y_n),且保证 p|c 时一定存在 (x_n,y_n)。
\]
\[\therefore ax+by=c一定有解
\]
证毕
实现
int exgcd(int a,int b,int &x,int &y)
{
if(!b)
{
x=1;
y=0;
return a;
}
int d=exgcd(b,a%b,y,x);
y-=(a/b)*x;
return d;
}
费马小定理
定义
对于素数 \(p\) 有 \(a^p \equiv a(\mod p)\)
证明
证:
数学归纳法:
\[当 a=1 时,显然成立
\]
\[当 a=a 时,令 p|a^p-a
\]
\[当 a=a+1 时,试证明 p|(a+1)^p-(a+1)
\]
\[由二项式定理得:
\]
\[(a+1)^p-(a+1)=\sum\limits_{k=0}^{p}C_p^k1^{p-k}-a-1=C_p^0a^01^p\sum\limits_{k=1}^{p-1}C_p^ka^k1^{p-k}+C_p^pa^p1^0-a-1=\sum\limits_{k=1}^{p-1}C_p^ka^k1^{p-k}+a^p-a
\]
\[\because C_p^k=\frac{\prod\limits_{x=1}^{p}x}{\prod\limits_{y=1}^{k}y\prod\limits_{z=1}^{p-k}z} 且 p 为素数
\]
\[\therefore p|\sum\limits_{k=1}^{p-1}C_p^ka^k1^{p-k}
\]
\[又\because p|a^p-a
\]
\[\therefore p|\sum\limits_{k=1}^{p-1}C_p^ka^k1^{p-k}+a^p-a
\]
\[即 p|(a+1)^p-(a+1)
\]
\[可得 a^p \equiv a(\mod p)
\]
证毕
拓展
在 p 与 a 互质时,\(a^{p-1} \equiv 1(\mod p)\)
乘法逆元
定义
若整数 \(a\) 与 \(m\) 互质,并且对于任意的整数 \(b\) 都有 \(a\) 能整除 \(b\),则存在一个整数 \(x\) 使得 \(\frac{b}{a} \equiv b \times x ( \mod m)\),则称 \(x\) 为 \(a \mod m\) 的乘法逆元,记为 \(a^{-1}(\mod m)\) 或 \(inv(a)\)。
证明
若在 \(\mod m\) 的情况下, \(a\) 有逆元 \(x\),则:
\[\frac{b}{a} = b \times a^{-1}
\]
\[inv(a) \equiv a^{-1}(\mod m)
\]
\[\frac{b}{a} \equiv b \times inv(a)(\mod m)
\]
证:
\[令:\frac{b}{a} \mod m = n,x 为 a 的乘法逆元。
\]
\[b \mod m = n \times a \mod m
\]
\[x \times a \equiv 1(\mod m)
\]
\[b \times x \equiv n \times a \times x(\mod m)
\]
\[b \times x \equiv n(\mod m)
\]
\[b \times x \mod m = n \mod m
\]
\[b \times x \mod m = \frac{b}{a} \mod m
\]
证毕
性质
当且仅当 \(a\) 与 \(m\) 互质时,\(a\) 关于 \(1 \mod m\) 的乘法逆元有解。当 \(m\) 为质数时,\(a^{m-2}\) 为 \(a\) 的乘法逆元。
证明1
为什么当且仅当 \(a\) 与 \(m\) 互质时,\(a\) 关于 \(1 \mod m\) 的乘法逆元有解?
证:
\[\because a \times x \equiv 1(\mod m)
\]
\[\therefore a \times x + m \times y = 1
\]
\[由裴蜀定理得:\gcd(a,m)=1时有解
\]
\[\therefore 当且仅当 a 与 m 互质时,a 关于 1 \mod m 的乘法逆元有解
\]
证毕
证明2
为什么当 \(m\) 为质数时,\(a^{m-2}\) 为 \(a\) 的乘法逆元。
证:
\[由费马小定理得:a^{m-1} \equiv 1(\mod m)
\]
\[\therefore a \times a^{m-2} \equiv 1(\mod m)
\]
\[\therefore a^{m-2} 在 m 为质数时为 a 的乘法逆元
\]
证毕
求乘法逆元
费马小定理
由费马小定理可得,在 \(a\) 与 \(m\) 互质时,\(a^{m-1} \equiv 1(\mod m)\)
所以 \(a^{m-2} \times a \equiv 1(\mod m)\)
所有 \(a^{m-2}\) 为 \(a\) 的乘法逆元。
用快速幂计算即可。
AC Code of LibreOJ 110 乘法逆元
#include<bits/stdc++.h>
#define int long long
#define pii pair<int,int>
#define x first
#define y second
#define rep1(i,l,r) for(int i=l;i<=r;i++)
#define rep2(i,l,r) for(int i=l;i>=r;i--)
const int N=1e5+10;
using namespace std;
int n,p;
inline int read()
{
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-') f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
x=(x<<1)+(x<<3)+(ch^48);
ch=getchar();
}
return f*x;
}
int power(int a,int b,int p)//快速幂
{
int ans=1;
while(b)
{
if(b&1) ans=ans*a%p;
b>>=1;
a=(a*a)%p;
}
return ans;
}
signed main()
{
scanf("%lld %lld",&n,&p);
rep1(i,1,n) printf("%lld\n",power(i,p-2,p));//逆元
return 0;
}
拓展欧几里得
\(ax \equiv 1(\mod m)\) 就是在求解 \(ax+my=1\)。所以使用 exgcd。
AC Code of LibreOJ 110 乘法逆元
#include<bits/stdc++.h>
#define int long long
#define pii pair<int,int>
#define x first
#define y second
#define rep1(i,l,r) for(int i=l;i<=r;i++)
#define rep2(i,l,r) for(int i=l;i>=r;i--)
const int N=1e5+10;
using namespace std;
int n,p,x,y;
inline int read()
{
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-') f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
x=(x<<1)+(x<<3)+(ch^48);
ch=getchar();
}
return f*x;
}
void exgcd(int a,int b)//exgcd
{
if(!b)
{
x=1;
y=0;
return;
}
exgcd(b,a%b);
int t=x;
x=y;
y=t-a/b*y;
return;
}
signed main()
{
n=read();
p=read();
rep1(i,1,n)
{
exgcd(i,p);
printf("%lld\n",(x%p+p)%p);
}
return 0;
}
线性递推
void inv_init(int n,int p)
{
inv[1]=1;
rep1(i,2,n) inv[i]=(p-p/i)*inv[p%i]%p;
}