欧几里得算法的拓展主要是用于求解   :

    已知整数 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欧几里得的拓展算法的更多相关文章

  1. ACM数论-欧几里得与拓展欧几里得

    ACM数论——欧几里得与拓展欧几里得 欧几里得算法: 欧几里德算法又称辗转相除法,用于计算两个整数a,b的最大公约数. 基本算法:设a=qb+r,其中a,b,q,r都是整数,则gcd(a,b)=gcd ...

  2. gcd模板(欧几里得与扩展欧几里得、拓展欧几里得求逆元)

    gcd(欧几里得算法辗转相除法): gcd ( a , b )= d : 即 d = gcd ( a , b ) = gcd ( b , a mod b ):以此式进行递归即可. 之前一直愚蠢地以为辗 ...

  3. ACM数论-欧几里得与拓展欧几里得算法

    欧几里德算法又称辗转相除法,用于计算两个整数a,b的最大公约数. 基本算法:设a=qb+r,其中a,b,q,r都是整数,则gcd(a,b)=gcd(b,r),即gcd(a,b)=gcd(b,a%b). ...

  4. <数论相关>欧几里得与拓展欧几里得证明及应用

    欧几里得算法 欧几里得算法的复杂度为O(log(n)),是一个非常高效的求最大公约数算法. 在这里不证明欧几里得算法的复杂度,有兴趣的可以访问以下链接:http://blog.sina.com.cn/ ...

  5. uva 10951 - Polynomial GCD(欧几里得)

    题目链接:uva 10951 - Polynomial GCD 题目大意:给出n和两个多项式,求两个多项式在全部操作均模n的情况下最大公约数是多少. 解题思路:欧几里得算法,就是为多项式这个数据类型重 ...

  6. 欧几里得 &amp; 拓展欧几里得算法 解说 (Euclid &amp; Extend- Euclid Algorithm)

    欧几里得& 拓展欧几里得(Euclid & Extend-Euclid) 欧几里得算法(Euclid) 背景: 欧几里德算法又称辗转相除法.用于计算两个正整数a.b的最大公约数. -- ...

  7. NOIP2012拓展欧几里得

    拉板题,,,不说话 我之前是不是说过数据结构很烦,,,我想收回,,,今天开始的数论还要恶心,一早上听得头都晕了 先来一发欧几里得拓展裸 #include <cstdio> void gcd ...

  8. poj 1061 青蛙的约会 (扩展欧几里得模板)

    青蛙的约会 Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status ...

  9. 算法马拉松35 E 数论只会Gcd - 类欧几里得 - Stern-Brocot Tree - 莫比乌斯反演

    题目传送门 传送门 这个官方题解除了讲了个结论,感觉啥都没说,不知道是因为我太菜了,还是因为它真的啥都没说. 如果 $x \geqslant y$,显然 gcd(x, y) 只会被调用一次. 否则考虑 ...

随机推荐

  1. 7月清北学堂培训 Day 4

    今天是丁明朔老师的讲授~ 图论 图是种抽象结构,这种抽象结构可以表示点与点之间的关系. 最短路: Dijkstra(堆优化) SPFA Floyd 最小生成树: Kruscal 连通性: BFS / ...

  2. 异步机制 - ReadFileEx(WriteFileEx)

    1 ReadFileEx定义 BOOL WINAPI ReadFileEx( __in HANDLE hFile, __out LPVOID lpBuffer, __in DWORD nNumberO ...

  3. Web Services之基本认识

    参考:http://www.w3school.com.cn/webservices 1.什么是Web Services Web Services 可使您的应用程序成为 Web 应用程序.Web Ser ...

  4. 跨域请求错误: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource

    今天在学习Angular 的HttpInterceptor 拦截器时,发现添加了新的headers键值之后总是报跨域错误.后台使用的是asp.net core. 检查发现,在添加了新的header之后 ...

  5. C#问答题与附解收集(三)

    post.get的区别 答: GET把参数包含在URL中,POST通过request body传递参数.GET请求在URL中传送的参数是有长度限制的,而POST没有.使用post提交的页面在点击[刷新 ...

  6. 阶段5 3.微服务项目【学成在线】_day04 页面静态化_17-页面静态化-模板管理-GridFS研究-存文件

    将模板信息保存在cms_template里面 存储在fs.chunks这个集合中.这个集合里面存的是分块文件. fs.files存的是文件的基本信息 chunks存的是块信息 创建测试文件 在cms的 ...

  7. nginx使用场景

    1. 对外开放本地封闭Server 本地server无法对外开放,nginx做反向代理,对外开发,使得外部可以访问封闭服务. upstream npm { server ; keepalive ; } ...

  8. React Native清除缓存实现

    清除缓存使用的第三方:react-native-http-cache   Github: https://github.com/reactnativecn/react-native-http-cach ...

  9. 同一个电脑配置两个github账号

    mac中.ssh文件夹在根目录下,所以表示成 ~/.ssh/. 一.同一个电脑配置两个github账号1.分别为两个GitHub账号生成SSH密钥 $ cd ~/.ssh $ ssh-keygen - ...

  10. Saltstack之使用salt安装es6.0以上的head插件

    本实验使用salt安装es6.0以上的head插件 ES6.0以上手动安装head插件参考:https://www.cnblogs.com/minseo/p/9117470.html 文件夹目录为 / ...