GCD欧几里得的拓展算法
欧几里得算法的拓展主要是用于求解 :
已知整数 a, b,然后我们进行 ax + by == gcd(a , b) 的问题求解
那么如何进行求解呢?和欧几里得算法一样, 我们需要进行递归的方式进行问题的求解, 而且涉及到 a % b 与 a / b 和 a 的关系
我们假设已经是求出了
b x' + ( a % b ) y' == gcd(a, b);
利用关系, 我们就可以进一步回溯
a y' + b (x' - a / b * y') == gcd(a, b);
但是注意, 这里面的 x, y 对应的 x' y' 似乎是颠倒了, 但是没大问题, 我们只需要在调用函数的时候进行 x, y 参数位置的颠倒就可以
附加挑战书上面双六的代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <list>
#include <map>
#include <stack>
#include <set>
#include <cstdlib>
using namespace std; //这个是x , y == gcd(x, y) 的小算法哦!!!
int extgcd(int a, int b, int &x, int &y){
int d = a;
if(b != ){
d = extgcd(b, a % b, y, x);
y -= a / b * x;
return d;
}
else{
x = , y = ;
return d;
}
}
// gcd 求解最大公因数
int gcd(int a, int b){
return b == ? a : gcd(b, a % b);
} /*这个是挑战书上面双六游戏的代码*/
int main()
{
int a, b; cin>>a>>b;
int x, y;
printf("GCD : %d\n", gcd(a, b));
printf("EXTGCD : %d\n", extgcd(a, b, x, y));
if(extgcd(a, b, x, y) == ){
printf("FIRST IF : \n");
int res = (a > ? a : -a) + (b > ? b : -b);
printf("%d\n", res);
}
else{
printf("SECOND ELSE : \n");
printf("-1\n");
}
return ;
}
注意事项 :
函数调用的时候, 他有着位置的交换(x, y)
然后 y 的数值有着更新
甚至他后两个函数的参数是 引用, 直接对输入变量的元存储进行了修改, 也是避免了返回数值两个 x, 和 y 的麻烦;
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <cctype>#include <algorithm>#include <string>#include <vector>#include <queue>#include <list>#include <map>#include <stack>#include <set>#include <cstdlib>using namespace std;
//这个是x , y == gcd(x, y) 的小算法哦!!!int extgcd(int a, int b, int &x, int &y){ int d = a; if(b != 0){ d = extgcd(b, a % b, y, x); y -= a / b * x; return d; } else{ x = 1, y = 0; return d; }}// gcd 求解最大公因数int gcd(int a, int b){ return b == 0 ? a : gcd(b, a % b);}
/*这个是挑战书上面双六游戏的代码*/int main(){ int a, b; cin>>a>>b; int x, y; printf("GCD : %d\n", gcd(a, b)); printf("EXTGCD : %d\n", extgcd(a, b, x, y)); if(extgcd(a, b, x, y) == 1){ printf("FIRST IF : \n"); int res = (a > 0 ? a : -a) + (b > 0 ? b : -b); printf("%d\n", res); } else{ printf("SECOND ELSE : \n"); printf("-1\n"); } return 0;}
GCD欧几里得的拓展算法的更多相关文章
- ACM数论-欧几里得与拓展欧几里得
ACM数论——欧几里得与拓展欧几里得 欧几里得算法: 欧几里德算法又称辗转相除法,用于计算两个整数a,b的最大公约数. 基本算法:设a=qb+r,其中a,b,q,r都是整数,则gcd(a,b)=gcd ...
- gcd模板(欧几里得与扩展欧几里得、拓展欧几里得求逆元)
gcd(欧几里得算法辗转相除法): gcd ( a , b )= d : 即 d = gcd ( a , b ) = gcd ( b , a mod b ):以此式进行递归即可. 之前一直愚蠢地以为辗 ...
- ACM数论-欧几里得与拓展欧几里得算法
欧几里德算法又称辗转相除法,用于计算两个整数a,b的最大公约数. 基本算法:设a=qb+r,其中a,b,q,r都是整数,则gcd(a,b)=gcd(b,r),即gcd(a,b)=gcd(b,a%b). ...
- <数论相关>欧几里得与拓展欧几里得证明及应用
欧几里得算法 欧几里得算法的复杂度为O(log(n)),是一个非常高效的求最大公约数算法. 在这里不证明欧几里得算法的复杂度,有兴趣的可以访问以下链接:http://blog.sina.com.cn/ ...
- uva 10951 - Polynomial GCD(欧几里得)
题目链接:uva 10951 - Polynomial GCD 题目大意:给出n和两个多项式,求两个多项式在全部操作均模n的情况下最大公约数是多少. 解题思路:欧几里得算法,就是为多项式这个数据类型重 ...
- 欧几里得 & 拓展欧几里得算法 解说 (Euclid & Extend- Euclid Algorithm)
欧几里得& 拓展欧几里得(Euclid & Extend-Euclid) 欧几里得算法(Euclid) 背景: 欧几里德算法又称辗转相除法.用于计算两个正整数a.b的最大公约数. -- ...
- NOIP2012拓展欧几里得
拉板题,,,不说话 我之前是不是说过数据结构很烦,,,我想收回,,,今天开始的数论还要恶心,一早上听得头都晕了 先来一发欧几里得拓展裸 #include <cstdio> void gcd ...
- poj 1061 青蛙的约会 (扩展欧几里得模板)
青蛙的约会 Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit Status ...
- 算法马拉松35 E 数论只会Gcd - 类欧几里得 - Stern-Brocot Tree - 莫比乌斯反演
题目传送门 传送门 这个官方题解除了讲了个结论,感觉啥都没说,不知道是因为我太菜了,还是因为它真的啥都没说. 如果 $x \geqslant y$,显然 gcd(x, y) 只会被调用一次. 否则考虑 ...
随机推荐
- 浅谈神经网络中的bias
1.什么是bias? 偏置单元(bias unit),在有些资料里也称为偏置项(bias term)或者截距项(intercept term),它其实就是函数的截距,与线性方程 y=wx+b 中的 b ...
- vue中.sync修饰符,实现子组件实时更新父组件的值
vue 修饰符sync的功能是:当一个子组件改变了一个 prop 的值时,这个变化也会同步到父组件中所绑定. 不过它有一个前身,先来看看.sync出现之前是如何实现的 父组件中(传递给子组件一个值:p ...
- Kinect v1 (Microsoft Kinect for Windows v1 )彩色和深度图像对的采集步骤
Kinect v1 (Microsoft Kinect for Windows v1 )彩色和深度图像对的采集步骤 一.在ubuntu下尝试 1. 在虚拟机VWware Workstation 12. ...
- CentOS 安装 docker-compose 加速
sudo curl -L "https://get.daocloud.io/docker/compose/releases/download/1.24.1/docker-compose-$( ...
- Java并发指南3:并发三大问题与volatile关键字,CAS操作
本文转载自互联网,侵删 序言 先来看如下这个简单的Java类,该类中并没有使用任何的同步. 01 final class SetCheck { 02 private int a = 0; 03 ...
- REGIONAL SCRUM GATHERING(RSG)2019 CHINA.
欢迎参加 REGIONAL SCRUM GATHERING(RSG)2019 CHINA. 今年RSG将于2019年8月23号~24号,在北京新世界酒店举办.在为期2天的敏捷大会中,将有接近40位国内 ...
- SpringBoot/SpringMVC 下载本地文件
页面代码: <!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Ty ...
- The first one spawns an additional process forwarding requests to a series of workers (think about it as a form of shield, at the same level of apache or nginx), while the second one sets workers to n
Things to know (best practices and “issues”) READ IT !!! — uWSGI 2.0 documentationhttps://uwsgi-docs ...
- 七天学会ASP.NET MVC
地址一: http://www.cnblogs.com/powertoolsteam/p/MVC_one.html http://www.cnblogs.com/powertoolsteam/p/MV ...
- jquery mCustomScrollbar使用
$(".content .desc").mCustomScrollbar({ axis: "y", theme: 'dark', mouseWheel: { e ...