当前位置:网站首页>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
边栏推荐
- How to obtain geographical location based on photos and how to prevent photos from leaking geographical location
- RSA 加密解密签名验签
- Using JS to realize a thousandth bit
- 1 + X cloud computing intermediate -- script construction, read-write separation
- Amazon cloud technology entry Resource Center, easy access to the cloud from 0 to 1
- Kettle experiment
- kettle庖丁解牛第14篇之JSON输入
- 112. 路径总和
- Program, process, thread; Memory structure diagram; Thread creation and startup; Common methods of thread
- Emuelec compilation summary
猜你喜欢

Detailed explanation of delete, truncate and drop principles in MySQL database

Principle of synchronized implementation

Comparison of overloading, rewriting and hiding

Simple understanding of arguments in JS

Unfortunately, I broke the leader's confidential documents and spit blood to share the code skills of backup files

Number of islands

SAP 101K 411k inventory change

ABAP CDs view with association example

Kettle experiment (III)

Random neurons and random depth of dropout Technology
随机推荐
Set the maximum width of the body, but why does the background color of the body cover the whole page?
npm ERR! network
《數字電子技術基礎》3.1 門電路概述、3.2 半導體二極管門電路
ABAP CDs view with association example
Sql1 [geek challenge 2019]
Kettle实验
JSON input of Chapter 14 of kettle paoding jieniu
JS what is an event? Event three elements and operation elements
Kettle experiment
Colorui solves the problem of blocking content in bottom navigation
Little girl walking
错题汇总1
653. 两数之和 IV - 输入 BST
NLLLoss+log_ SoftMax=CE_ Loss
Kernel PWN learning (4) -- double fetch & 0ctf2018 baby
Three challenges that a successful Devops leader should be aware of
Flutter's loading animation is more interesting
Creation of raid0 and RAID5 and Simulation of how RAID5 works
ABAP publishes OData service samples from CDs view
MySQL of database -- overview and installation