扩展GCD算法
extended GCD algorithm
我已经为扩展GCD算法编写了以下算法,只是不知道如何返回三重,有人能帮助我吗?
#include<iostream>
#include<math.h>
using namespace std;
int gcd(int a,int b) { return (b==0 ?a:gcd(b,a%b));}
long long gcd(long a,long b) { return (b==0 ?a:gcd(b,a%b));}
template<class Int> Int gcd(Int a,Int b) { return (b==0 ?a:gcd(b,a%b));}
template<class Int>
struct Triple
{
Int d,x,y;
Triple(Int q,Int w,Int e) :d(q),x(w),y(e)) {}
};
//extended GCD
/* computes d=gcd(a,b)
also x and y such that d=a*x+y*b and return tripls (d,x,y)
*/
template<class Int>
Triple <Int> egcd(Int a,Int b) {
if(!b) return Triple<Int>(a,Int(1),Int(0));
Triple<int>q=egcd(b,a%b);
return Triple<Int>(q.d,q.y,q.x-a/b*q.y);
}
int main(){
int a=35;
int b=13;
return 0;
}
如何完成它使用我的三重结构构造器?
(1)更正构造函数,它不编译(去掉一个括号):
Triple(Int q,Int w,Int e) : d(q), x(w), y(e) {}
(2) In main()
call:
Triple <int> t = egcd(a, b);
cout << t.d << ", " << t.x << ", " << t.y << endl;
相关文章:
- 为什么这个运算符<重载函数对 STL 算法不可见?
- 基于ELO的团队匹配算法
- C++选择排序算法中的逻辑错误
- 有没有办法将谓词中的元素偏移量传递给 std 算法?
- C++A*算法并不总是在路径中具有目标节点
- 排序算法c++
- 构建可组合有向图(扫描仪生成器的汤普森构造算法)
- 算法问题:查找从堆栈中弹出的所有序列
- 下面是排序算法O(n)吗
- KMP算法和LPS表构造的运行时间
- 为什么我的排序算法会更改数组值
- 求最大元素位置的分治算法
- 具有非整数边容量的最大流量的Dinic算法
- 到连接组件算法的问题(递归)
- NTRUEncrypt:使用开源标准算法中的描述无法正确找到两个多项式的GCD,无法定义是否存在多边形的逆
- 二进制 GCD - 算法太慢
- GCD 算法不正确
- 使用欧几里得算法找到GCF(GCD)
- 扩展GCD算法
- 使用欧几里得算法识别两个值的最大公约数 (GCD)