同时使A和B平等的操作数量最少
给定两个非负整数A和B,请找到最小数量的操作,以同时使它们同样。在一个操作中,您可以:
- 将A更改为2*A
- 或更改B为2*B
- 或将A和B更改为A-1,B-1
:A = 7,B = 25
操作序列将为:
- 6 24
- 12 24 24
- 24 24
我们不能在少于3个操作中使它们相等
我们不能在一周前的测试中提出这个编码问题, 。无法想到一个解决方案,它被卡在我的头上。输入A和B略高于10^12,因此很明显,我不能使用循环否则会超过时间限制。
Given two non-negative integers A and B, find the minimum number of operations to make them equal simultaneously. In one operation, you can:
- either change A to 2*A
- or change B to 2*B
- or change both A and B to A-1, B-1
For example: A = 7, B = 25
Sequence of operations would be:
- 6 24
- 12 24
- 24 24
We cannot make them equal in less than 3 operations
I was asked this coding question in a test a week ago. Cannot think of a solution, it is stuck in my head.The input A and B were somewhat over 10^12 so it is clear that I cannot use a loop else it will exceed time limit.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
缓慢但有效的解决方案:
在步骤4中,最大减少。在步骤5中,绝对差减小。因此,最终该算法终止。
A slow but working solution:
In step 4, the maximum decreases. In step 5, the absolute difference decreases. Thus eventually the algorithm terminates.
这应该给出最佳解决方案。我们必须比较几种不同的方法并采用最佳解决方案。
一种工作解决方案是将较小数量的数量加倍,因为它停留在较大数字以下(可能为零倍)。然后计算(可能多次)的双重差异较小的数字和较大的数字之间的差异。并减少数量多次。然后再将较小的时间加倍。 [如果数字从开始时相等,则解决方案是微不足道的。]这给出了步骤的上限。
现在尝试以下优化:
2a)选择一个数字n在0到迄今为止最佳解决方案的步骤数之间。
2b)选择一个数字作为A,一个数字作为B(两个可能性)。
2C)现在计算以下过程的应用步骤。
双n次。
计算2(= m)的最小功率,使用
b * 2^m> = a
。 m至少应为1。从步骤6中获取最重要的未处理数字,并执行许多减少步骤。
双b。
重复7。使用下一个数字;如果不再剩下数字,则数字相等。
如果步骤数小于到目前为止的最佳解决方案,请选择作为建议的解决方案。
返回步骤2进行其他选择。
从2中进行所有选择后,我们应该具有最低数量的最佳解决方案。
以下示例来自答案的较早版本,其中A始终是较大的数字和n = 0,因此我们仅测试一个选择。
示例17和65
示例18和67
示例10和137
This should give the optimal solution. We have to compare a few different ways and take the best solution.
One working solution is to double the smaller number as many times as it stays below the larger number (can be zero times). Then calculate the difference between the double of the (possibly multiple times) doubled smaller number and the larger number. And decrease the numbers as many times. Then double the smaller number one more time. [If the numbers are equal from the beginning, the solution is trivial instead.] This gives an upper bound of the steps.
Now try out the following optimizations:
2a) Choose a number n between 0 and up to the number of steps of the best solution so far.
2b) Choose one number as A and one number as B (two possibilities).
2c) Now count the applied steps of the following procedure.
Double A n times.
Calculate the smallest power of 2 (=m), with which
B * 2^m >= A
. m should be at least 1.Calculate the difference of A with the product from step 4 in a mixed base (correct term?) system with each digit having a positional value of
2^(n+1)-1
, which is from the least significant right digit to the left: 1, 3, 7, 15, 31, 63, ... From all possible representations the number must have the smallest crosssum, e.g. 100 for 7 is correct, 021 not. Sidenote: For the least checksum there will mostly be digits 0 and 1 and at most one digit 2, no other digits. There will never be a digit 1 right of a 2.)Represent the number as m digits by filling the left positions with zero. If the number does not fit, go back to step 2 for another selection.
Take the most significant not processed digit from step 6 and do as many decreasing steps.
Double B.
Repeat from 7. with the next digit; if there are no more digits left, the numbers are equal.
If the number of steps is less than the best solution so far, choose this as the proposed solution.
Go back to step 2 for another selection.
After doing all selections from 2 we should have the optimal solution with the minimum number of steps.
The following examples are from an earlier version of the answer, where A is always the larger number and n=0, so we test only one selection.
Example 17 and 65
Example 18 and 67
Example 10 and 137
这是一个广度优先的搜索,确实返回正确的答案,但可能不是找到它的最佳方法。也许它可以帮助他人检测模式。
Javasscript代码:
Here's a breadth-first search that does return the correct answer but may not be an optimal method of finding it. Maybe it can help others detect a pattern.
JavasScript code: