当前位置:网站首页>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
边栏推荐
- Comparison of overloading, rewriting and hiding
- Golang force buckle leetcode 396 Rotation function
- kettle庖丁解牛第14篇之JSON输入
- SAP 101K 411k inventory change
- 《數字電子技術基礎》3.1 門電路概述、3.2 半導體二極管門電路
- 【读书笔记】《Verilog数字系统设计教程》 第5章 条件语句、循环语句和块语句(附思考题答案)
- Simple understanding of arguments in JS
- SAP 03-amdp CDs table function using 'with' clause
- [reading notes] Chapter 5 conditional statements, circular statements and block statements of Verilog digital system design tutorial (with answers to thinking questions)
- Single sign on SSO
猜你喜欢

A must see wechat applet development guide 1 - basic knowledge

Random neurons and random depth of dropout Technology

错题汇总1

Node installation

Using sqlmap injection to obtain the account and password of the website administrator

Kettle实验 转换案例

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

Leetcode question bank 78 Subset (recursive C implementation)

Applet error: cannot read property'currenttarget'of undefined

NLLLoss+log_ SoftMax=CE_ Loss
随机推荐
Flink 流批一体在小米的实践
Creation of raid0 and RAID5 and Simulation of how RAID5 works
阿里云架构师解读四大主流游戏架构
Number theory to find the sum of factors of a ^ B (A and B are 1e12 levels)
golang力扣leetcode 396.旋转函数
AQS & reentrantlock implementation principle
Using sqlmap injection to obtain the account and password of the website administrator
Thread scheduling (priority)
NLLLoss+log_ SoftMax=CE_ Loss
《信息系统项目管理师总结》第八章 项目干系人管理
Leetcode0587. Install fence
Operation not allowed for a result set of type resultset TYPE_ FORWARD_ ONLY. Explain in detail
Program, process, thread; Memory structure diagram; Thread creation and startup; Common methods of thread
Chapter VIII project stakeholder management of information system project manager summary
Sql1 [geek challenge 2019]
SAP debug debug for in, reduce and other complex statements
108. 将有序数组转换为二叉搜索树
【SQL server速成之路】数据库的视图和游标
考研线性代数常见概念、问题总结
SAP salv14 background output salv data can directly save files and send emails (with sorting, hyperlink and filtering format)