C++ 方程'a+的积分解;bx=c+;dy`
在方程a+bx=c+dy中,所有变量都是整数<已知代码>a、C++ 方程'a+的积分解;bx=c+;dy`,c++,math,equation,equation-solving,integer-arithmetic,C++,Math,Equation,Equation Solving,Integer Arithmetic,在方程a+bx=c+dy中,所有变量都是整数a、b、c和d。如何找到x和y的积分解?如果我的想法正确,将有无限多个解,由b和d的最小公倍数分隔,但我只需要一个解,其余的我可以计算。下面是一个例子: a = 2 b = 3 c = 4 d = 5 a + bx: (2, 5, 8, 11, 14) c + dy: (4, 9, 14, 19, 24) a + bx intersects c + dy at 14, so: x = 4 y = 2 现在,我正在循环遍历x的整数值,直到找到y的整
b
、c
和d
。如何找到x
和y
的积分解?如果我的想法正确,将有无限多个解,由b
和d
的最小公倍数分隔,但我只需要一个解,其余的我可以计算。下面是一个例子:
a = 2
b = 3
c = 4
d = 5
a + bx: (2, 5, 8, 11, 14)
c + dy: (4, 9, 14, 19, 24)
a + bx intersects c + dy at 14, so:
x = 4
y = 2
现在,我正在循环遍历x
的整数值,直到找到y
的整数值(伪代码):
函数积分解(inta,intb,intc,intd){
//a+bx==c+dy
//(a+bx-c)/d==y
//有些参数可能没有积分解,
//例如,如果b==d和(a-c)%b!=0
//如果在x==d之前没有解,那么就没有解。
int x=0;
while(xc
是a
和b
的最大公约数,这意味着a=z'c
和b=z'c
,则其形式如下
随着a=z'
和b=z'
的变化,方程有无穷多的解。因此,如果c
是a
和b
中的最大公约数(GCD),您可以检查而不是尝试搜索方法(在您的例子中,这转换为bx-dy=c-a
)
如果a
和b
确实是c
的倍数,那么x
和y
就可以计算出满足贝佐特恒等式的整数x
和y
(其中一个通常为负数)
你的答案是:
a=k*x
,
b=k*y
,
c-a=k*gcd(a,b)
用于任何整数k
(作为旁注:这也适用于任何其他情况,即多项式环&每个欧几里德域都是)。您可以使用迭代法找到以下解决方案:
迭代法 通过对类似项进行扩展和分组的常规代数(参见前面提到的最后一节),得到迭代法的以下算法:
- 一,。应用欧几里德算法,让qn(n从1开始)是除法中商的有限列表
- 二,。分别将x0、x1初始化为1、0和y0、y1初始化为0,1。
- 2.1对于每个i,只要定义了qi < Li > 2.2计算席+ 1=席−1.− 祁西
- 2.3计算yi+1=yi−1.− 七一
- 2.4在i增加1后重复上述步骤
function integral_solution(int a, int b, int c, int d) {
// a + bx == c + dy
// (a + bx - c) / d == y
// Some parameters may have no integral solution,
// for example if b == d and (a - c) % b != 0
// If we don't have a solution before x == d, there is none.
int x = 0;
while (x < d)
{
if ((a + bx - c) % d == 0)
{
return [x, (a + bx - c) / d];
}
x++;
}
return false;
}
function extended_gcd(a, b)
x := 0 lastx := 1
y := 1 lasty := 0
while b ≠ 0
quotient := a div b
(a, b) := (b, a mod b)
(x, lastx) := (lastx - quotient*x, x)
(y, lasty) := (lasty - quotient*y, y)
return (lastx, lasty)
因此,我编写了一个示例算法,该算法使用欧几里德算法迭代法计算非负a
和b
(对于负-需要额外的步骤),它返回GCD并将x
和y
的解决方案存储在通过引用传递给它的变量中:
int gcd_iterative(int a, int b, int& x, int& y) {
int c;
std::vector<int> r, q, x_coeff, y_coeff;
x_coeff.push_back(1); y_coeff.push_back(0);
x_coeff.push_back(0); y_coeff.push_back(1);
if ( b == 0 ) return a;
while ( b != 0 ) {
c = b;
q.push_back(a/b);
r.push_back(b = a % b);
a = c;
x_coeff.push_back( *(x_coeff.end()-2) -(q.back())*x_coeff.back());
y_coeff.push_back( *(y_coeff.end()-2) -(q.back())*y_coeff.back());
}
if(r.size()==1) {
x = x_coeff.back();
y = y_coeff.back();
} else {
x = *(x_coeff.end()-2);
y = *(y_coeff.end()-2);
}
std::vector<int>::iterator it;
std::cout << "r: ";
for(it = r.begin(); it != r.end(); it++) { std::cout << *it << "," ; }
std::cout << "\nq: ";
for(it = q.begin(); it != q.end(); it++) { std::cout << *it << "," ; }
std::cout << "\nx: ";
for(it = x_coeff.begin(); it != x_coeff.end(); it++){ std::cout << *it<<",";}
std::cout << "\ny: ";
for(it = y_coeff.begin(); it != y_coeff.end(); it++){ std::cout << *it<<",";}
return a;
}
r:5,3,2,1,0
问:5,4,1,1,2
x:1,0,1,-4,5,-9,23
y:0,1,-5,21,-26,47,-120
与本例给定表格一致的内容:
谷歌“丢番图方程”。这是数学的一个完整分支。在这里复制它没有真正的意义。快乐阅读!你有上面x或y的范围吗?域是否有任何限制?您希望实现什么?找到解决办法了吗?还是x/y的给定范围?@Bathsheba谢谢你的指点。我来看看。@computer不,任何x和y都可以返回,我会用模做一些事情来得到我想要的值。哇,伙计,你在这方面比你自己做得好。谢谢,我很感激!
int main(int argc, char** argv) {
// 120x + 23y = gcd(120,23)
int x_solution, y_solution;
int greatestCommonDivisor = gcd_iterative(120, 23, x_solution, y_solution);
return 0;
}