当前位置:网站首页>Expansion of number theory Euclid
Expansion of number theory Euclid
2022-04-23 09:34:00 【Zimba_】
Summary :
First, let's look at what it looked like before evolution , That is, the well-known Euclidean algorithm , Use the division method to find gcd.
ll gcd(ll a,ll b)
{
return b?gcd(b,a%b):a;
}
And expand Euclid , It expands the application of Euclidean algorithm . It's solving gcd On the basis of , Solved an additional problem —— solve General solution of binary indefinite equation , namely a x + b y = m ax+by=m ax+by=m, among a a a, b b b, m m m For known , x x x, y y y Is an unknown quantity , Demand solution x x x and y y y Solution .
Text :
First , To meet the The necessary and sufficient conditions for the solution of the equation : [ ( a , b ) ∣ m ] [(a,b)|m] [(a,b)∣m].
If not satisfied , Then the equation has no solution .
ps: ( a , b ) (a,b) (a,b) Express a a a and b b b Maximum common divisor of , and ∣ | ∣ It's an integer division sign , That is to say m m m yes g c d ( a , b ) gcd(a,b) gcd(a,b) Multiple . I don't know how to prove this , It's easy to get , Readers think for themselves .
So , The original equation can be seen as , a x + b y = t × g c d ( a , b ) ax+by=t \times gcd(a,b) ax+by=t×gcd(a,b).
You can find , We just need to find out a x + b y = g c d ( a , b ) ax+by=gcd(a,b) ax+by=gcd(a,b) Solution , Then multiply it by t t t that will do .
The key is coming. !!!
First, we ask for a set of special solutions .
We recursively will a a a and b b b Perform rolling Division , When reaching the recursive boundary , a = g c d ( a , b ) , b = 0 a=gcd(a,b),b=0 a=gcd(a,b),b=0.
here equation g c d ( a , b ) x + 0 ∗ y = g c d ( a , b ) gcd(a,b)x+0*y=gcd(a,b) gcd(a,b)x+0∗y=gcd(a,b) We can easily get a set of solutions : x = 1 , y = 0 x=1,y=0 x=1,y=0.
We From a higher level a a a and b b b Change to the next level a a a and b b b after , The solution of the equation is also from the upper layer x x x and y y y Into the next layer x x x and y y y. So while we are recursively backtracking , Through the lower x x x and y y y To calculate the upper layer x x x and y y y.( The specific steps are omitted )
therefore , We get a set of special solutions of the equation x 0 × t x_{0} \times t x0×t, y 0 × t y_{0} \times t y0×t.
therefore , We also get the general solution of the equation x = x 0 t + k b g c d ( a , b ) , y = y 0 t − k a g c d ( a , b ) x=x_{0}t+\frac{kb}{ gcd(a,b) },y=y_{0}t-\frac{ka}{ gcd(a,b) } x=x0t+gcd(a,b)kb,y=y0t−gcd(a,b)ka.
Templates :
ll ex_gcd(ll a,ll b,ll& x,ll& y)
{
if(b==0)
{
x=1;y=0;
return a;
}
ll g=ex_gcd(b,a%b,x,y);
ll tmp=x;
x=y;
y=tmp-a/b*y;
return g;
}
application (exgcd Inverse element ): Portal
On inverse element , That is to say a − 1 a^{-1} a−1, I won't go into details here .
a In the mold p In the sense of A necessary and sufficient condition for the existence of inverse elements : [ g c d ( a , p ) = 1 ] [gcd(a,p)=1] [gcd(a,p)=1]
When modulus p Prime number , You can use Fermat's small Theorem : a p − 1 ≡ 1 ( m o d p ) a^{p-1}\equiv 1\; (mod \; p) ap−1≡1(modp)
We demand a x ≡ 1 ( m o d p ) ax\equiv 1\; (mod \; p) ax≡1(modp), here x x x As long as it equals a p − 2 a^{p-2} ap−2 Just fine .
Then when the modulus p When not prime , That's it Expand Euclid La .
Demand solution a x ≡ 1 ( m o d p ) ax\equiv 1\; (mod \; p) ax≡1(modp). In fact, it is to solve a x + p y = 1 ax+py=1 ax+py=1 A set of special solutions . See light suddenly , I won't talk about it later .
Templates ( Tuoou inverse element ):
ll ex_gcd(ll a,ll b,ll& x,ll& y)
{
if(b==0)
{
x=1;y=0;
return a;
}
ll ans=ex_gcd(b,a%b,x,y);
ll tmp=x;
x=y;
y=tmp-a/b*y;
return ans;
}
ll inv(ll a,ll mod)// Existence of inverse element condition :gcd(a,mod)=1
{
ll x,y;
ll g=ex_gcd(a,mod,x,y);
if(g!=1)return -1;
return (x%mod+mod)%mod;
}
版权声明
本文为[Zimba_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230621425835.html
边栏推荐
- Thread scheduling (priority)
- 112. Path sum
- Canary publishing using ingress
- ALV tree (ll LR RL RR) insert delete
- Golang force buckle leetcode 396 Rotation function
- ABAP publishes OData service samples from CDs view
- 阿里云架构师解读四大主流游戏架构
- SAP debug debug for in, reduce and other complex statements
- ASUS laptop can't read USB and surf the Internet after reinstalling the system
- MySQL of database -- overview and installation
猜你喜欢

成功的DevOps Leader 应该清楚的3个挑战

501. Mode in binary search tree

Simple understanding of arguments in JS

Installation of data cleaning ETL tool kettle

kernel-pwn学习(3)--ret2user&&kernel ROP&&QWB2018-core

SAP CR transmission request sequence and dependency check

Redis 内存占满导致的 Setnx 命令执行失败

ABAP 7.4 SQL Window Expression

PHP notes (I): development environment configuration

Using sqlmap injection to obtain the account and password of the website administrator
随机推荐
Codeforces Round #784 (Div. 4)
KVM installation and deployment
Kernel PWN learning (4) -- double fetch & 0ctf2018 baby
ALV tree (ll LR RL RR) insert delete
Creation of raid0 and RAID5 and Simulation of how RAID5 works
1 + X cloud computing intermediate -- script construction, read-write separation
《数字电子技术基础》3.1 门电路概述、3.2 半导体二极管门电路
Three ways to create objects in JS
RSA encryption and decryption signature verification
Leetcode0587. 安装栅栏(difficult)
Kettle实验 转换案例
Simply understand = = and equals, why can string not use new
Image processing in opencv -- Introduction to contour + contour features
Applet error: cannot read property'currenttarget'of undefined
Base de la technologie électronique numérique 3.1 aperçu du circuit de porte, 3.2 circuit de porte à diode semi - conductrice
Leetcode-199 - right view of binary tree
Installation of data cleaning ETL tool kettle
Random neurons and random depth of dropout Technology
SAP pi / PO function operation status monitoring and inspection
#yyds干货盘点#ubuntu18.0.4安装mysql并解决ERROR 1698: Access denied for user ''root''@''localhost''