当前位置:网站首页>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
边栏推荐
- SAP pi / PO function operation status monitoring and inspection
- Unfortunately, I broke the leader's confidential documents and spit blood to share the code skills of backup files
- 高薪程序员&面试题精讲系列91之Limit 20000加载很慢怎么解决?如何定位慢SQL?
- OpenCV中的图像处理 —— 轮廓入门+轮廓特征
- Redis 过期 key 清理删除策略汇总
- Random neurons and random depth of dropout Technology
- How to render web pages
- Golang force buckle leetcode 396 Rotation function
- How to protect open source projects from supply chain attacks - Security Design (1)
- JS DOM event
猜你喜欢
[SQL Server fast track] view and cursor of database
JS DOM event
A must see wechat applet development guide 1 - basic knowledge
DVWA range practice
Amazon cloud technology entry Resource Center, easy access to the cloud from 0 to 1
AI上推荐 之 MMOE(多任务yyds)
Applet error: cannot read property'currenttarget'of undefined
Applet error: should have URL attribute when using navigateto, redirectto or switchtab
Exclusive thoughts and cases of JS
npm ERR! network
随机推荐
Kernel PWN learning (3) -- ret2user & kernel ROP & qwb2018 core
1 + X cloud computing intermediate -- script construction, read-write separation
[SQL Server fast track] view and cursor of database
golang力扣leetcode 396.旋转函数
Secrets in buffctf file 1
Simple understanding of arguments in JS
Codeforces Round #784 (Div. 4)
MySQL of database -- basic common query commands
Detailed explanation of delete, truncate and drop principles in MySQL database
STM32 and FreeRTOS stack parsing
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
Three ways to create objects in JS
SAP pi / PO soap2proxy consumption external WS example
RSA encryption and decryption signature verification
Learn FPGA (from Verilog to HLS)
Sql1 [geek challenge 2019]
重载、重写、隐藏的对比
ASUS laptop can't read USB and surf the Internet after reinstalling the system
Operation not allowed for a result set of type resultset TYPE_ FORWARD_ ONLY. Explain in detail