当前位置:网站首页>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
边栏推荐
- Go language learning notes - array | go language from scratch
- Principle of synchronized implementation
- MySQL - Chapter 1 (data type 2)
- Give the method of instantiating the object to the new object
- Three ways to create objects in JS
- NPM installation yarn
- Using JS to realize a thousandth bit
- Yyds dry goods inventory ubuntu18 0.4 install MySQL and solve error 1698: access denied for user ''root' '@' 'localhost' '
- Kettle实验 (三)
- Amazon cloud technology entry Resource Center, easy access to the cloud from 0 to 1
猜你喜欢
How to protect open source projects from supply chain attacks - Security Design (1)
Kettle实验
ABAP 7.4 SQL Window Expression
kettle庖丁解牛第14篇之JSON输入
Comparison of overloading, rewriting and hiding
Applet error: cannot read property'currenttarget'of undefined
108. Convert an ordered array into a binary search tree
Kettle experiment (III)
Redis 异常 read error on connection 解决方案
SAP 101K 411K 库存变化
随机推荐
Two methods of building Yum source warehouse locally
Canary publishing using ingress
Redis 过期 key 清理删除策略汇总
Using sqlmap injection to obtain the account and password of the website administrator
Personal homepage software fenrus
Chapter VIII project stakeholder management of information system project manager summary
SAP 03-amdp CDs table function using 'with' clause
Cloud computing competition -- basic part of 2020 competition [task 3]
DMP engine work summary (2021, lightsaber)
《数字电子技术基础》3.1 门电路概述、3.2 半导体二极管门电路
kettle实验
ALV树(LL LR RL RR)插入删除
JS node operation, why learn node operation
A must see wechat applet development guide 1 - basic knowledge
501. Mode in binary search tree
机器学习(六)——贝叶斯分类器
亚马逊云科技入门资源中心,从0到1轻松上云
Buuctf [actf2020 freshman competition] include1
nn. Explanation of module class
Applet error: should have URL attribute when using navigateto, redirectto or switchtab