我有一个数字数组,我想要它们的相应系数,以便它们的线性组合能给我最大的公约数。
在此link中,我找到了如何对2个数字执行此操作。但是我想知道如何有效地处理一组数字。
ll gcdExtended(ll a, ll b, ll *x, ll *y)
{
// Base Case
if (a == 0)
{
*x = 0;
*y = 1;
return b;
}
ll x1, y1; // To store results of recursive call
ll gcd = gcdExtended(b%a, a, &x1, &y1);
// Update x and y using results of recursive
// call
*x = y1 - (b/a) * x1;
*y = x1;
return gcd;
}
ll calc(ll gcd, int i)
{
ll x,y;
ll gcd_new = gcdExtended(gcd,conc[i],x,y);
if (i == conc.size()-1)
{
return x;
}
ll temp = calc(gcd_new, i+1);
x *= temp;
y* = temp;
return x;
}
这是我尝试过的。 ll
代表long long int
,conc
是一个全局向量,其中包含需要查找其GCD的数字。