扩展欧几里得学习笔记
簡述Exgcd
要求關(guān)于\(x,y\)的方程\(ax+by=c\)的一組解。
假裝顯然我們要先求出\(ax+by=\mathrm{gcd}(a,b)\)的一組解,然后就可以找出所有的解\(x,y\)。
設(shè)\(bx'+(a\%b)y'=\mathrm{gcd}(b,a\%b)=\mathrm{gcd}(a,b)\)
\(\because a \% b = a - \left\lfloor\frac{a}\right\rfloor \times b\)
\(\therefore bx' + (a \% b) y' = bx' + (a - \left\lfloor\frac{a}\right\rfloor \times b) y’\)
\(\quad = bx' + ay' - \left\lfloor\frac{a}\right\rfloor \times by’ = ay' + b (x' - \left\lfloor\frac{a}\right\rfloor \times y')\)
故有:
\[ \mathrm{Exgcd}(a,b)=\left\{ \begin{array}{} x = 1,\; y = 1 & \mathrm{if}\;b=0\\ \mathrm{Exgcd}(b,a \% b),\; y = x - \left\lfloor\frac{a}\right\rfloor\times y & \mathrm{if} \; b \not = 0 \end{array} \right. \]
代碼
void exgcd(int a, int b, int &d, int &x, int &y) {if(b == 0) {d = a, x = 1, y = 0;} else {exgcd(b, a % b, d, x, y);int t = x;x = y, y = t - (a / b) * y;} }轉(zhuǎn)載于:https://www.cnblogs.com/zhylj/p/9571264.html
總結(jié)
以上是生活随笔為你收集整理的扩展欧几里得学习笔记的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Struts2.0第三章(文件上传、aj
- 下一篇: 【linux 06】 linux中的用户