- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
在等式 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
的整数值(伪代码):
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;
}
我觉得有更好的方法来做到这一点。有什么办法不用循环就可以找到 x 和 y 吗?我正在使用 C++,如果这很重要的话。
最佳答案
Linear Diophantine方程采用 ax + by = c
的形式。如果 c
是 a
和 b
的最大公约数,这意味着 a=z'c
和 b=z''c
那么这是 Bézout's identity形式的
a=z'
和 b=z''
方程有无限多解。因此,除了尝试搜索方法之外,您还可以检查c
是否是a
和b 的最大公约数 (GCD)
(在您的情况下,这转化为 bx - dy = c - a
)
如果 a
和 b
确实是 c
的倍数,那么 x
和 y
可以使用 extended Euclidean algorithm 计算它找到满足 Bézout 身份的整数 x
和 y
(其中一个通常为负数)
你的答案是:
a = k*x
,b = k*y
,c - a = k * gcd(a,b)
对于任何整数 k。
(作为旁注:这也适用于任何其他 Euclidean domain ,即多项式环和每个欧几里德域都是 unique factorization domain )。您可以使用迭代法找到这些解决方案:
通过对同类项进行展开分组的常规代数运算(引用前文wikipedia article的最后一节),迭代法得到如下算法:
伪代码:
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
(对于负值 - these 需要额外的步骤),它返回 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;
}
通过传递给它一个 example从维基百科 a = 120
和 b = 23
我们得到:
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;
}
r: 5,3,2,1,0,
q: 5,4,1,1,2,
x: 1,0,1,-4,5,-9,23,
y: 0,1,-5,21,-26,47,-120,
根据这个例子给定的表格是什么:
关于c++ - 方程 `a + bx = c + dy` 的积分解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19338633/
请提出一个数据结构来表示内存中的记录列表。每条记录由以下部分组成: 用户名 积分 排名(基于积分)- 可选字段- 可以存储在记录中或可以动态计算 数据结构应该支持高效实现以下操作: Insert(re
我正在使用 integrate 将一些集成到循环中我想出了一个我无法理解的错误,也无法摆脱。这是我可以提取的 MWE: u_min = 0.06911363 u_max = 1.011011 m =
掌上生活17要吃节签到抽腾讯视频爱奇艺会员月卡 5元饭票 积分 打开掌上生活APP,首页全部专区进入找到活动日历往下拉可以看到17要吃节进入活动页面 可以集3个赞兑换星巴克喝,也可以签到抽爱
我遇到了一个有趣但相当烦人的问题。 我正在尝试集成一个从数据集计算出来的函数。 数据可以在这里找到:Link to sample.txt . 我首先将一条线拟合到我的数据中。这可以通过 approxf
当我使用 Three.js 创建一个点时,它看起来像一个正方形。我怎样才能使它看起来圆?我在文档中看到了一些混合因素,但我不太明白如何在我的观点中使用它们,我什至不知道这是否是正确的方法。 最佳答案
我尝试了此处找到的示例代码: https://developers.facebook.com/docs/creditsapi/即使我添加了我的公司地址和付款方式,我仍然会收到此错误: API Erro
我想使用 scipy.integrate.ode 求解器。我只能将可调用函数 f 定义为离散点数组(因为它取决于先前迭代的积分结果)。但是从文档来看,集成商似乎希望可调用函数是一个连续函数。我想需要进
我无法理解 sympy.integrate() 函数的行为。最简单的例子,整合和分化: t = sy.Symbol('t') t1 = sy.Symbol('t1') f = sy.Function(
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,
我在 zeroSSL 面板中有一个过期的 SSL 证书,但我无法更新它,因为我生成了 3/3 证书。 1 仍处于事件状态,但其他两个已过期(已为这些相同的域提前生成)。是否有可能以某种方式删除其中一个
我有一个数据结构,例如表达式树或图形。我想添加一些“测量”功能,例如depth和 size . 如何最好地键入这些函数? 我认为以下三个变体的用处大致相同: depth :: Expr -> Int
让 Mathematica 7 或 8 进行积分的最佳方法是什么 NIntegrate[Exp[-x]/Sin[Pi x], {x, 0, 50}] 每个整数都有极点 - 我们需要柯西原理值。这个想法
只是想知道是否有人知道如何查询 Facebook Credits (FBC) API 以获取用户拥有的信用数?我的应用程序有此要求,并且 FBC API 中没有对此进行解释或提及。 谢谢 最佳答案 也
好的,所以这让我难住了超过 3 天,在离解决方案还差一步之后,我要在这里试试运气。 过去,我为一个特定的排序数据集编写了一些代码,它是这样的: n maxobs){FG = 1} else {
在激活通过 MSDN 订阅获得的 Azure 积分时,我使用了工作帐户。 事实证明,由于我没有 Active Directory 管理员权限,因此无法注册应用程序等。这使得它毫无用处。我也不太可能获得
如何使用 Romberg 积分近似计算以下积分, min:1, max:1.6, integral (2x)/((x^2)-4) 还计算 Romberg 表,直到 |R_n-1,n-1 - R_n,n
我正在尝试计算积分 sin(x)/x , x = [0,inf] 我做了以下事情: import math from scipy.integrate import quad t = float("in
所以我的代码有效,只是出于某种原因,我的代码总是运行两个 if 语句(两个 y 方程,无论我为第一个 fprintf 问题输入哪个数字)。此外,t,y 列总是比 t,y2 列长得多(编辑,即如果我输入
我有一个简单的问题。我正在尝试使用 Matlab R2012a 评估 0 阶贝塞尔函数的不正确积分: v = integral(@(x)(besselj(0, x), 0, Inf) 这给了我 v =
我正在与 iPhone Native Game App 一起开发 Facebook Canvas Game 项目,该项目使用 Facebook 积分作为唯一的虚拟货币。 据我们所知,Apple 应用内
我是一名优秀的程序员,十分优秀!