【发布时间】:2012-05-13 00:59:25
【问题描述】:
假设我有一个简单的公式:
7x + 4y = n
其中 n 由我们选择,x、y 和 n 都是正整数。这是给我们的唯一等式。在可能的解决方案中,我们需要 x 最小的解决方案 (x,y)。例如
7x + 4y = 14, then (2, 0) is the solution
7x + 4y = 15, then (1, 2) is the solution
7x + 4y = 32, then (4, 1) and (0, 8) are the possible solutions,
of which (0, 8) is the correct solution
我想设计一种算法,以尽可能少的运行时间计算它。我想到的当前算法是这样的:
Given an input n
Calculate max(x) = n/7
for i = 0 to max(x)
If the equation 7*i + 4*y = n holds
return value of i and y
else
continue
我认为,在最坏的情况下,该算法的运行时间可达 O(n)。有没有更好的算法来计算解?
【问题讨论】:
-
你说
If the equation 7*i + 4*y = n holds我是从循环中得到的,但是 y 是什么? -
X 和 Y 是否有上限?如果是这样,那么二进制搜索就是您的成功之道。
-
您可能想了解linear integer programming。您的问题绝对是广义问题的一个特定实例,但我很好奇您面临的简化问题是否有有效的解决方案。
-
@Betlista 我有 raised the issue on meta 如果你想进一步评论的话