-
欧拉函数
- 欧拉函数 φ ( N ) \varphi(N) φ(N) : 1-N中与N互质的数的个数
- 若 N = p 1 a 1 ⋅ p 2 a 2 ⋅ p 3 a 3 ⋅ ⋅ ⋅ ⋅ p n a n N = p_1^{a_1} · p_2^{a_2} · p_3^{a_3} ··· ·p_n^{a_n} N=p1a1⋅p2a2⋅p3a3⋅⋅⋅⋅pnan 其中p为N的所有质因子
- 则 φ ( N ) = N ( 1 − 1 p 1 ) ( 1 − 1 p 2 ) ⋅ ⋅ ⋅ ( 1 − 1 p n ) \varphi(N) = N(1-\frac{1}{p_1})(1-\frac{1}{p_2})···(1-\frac{1}{p_n}) φ(N)=N(1−p11)(1−p21)⋅⋅⋅(1−pn1)
-
证明:
- 互质:两数的公共因子只有1
- 去掉所有与N有(大于1的)公共因子的数,剩下的数就是与N互质的数
- 对N的所有质因子 p k p_k pk,去掉所有 质数 p k 的倍数 ‾ \underline{质数p_k的倍数} 质数pk的倍数(与N有公共因子的数), 每个质数的倍数 ‾ \underline{每个质数的倍数} 每个质数的倍数个数为 N p k \frac{N}{p_k} pkN,即 N − N p 1 − N p 2 − ⋅ ⋅ ⋅ − N p n N - \frac{N}{p_1} - \frac{N}{p_2} - ··· -\frac{N}{p_n} N−p1N−p2N−⋅⋅⋅−pnN
- 对于数 p i ⋅ p j p_i·p_j pi⋅pj,在去掉 p i p_i pi 的倍数和去掉 p j p_j pj 的倍数的过程中去除了两次,所以要加上一次,即 N − ( N p 1 + N p 2 + ⋅ ⋅ ⋅ + N p n ) + ( N p 1 p 2 + N p 1 p 3 + ⋅ ⋅ ⋅ + N p n − 1 p n ) N - (\frac{N}{p_1} + \frac{N}{p_2} + ··· +\frac{N}{p_n}) + (\frac{N}{p_1p_2} + \frac{N}{p_1p_3} + ··· +\frac{N}{p_{n-1}p_n}) N−(p1N+p2N+⋅⋅⋅+pnN)+(p1p2N+p1p3N+⋅⋅⋅+pn−1pnN)
- 对于数 p i ⋅ p j ⋅ p k p_i·p_j·p_k pi⋅pj⋅pk,在去掉 p k p_k pk的倍数的过程中被去掉了三次,在加上合数 p i ⋅ p j p_i·p_j pi⋅pj 的过程中被加上了三次,所以合数 p i ⋅ p j ⋅ p k p_i·p_j·p_k pi⋅pj⋅pk 没有被去掉,因此要去掉它,即 N − ( N p 1 − N p 2 − ⋅ ⋅ ⋅ − N p n ) + ( N p 1 p 2 + N p 1 p 3 + ⋅ ⋅ ⋅ + N p n − 1 p n ) − ( N p 1 p 2 p 3 + N p 1 p 2 p 4 + ⋅ ⋅ ⋅ + N p n − 2 p n − 1 p n ) N - (\frac{N}{p_1} - \frac{N}{p_2} - ··· -\frac{N}{p_n}) + (\frac{N}{p_1p_2} + \frac{N}{p_1p_3} + ··· +\frac{N}{p_{n-1}p_n}) - (\frac{N}{p_1p_2p_3} + \frac{N}{p_1p_2p_4} + ··· +\frac{N}{p_{n-2}p_{n-1}p_n}) N−(p1N−p2N−⋅⋅⋅−pnN)+(p1p2N+p1p3N+⋅⋅⋅+pn−1pnN)−(p1p2p3N+p1p2p4N+⋅⋅⋅+pn−2pn−1pnN)
- 对于合数 p i ⋅ p j ⋅ p k ⋅ p m p_i·p_j·p_k·p_m pi⋅pj⋅pk⋅pm 同理,归纳递推可知,所有质数个数为: N − ∑ i = 1 n N p i + ∑ 1 < = i < j < = n N p i p j − ∑ 1 < = i < j < k < = n N p i p j p k + ⋅ ⋅ ⋅ + ( − 1 ) 2 n − 1 ∑ 1 < = i < j < k < ⋅ ⋅ ⋅ < = n N p i p j p k ⋅ ⋅ ⋅ p N - \displaystyle\sum^n_{i=1}{\frac{N}{p_i}}+\displaystyle \sum_{1<=i<j<=n}{\frac{N}{p_ip_j}}-\displaystyle \sum_{1<=i<j<k<=n}{\frac{N}{p_ip_jp_k}}+···+(-1)^{2n-1}\displaystyle \sum_{1<=i<j<k<···<=n}{\frac{N}{p_ip_jp_k···p}} N−i=1∑npiN+1<=i<j<=n∑pipjN−1<=i<j<k<=n∑pipjpkN+⋅⋅⋅+(−1)2n−11<=i<j<k<⋅⋅⋅<=n∑pipjpk⋅⋅⋅pN
- 同样基于容斥原理:
- ∣ A ∪ B ∪ C ∣ = ∣ A ∣ + ∣ B ∣ + ∣ C ∣ − ∣ A ∩ B ∣ − ∣ A ∩ C ∣ − ∣ B ∩ C ∣ + ∣ A ∩ B ∩ C ∣ |A\cup B\cup C|=|A|+|B|+|C|-|A\cap B|-|A\cap C|-|B\cap C|+|A\cap B\cap C| ∣A∪B∪C∣=∣A∣+∣B∣+∣C∣−∣A∩B∣−∣A∩C∣−∣B∩C∣+∣A∩B∩C∣
- ∣ ∪ i = 1 n A i ∣ = ∑ i ∣ A i ∣ − ∑ i , j ∣ A i ∩ A j ∣ + … + ( − 1 ) n + 1 ∣ ∩ i = 1 n A i ∣ |\displaystyle \cup_{i=1}^n A_i |=\sum_{i}|A_i|-\sum_{i,j} |A_i \cap A_j|+\ldots +(-1)^{n+1}|\cap_{i=1}^n A_i | ∣∪i=1nAi∣=i∑∣Ai∣−i,j∑∣Ai∩Aj∣+…+(−1)n+1∣∩i=1nAi∣
- 与N有(大于1的)公共因子的个数 =| p i p_i pi 的倍数的个数| - | p i p j p_ip_j pipj 的倍数的个数| + | p i p j p k p_ip_jp_k pipjpk 的倍数的个数 ··· ( − 1 ) n + 1 (-1)^{n+1} (−1)n+1 | p i p j ⋅ ⋅ ⋅ p n p_ip_j···p_n pipj⋅⋅⋅pn 的倍数的个数|
- 将上式因式分解后: N ( 1 − 1 p 1 ) ( 1 − 1 p 2 ) ⋅ ⋅ ⋅ ( 1 − 1 p n ) N(1-\frac{1}{p_1})(1-\frac{1}{p_2})···(1-\frac{1}{p_n}) N(1−p11)(1−p21)⋅⋅⋅(1−pn1)
-
容斥原理:
int res = a; for(int i = 2; i <= a / i; ++ i){ if(a % i == 0){//i为质因子 res = res / i * (i - 1);//套公式 while(a % i == 0) a /= i;//把因子除干净 } } if(a > 1) res = res / a * (a - 1);//最后一个因子可能大于sqrt(a)
-
筛选法:
- 利用线性筛选质数的过程求出每个数的欧拉函数
- 欧拉函数为积性函数,当a与b互质时有 φ ( a ⋅ b ) = φ ( a ) ⋅ φ ( b ) \varphi(a·b)=\varphi(a)·\varphi(b) φ(a⋅b)=φ(a)⋅φ(b)
- 当i为质数时, φ ( i ) = i − 1 \varphi(i)=i-1 φ(i)=i−1
- 当 i p j = 0 \frac{i}{p_j} = 0 pji=0时, p j p_j pj为i的质因子,此时 p j ⋅ i p_j·i pj⋅i的质因子与i的质因子完全相同,所以 φ ( p j ⋅ i ) = p j ⋅ i ⋅ ( 1 − 1 p 1 ) ( 1 − 1 p 2 ) ⋅ ⋅ ⋅ ( 1 − 1 p n ) = p j ⋅ φ ( i ) \varphi(pj·i)=pj·i·(1-\frac{1}{p_1})(1-\frac{1}{p_2})···(1-\frac{1}{p_n})=p_j·\varphi(i) φ(pj⋅i)=pj⋅i⋅(1−p11)(1−p21)⋅⋅⋅(1−pn1)=pj⋅φ(i)
- 当 i p j ≠ 0 \frac{i}{p_j} \neq 0 pji=0时, p j p_j pj不为i的质因子,此时 p j ⋅ i p_j·i pj⋅i的质因子比i的质因子多一个 p j p_j pj,所以 φ ( p j ⋅ i ) = p j ⋅ i ⋅ ( 1 − 1 p 1 ) ( 1 − 1 p 2 ) ⋅ ⋅ ⋅ ( 1 − 1 p n ) ( 1 − 1 p j ) = p j ⋅ φ ( i ) ⋅ ( 1 − 1 p j ) = ( p j − 1 ) ⋅ φ ( i ) \varphi(pj·i)=pj·i·(1-\frac{1}{p_1})(1-\frac{1}{p_2})···(1-\frac{1}{p_n})(1-\frac{1}{p_j})=p_j·\varphi(i)·(1-\frac{1}{p_j})=(p_j - 1)·\varphi(i) φ(pj⋅i)=pj⋅i⋅(1−p11)(1−p21)⋅⋅⋅(1−pn1)(1−pj1)=pj⋅φ(i)⋅(1−pj1)=(pj−1)⋅φ(i)
- 代码:
const int N = 1e6 + 10; typedef long long LL; int primes[N], cnt;//质数数组,下标 int st[N];//标记为合数 int phi[N];//欧拉函数 int n; LL get_eulers(int n){ phi[1] = 1; for(int i = 2; i <= n; ++ i){ if(!st[i]){ primes[ ++ cnt] = i; phi[i] = i - 1;//质数i的欧拉函数为i - 1 } for(int j = 1; primes[j] <= n / i; ++ j){ int pj = primes[j]; st[pj * i] = 1; if(i % pj == 0){//i是合数 phi[pj * i] = pj * phi[i];//pj为i的质因子 break; } else phi[pj * i] = (pj - 1) * phi[i];//pj不是i的因子 } } LL ans = 0; for(int i = 1; i <= n; ++ i) ans += phi[i]; return ans; }
-
欧拉函数的应用
-
欧拉定理
- 若a与n互质,则 a φ ( n ) ( m o d n ) ≡ 1 a^{\varphi(n)}\pmod n\equiv1 aφ(n)(modn)≡1
- 证明:
- 若a与b互质,且 a ( m o d b ) ≠ 0 a\pmod b\neq 0 a(modb)=0则 a ( m o d b ) a\pmod b a(modb) 也与b互质
- 设 a 1 , a 2 , a 3 ⋅ ⋅ ⋅ a φ ( n ) a_1,a_2,a_3···a_{\varphi(n)} a1,a2,a3⋅⋅⋅aφ(n) 为1~n中的所有与n互质的数
- 每个数同时乘上a得 a ⋅ a 1 , a ⋅ a 2 , a ⋅ a 3 ⋅ ⋅ ⋅ a ⋅ a φ ( n ) a·a_1,a·a_2,a·a_3···a·a_{\varphi(n)} a⋅a1,a⋅a2,a⋅a3⋅⋅⋅a⋅aφ(n) 由于a也与n互质,所以乘a后所得的数也全部与n互质
- 将每个数mod n,取模后的每个数都在1~n范围内,并且仍然都与n互质,显然取模后的这几个数就是原来的几个数,只不过数的顺序可能发生了变化
- 将所有数相乘,则有 a ⋅ a 1 ⋅ a ⋅ a 2 ⋅ a ⋅ a 3 ⋅ ⋅ ⋅ a ⋅ a φ ( n ) ( m o d n ) = a 1 ⋅ a 2 ⋅ a 3 ⋅ ⋅ ⋅ a φ ( n ) a·a_1·a·a_2·a·a_3···a·a_{\varphi(n)} \pmod n=a_1·a_2·a_3···a_{\varphi(n)} a⋅a1⋅a⋅a2⋅a⋅a3⋅⋅⋅a⋅aφ(n)(modn)=a1⋅a2⋅a3⋅⋅⋅aφ(n)
- 所以有: a φ ( n ) ⋅ ( a 1 a 2 a 3 ⋅ ⋅ ⋅ a φ ( n ) ) ( m o d n ) ≡ ( a 1 ⋅ a 2 ⋅ a 3 ⋅ ⋅ ⋅ a φ ( n ) ) a^{\varphi(n)}·(a_1a_2a_3···a_{\varphi(n)})\pmod n \equiv (a_1·a_2·a_3···a_{\varphi(n)}) aφ(n)⋅(a1a2a3⋅⋅⋅aφ(n))(modn)≡(a1⋅a2⋅a3⋅⋅⋅aφ(n))即 a φ ( n ) ( m o d n ) ≡ 1 a^{\varphi(n)}\pmod n\equiv1 aφ(n)(modn)≡1
- 特别地,当n为质数时, a n − 1 ( m o d n ) ≡ 1 a^{n-1}\pmod n\equiv1 an−1(modn)≡1 为费马定理
-
-
快速幂
- 快速地求出 a k ( m o d b ) a^k\pmod b ak(modb),时间复杂度为== O ( l o g ( k ) ) O(log(k)) O(log(k))==
- 将k拆分为二进制相加,即
- k = c 1 ⋅ 2 1 + c 2 ⋅ 2 2 + c 3 ⋅ 2 3 + ⋅ ⋅ ⋅ + c n ⋅ 2 n k=c_1·2^1+c_2·2^2+c_3·2^3+···+c_n·2^n k=c1⋅21+c2⋅22+c3⋅23+⋅⋅⋅+cn⋅2n
- 其中 c i c_i ci 是k的二进制的每一位(0/1),n最大为k的二进制的位数
- 所以
- a k ( m o d b ) = a c 1 ⋅ 2 1 + c 2 ⋅ 2 2 + c 3 ⋅ 2 3 + ⋅ ⋅ ⋅ + c n ⋅ 2 n ( m o d b ) = a c 1 ⋅ 2 1 ⋅ a c 2 ⋅ 2 2 ⋅ a c 3 ⋅ 2 3 ⋅ ⋅ ⋅ a c n ⋅ 2 n ( m o d b ) a^k\pmod b=a^{c_1·2^1+c_2·2^2+c_3·2^3+···+c_n·2^n} \pmod b=a^{c_1·2^1}·a^{c_2·2^2}·a^{c_3·2^3}···a^{c_n·2^n} \pmod b ak(modb)=ac1⋅21+c2⋅22+c3⋅23+⋅⋅⋅+cn⋅2n(modb)=ac1⋅21⋅ac2⋅22⋅ac3⋅23⋅⋅⋅acn⋅2n(modb)
- 所以每次遍历k的所有二进制位,同时预处理出 a i a^i ai,根据k的二进制的取值将答案累乘
- 代码:
typedef long long LL; //求a^k mod b int qmi(int a, int k, int b){ int res = 1; while(k){ if(k & 1) res = (LL)res * a % b;//k的当前位非0,则将a累乘到答案 k >>= 1;//k右移一位 a = (LL)a * a % b;//a的幂倍增 } return res; }
-
快速幂求逆元
- 若b与p互质,且 b ∣ a b|a b∣a, a b = a ⋅ x ( m o d p ) \frac a b=a·x \pmod p ba=a⋅x(modp) ,则x为b的逆元
- 两边乘b有 a = a ⋅ b ⋅ x ( m o d p ) a=a·b·x \pmod p a=a⋅b⋅x(modp)
- 所以有 b ⋅ x = 1 ( m o d p ) b·x=1 \pmod p b⋅x=1(modp)
- 若p是质数,有费马定理 b p − 1 = 1 ( m o d p ) b^{p-1} = 1 \pmod p bp−1=1(modp)则 x = b p − 2 x=b^{p-2} x=bp−2
- 若p不是质数,有欧拉定理 b φ ( p ) = 1 ( m o d p ) b^{\varphi(p)}=1 \pmod p bφ(p)=1(modp)则 x = b φ ( p ) − 1 x=b^{\varphi(p)- 1} x=bφ(p)−1
- 代码:
int qmi(int a, int b, int p){ 略... } if(b与q互质){ //互质是有解的前提 if(p为质数) cout << qmi(b, p - 2, p); else cout << qmi(b, phi[p] - 1, p); }else 无解
-
扩展欧几里得定理
-
裴蜀定理:对于任意正整数a,b, 都一定存在整数x,y, 使得 a x + b y = g c d ( a , b ) ax+by=gcd(a,b) ax+by=gcd(a,b), 并且 g c d ( a , b ) gcd(a,b) gcd(a,b) 一定是a与b能构造出来的最小公约数
-
扩展欧几里得算法就是求这样的x,y
-
用于求解方程 a x + b y = g c d ( a , b ) ax+by=gcd(a,b) ax+by=gcd(a,b) 的解
-
当 b = 0 b=0 b=0 时 a x + b y = a ax+by=a ax+by=a 故而 x = 1 , y = 0 x=1,y=0 x=1,y=0
-
当 b ≠ 0 b \neq 0 b=0 时,因为
-
g c d ( a , b ) = g c d ( b , a % b ) gcd(a,b)=gcd(b,a \% b) gcd(a,b)=gcd(b,a%b)
-
而
-
b ⋅ x 1 + ( a % b ) ⋅ y 1 = g c d ( b , a % b ) b·x_1+(a \% b)·y_1 = gcd(b,a\%b) b⋅x1+(a%b)⋅y1=gcd(b,a%b)
-
b ⋅ x 1 + ( a − ⌊ a / b ⌋ ⋅ b ) ⋅ y 1 = g c d ( b , a % b ) b·x_1+(a-\lfloor a/b \rfloor · b)·y_1=gcd(b,a\%b) b⋅x1+(a−⌊a/b⌋⋅b)⋅y1=gcd(b,a%b)
-
a y 1 + b ⋅ ( x 1 − ⌊ a / b ⌋ ) = g c d ( b , a % b ) = g c d ( a , b ) ay_1+b·(x_1-\lfloor a/b \rfloor)=gcd(b,a\%b)=gcd(a,b) ay1+b⋅(x1−⌊a/b⌋)=gcd(b,a%b)=gcd(a,b)
-
故而 x = y 1 , y = x 1 − ⌊ a / b ⌋ ⋅ y 1 x=y_1,\quad y=x_1-\lfloor a/b \rfloor ·y_1 x=y1,y=x1−⌊a/b⌋⋅y1
-
代码:
int exgcd(int a, int b, int &x, int &y){ if(!b){ //b = 0时,ax + by = gcd(a, 0) = a,所以x = 1, y = 0; x = 1, y = 0; return a; }else { int d = exgcd(b, a % b, x, y);//交换a与b,x与y int t = x; x = y;//x1 = y2 y = t - a / b * y;//y1 = x2 - a / b * y2 return d; } } //简化版 void exgcd(int a, int b, int &x, int &y){ if(!b){ x = 1, y = 0; return a; }else{ int d = exgcd(b, a % b, y, x); y -= a / b * x; return d; } }
-
扩展欧几里得求解线性同余方程
- 线性同余方程: a x ≡ b ( m o d m ) ax \equiv b \pmod m ax≡b(modm) 其中a,x,b,m均为整数,x为未知数,方程等价于 a x = k m + b ax=km+b ax=km+b,也等价于 a x + m k = b ax+mk=b ax+mk=b其中x,k均为未知数
- 方程形如: a x + b y = c ax+by=c ax+by=c 的直线方程,解(x,y)为直线上散列的点
- 求出a,b的最大公约数 d = g c d ( a , b ) d = gcd(a,b) d=gcd(a,b),方程化为 d ( x a d + y b d ) = c d(x \frac a d+y\frac b d)=c d(xda+ydb)=c易知其中 x a d + y b d x \frac a d+y\frac b d xda+ydb 为整数,所以要求 d ∣ c d|c d∣c,故 c = k ⋅ d = k ⋅ g c d ( a , b ) c=k·d=k·gcd(a,b) c=k⋅d=k⋅gcd(a,b)
- 所以线性同余方程 a x ≡ b ( m o d m ) ax \equiv b \pmod m ax≡b(modm)有解的充要条件为b为 g c d ( a , m ) gcd(a,m) gcd(a,m)的倍数
- 当 b ≠ k ′ ⋅ g c d ( a , m ) b \neq k'·gcd(a,m) b=k′⋅gcd(a,m) 时( k ′ k' k′ 为整数),方程无解
- 当 b = k ′ ⋅ g c d ( a , m ) b = k'·gcd(a,m) b=k′⋅gcd(a,m) 时,先用扩展欧几里得求出方程 a x + m k = g c d ( a , m ) ax+mk=gcd(a,m) ax+mk=gcd(a,m) 的特解 ( x , k ) (x,k) (x,k),可知 a x + m k = b ax+mk=b ax+mk=b 方程的解为 b g c d ( a , m ) ⋅ ( x , k ) \frac {b}{gcd(a,m)}·(x,k) gcd(a,m)b⋅(x,k) ,所以原式的解为 b g c d ( a , m ) ⋅ x \frac {b}{gcd(a,m)}·x gcd(a,m)b⋅x
- 代码:
const int N = 1e5 + 10; typedef long long LL; int n; int a, b, m; int x, y; //扩展欧几里得算法 int exgcd(int a, int b , int &x, int &y){ if(!b){ x = 1, y = 0; return a; }else{ int t = exgcd(b, a % b, y, x); y -= a / b * x; return t; } } int main(){ cin >> n; while(n -- ){ cin >> a >> b >> m; int t = exgcd(a, m, x, y); //b是gcd(a,m)的倍数才有解 if(b % t != 0) cout << "impossible" << endl; else cout << (LL)x * (b / t) % m << endl;//特解乘上倍数 } return 0; }
-
-
中国剩余定理
- 中国剩余定理 (Chinese Remainder Theorem, CRT) 可求解如下形式的一元线性同余方程组(其中 n 1 , n 2 , n 3 , ⋅ ⋅ ⋅ n k n_1,n_2,n_3,···n_k n1,n2,n3,⋅⋅⋅nk 两两互质):
- { x ≡ a 1 ( m o d n 1 ) x ≡ a 2 ( m o d n 2 ) ⋮ x ≡ a k ( m o d n k ) \begin{cases} x &\equiv a_1 \pmod {n_1} \\ x &\equiv a_2 \pmod {n_2} \\ &\vdots \\ x &\equiv a_k \pmod {n_k} \\ \end{cases} ⎩ ⎨ ⎧xxx≡a1(modn1)≡a2(modn2)⋮≡ak(modnk)
- 过程:
- 计算所有模数的积
- 对于第
i
i
i 个方程:
- 计算 m i = n n i m_i=\frac{n}{n_i} mi=nin
- 计算 m i m_i mi 在模 n i n_i ni 意义下的逆元 m i − 1 m^{-1}_i mi−1
- 计算 c i = m i m i − 1 c_i=m_im^{-1}_i ci=mimi−1 (不要对 n i n_i ni 取模)
- 方程组在模 n n n 意义下的唯一解为: x = ∑ i = 1 k a i c i ( m o d n ) x=\sum^{k}_{i=1}a_ic_i \pmod n x=∑i=1kaici(modn)
-
扩展版中国剩余定理:
- 当不满足 n 1 , n 2 , n 3 , ⋅ ⋅ ⋅ n k n_1,n_2,n_3,···n_k n1,n2,n3,⋅⋅⋅nk 两两互质时,求解线性同余方程组
- ① x ≡ a 1 ( m o d n 1 ) ⇔ x = k 1 n 1 + a 1 ①\quad x\equiv a_1\pmod {n_1} \Leftrightarrow x=k_1n_1+a_1 ①x≡a1(modn1)⇔x=k1n1+a1
- ② x ≡ a 2 ( m o d n 2 ) ⇔ x = k 2 n 2 + a 2 ②\quad x \equiv a_2\pmod {n_2} \Leftrightarrow x=k_2n_2+a_2 ②x≡a2(modn2)⇔x=k2n2+a2
- 由①②知 k 1 n 1 + a 1 = k 2 n 2 + a 2 k_1n_1+a_1=k_2n_2+a_2 k1n1+a1=k2n2+a2
- 移项 k 1 n 1 − k 2 n 2 = a 2 − a 1 k_1n_1-k_2n_2=a_2-a_1 k1n1−k2n2=a2−a1
- 其中 n 1 , n 2 , a 2 , a 1 n_1,n_2,a_2,a_1 n1,n2,a2,a1 已知, k 1 , k 2 k_1,k_2 k1,k2 未知,可以用扩展欧几里得算法求出
- 当 d = a 2 − a 1 g c d ( n 1 , n 2 ) ≠ 0 d=\frac {a_2-a_1}{gcd(n_1,n_2)} \neq 0 d=gcd(n1,n2)a2−a1=0 时无解
- 当 d = a 2 − a 1 g c d ( n 1 , n 2 ) = 0 d=\frac {a_2-a_1}{gcd(n_1,n_2)} = 0 d=gcd(n1,n2)a2−a1=0 时,先求出方程
- k 1 ‘ n 1 − k 2 ‘ n 2 = g c d ( n 1 , n 2 ) k_1^`n_1-k_2^`n_2=gcd(n_1,n_2) k1‘n1−k2‘n2=gcd(n1,n2)
- 的特解,求出 k 1 ‘ , k 2 ‘ k_1^`,k_2^` k1‘,k2‘ 后
- k 1 ∗ = k 1 ‘ ⋅ d , k 2 ∗ = k 2 ‘ ⋅ d k_1^*=k_1^`·d,\quad k_2^*=k_2^`·d k1∗=k1‘⋅d,k2∗=k2‘⋅d
- 此时 k 1 ∗ , k 2 ∗ k_1^*,k_2^* k1∗,k2∗ 就是原方程的特解
- 观察原方程可知通解为
- k 1 = k 1 ∗ + k ⋅ n 2 d , k 2 = k 2 ∗ + k ⋅ n 1 d ( 其中 k 为整数 ) k_1=k_1^*+k·\frac{n_2}{d},\quad k_2=k_2^*+k·\frac{n_1}{d}(其中k为整数) k1=k1∗+k⋅dn2,k2=k2∗+k⋅dn1(其中k为整数)
- 将通解代入①式得
- x = ( k 1 ∗ + k ⋅ n 2 d ) n 1 + a 1 = ( k 1 ∗ n 1 ) + a 1 + k ⋅ n 1 n 2 d = ( k 1 ∗ n 1 ) + a 1 + k ⋅ l c m ( n 1 , n 2 ) x=(k_1^*+k·\frac{n_2}{d})n_1+a_1=(k_1^*n_1)+a_1+k·\frac{n_1n_2}{d}=(k_1^*n_1)+a_1+k·lcm(n_1,n_2) x=(k1∗+k⋅dn2)n1+a1=(k1∗n1)+a1+k⋅dn1n2=(k1∗n1)+a1+k⋅lcm(n1,n2)
- 所以
- ③③ x = k ⋅ l c m ( n 1 , n 2 ) + ( k 1 ∗ n 1 ) + a 1 ③③\quad x=k·lcm(n_1,n_2)+(k_1^*n_1)+a_1 ③③x=k⋅lcm(n1,n2)+(k1∗n1)+a1
- 其中 k 1 ∗ , n 1 , a 1 , l c m ( n 1 , n 2 ) k_1^*,n_1,a_1,lcm(n_1,n_2) k1∗,n1,a1,lcm(n1,n2) 已知, k k k 未知
- 令 n 2 ‘ = l c m ( n 1 , n 2 ) , a 2 ‘ = ( k 1 ∗ n 1 ) + a 1 n_2^`=lcm(n_1,n_2),\quad a_2^`=(k_1^*n_1)+a_1 n2‘=lcm(n1,n2),a2‘=(k1∗n1)+a1 所以③式化为
- ③ x = k ⋅ n 2 ‘ + a 2 ‘ ③\quad x=k·n_2^`+a_2^` ③x=k⋅n2‘+a2‘
- 所以将①②式合并后的③式仍与方程组方程相似,于是可以循环将方程组合并为一个式子,通过最后一个式子即可用扩展欧几里得算法求出x
- 代码:
const int N = 30; typedef long long LL; int n; LL exgcd(LL a, LL b, LL &x, LL &y){ if(!b){ x = 1, y = 0; return a; } LL d = exgcd(b, a % b, y, x); y -= a / b * x; return d; } int main(){ cin >> n; LL a1, m1; bool flag = true; cin >> a1 >> m1;//用于存储更新的a与m for(int i = 1; i < n; ++ i){ //合并n-1次 LL an, mn;//接受新的a与m LL k1, k2; //存储用欧几里得求的系数 cin >> an >> mn; LL d = exgcd(a1, an, k1, k2); //求出gcd(a1, an)以及系数k1, k2 if((mn - m1) % d){ //无解判断 flag = false; break; } LL t = an / d; //通解k1 = k1 + k * (an / d), k2 = k2 + k * (a1 / d),题目为了求最小的x,故要让k1为最小解故要k1不断膜上t k1 = k1 * (mn - m1) / d; //更新k1 k1 = (k1 % t + t) % t; //取k1的最小解 m1 = k1 * a1 + m1; //m更新为k1 * a1 * m1 a1 = abs(a1 / d * an); //a1更新为gcd(a1, an) } if(flag){ cout << (m1 % a1 + a1) % a1; //最后一个式子的就是余数m }else cout << -1; return 0; }
[数论第二节]欧拉函数/快速幂/扩展欧几里得算法
于 2023-08-08 17:47:49 首次发布