就像Bezout的身份一样,但具有自然数和任意常数。可以解决吗?
我是一个业余演奏,播放离散数学。这不是 功课问题,尽管我在家做。
我想用a,b和c求解ax + by = c。 给出以及要计算的x和y。我想找到所有x,y对 这将满足方程式。
这与bezout的整数身份具有类似的结构 在哪里有多个(无限?)溶液对。我想 相似性可能意味着扩展的欧几里得算法 可以在这里提供帮助。以下是EEA的两个实现 似乎有效;它们都从网络上的代码中进行了调整。
这些是否可以适应任务,或者可能有人可以 找到一个更有前途的大道?
typedef long int Int;
#ifdef RECURSIVE_EEA
Int // returns the GCD of a and b and finds x and y
// such that ax + by == GCD(a,b), recursively
eea(Int a, Int b, Int &x, Int &y) {
if (0==a) {
x = 0;
y = 1;
return b;
}
Int x1; x1=0;
Int y1; y1=0;
Int gcd = eea(b%a, a, x1, y1);
x = y1 - b/a*x1;
y = x1;
return gcd;
}
#endif
#ifdef ITERATIVE_EEA
Int // returns the GCD of a and b and finds x and y
// such that ax + by == GCD(a,b), iteratively
eea(Int a, Int b, Int &x, Int &y) {
x = 0;
y = 1;
Int u; u=1;
Int v; v=0; // does this need initialising?
Int q; // quotient
Int r; // remainder
Int m;
Int n;
while (0!=a) {
q = b/a; // quotient
r = b%a; // remainder
m = x - u*q; // ?? what are the invariants?
n = y - v*q; // ?? When does this overflow?
b = a; // A candidate for the gcd - a's last nonzero value.
a = r; // a becomes the remainder - it shrinks each time.
// When a hits zero, the u and v that are written out
// are final values and the gcd is a's previous value.
x = u; // Here we have u and v shuffling values out
y = v; // via x and y. If a has gone to zero, they're final.
u = m; // ... and getting new values
v = n; // from m and n
}
return b;
}
#endif
I'm an amateur playing with discrete math. This isn't a
homework problem though I am doing it at home.
I want to solve ax + by = c for natural numbers, with a, b and c
given and x and y to be computed. I want to find all x, y pairs
that will satisfy the equation.
This has a similar structure to Bezout's identity for integers
where there are multiple (infinite?) solution pairs. I thought
the similarity might mean that the extended Euclidian algorithm
could help here. Below are two implementations of the EEA that
seem to work; they're both adapted from code found on the net.
Could these be adapted to the task, or perhaps can someone
find a more promising avenue?
typedef long int Int;
#ifdef RECURSIVE_EEA
Int // returns the GCD of a and b and finds x and y
// such that ax + by == GCD(a,b), recursively
eea(Int a, Int b, Int &x, Int &y) {
if (0==a) {
x = 0;
y = 1;
return b;
}
Int x1; x1=0;
Int y1; y1=0;
Int gcd = eea(b%a, a, x1, y1);
x = y1 - b/a*x1;
y = x1;
return gcd;
}
#endif
#ifdef ITERATIVE_EEA
Int // returns the GCD of a and b and finds x and y
// such that ax + by == GCD(a,b), iteratively
eea(Int a, Int b, Int &x, Int &y) {
x = 0;
y = 1;
Int u; u=1;
Int v; v=0; // does this need initialising?
Int q; // quotient
Int r; // remainder
Int m;
Int n;
while (0!=a) {
q = b/a; // quotient
r = b%a; // remainder
m = x - u*q; // ?? what are the invariants?
n = y - v*q; // ?? When does this overflow?
b = a; // A candidate for the gcd - a's last nonzero value.
a = r; // a becomes the remainder - it shrinks each time.
// When a hits zero, the u and v that are written out
// are final values and the gcd is a's previous value.
x = u; // Here we have u and v shuffling values out
y = v; // via x and y. If a has gone to zero, they're final.
u = m; // ... and getting new values
v = n; // from m and n
}
return b;
}
#endif
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我们稍微更改方程式:
那么我们可以循环
x
通过其范围内的所有数字(a*x< = c
),并计算如果可行的天然y
存在。 没有无限数量的解决方案限制为min(c/a,c/b)
因此,
y
也只需检查c-ax
是否可以通过b
进行除外:如您现在可以看到的那样:您现在可以看到
x,y
在相同的循环中以相反的方向迭代,并且在循环中不再存在划分或乘法,因此在这里的首先要快得多:我希望,对于真正巨大的
c
和小a,b
数字,这可能比使用GCD的实际除法速度慢。
If we slightly change the equation form:
Then we can loop
x
through all numbers in its range (a*x <= c
) and compute if viable naturaly
exists. So no there is not infinite number of solutions the limit ismin(c/a,c/b)
... Here small C++ example of naive solution:If you want to speed this up then just iterate
y
too and just check ifc-ax
is divisible byb
Something like this:As you can see now both
x,y
are iterated in opposite directions in the same loop and no division or multiplication is present inside loop anymore so its much faster here first few results:I expect that for really huge
c
and smalla,b
numbers thismight be slower than actual division using GCD ...