Farmer John's cows each want to find their soulmate -- another cow with similar characteristics with whom they are maximally compatible. Each cow's personality is described by an integer pipi (1≤pi≤10181≤pi≤1018). Two cows with the same personality are soulmates. A cow can change her personality via a "change operation" by multiplying by 22, dividing by 22 (if pipi is even), or adding 11.
Farmer John initially pairs his cows up in an arbitrary way. He is curious how many change operations would be needed to make each pair of cows into soulmates. For each pairing, decide the minimum number of change operations the first cow in the pair must make to become soulmates with the second cow.
The first line contains NN (1≤N≤101≤N≤10), the number of pairs of cows. Each of the remaining NN lines describes a pair of cows in terms of two integers giving their personalities. The first number indicates the personality of the cow that must be changed to match the second.
Please write NN lines of output. For each pair, print the minimum number of operations required for the first cow to make her personality match that of the second.
6 31 13 12 8 25 6 10 24 1 1 997 120
8 3 8 3 0 20
For the first test case, an optimal sequence of changes is 31⟹32⟹16⟹8⟹9⟹10⟹11⟹12⟹1331⟹32⟹16⟹8⟹9⟹10⟹11⟹12⟹13.
For the second test case, an optimal sequence of changes is 12⟹6⟹7⟹812⟹6⟹7⟹8.
Problem credits: Quanquan Liu
[/hide]
(Analysis by Richard Qi)
Denote AA as the number we start with and BB as the number we want to turn AA into.
Observation 1: The answer is always O(logmax(A,B))O(logmax(A,B)).
To see why this is the case, we first prove that we can turn AA into 11 using O(logA)O(logA) operations.
Observe the binary representation (base 2) of AA. If AA is a power of two, then clearly we can just do dividing operations and reduce it to 11 quickly. Otherwise, if the last bit is 00, do a dividing operation; if the last bit is a 11, do a plus followed by a dividing operation. This reduces the number of bits in the binary representation of AA by 11. Thus, either AA is a power of two and we do O(logA)O(logA) dividing operations, or we do a maximum of 22 operations to reduce the number of bits in the binary representation of AA by 11. Since there are only O(logA)O(logA) total bits in AA, this takes O(logA)O(logA) operations in total.
For the case of turning 11 into BB, note that if we take the inverse of all operations, this is equivalent to turning BB into 11 using "subtract 1" and dividing operations. By a similar argument as above, if the last bit of BB is a 00, we can divide by 22, otherwise we can subtract one and then divide by 22. This takes O(logB)O(logB) operations to turn BB into 11, so it takes O(logB)O(logB) operations to turn 11 into BB using addition and multiplication operations.
Thus, it takes O(logA+logB)O(logA+logB) operations to turn AA into 11, and then turn 11 into BB, so the answer is always O(logmax(A,B))O(logmax(A,B)).
Observation 2: There is never a division that takes place after a multiplication operation. Intuitively, any 11s that we add in between a multiplication and division operation would be more efficiently moved after the division operation.
We will prove this via proof by contradiction: suppose there is a division operation that takes place after a multiplication operation. Then, in the middle of the sequence of operations, we have ⋯∗,+k,/⋯⋯∗,+k,/⋯, where ∗∗ denotes a multiplication operation, +k+k denotes k≥0k≥0 addition operations, // denotes a division operation, and the ⋯⋯ indicate operations occurring before and after this subsequence of operations.
Now, because a division operation was used, and the number after the ∗∗ operation was even, the number of ++ operations in between is even. Suppose we replaced this subsequence of operations with ⋯+k/2⋯⋯+k/2⋯. Let the number starting the subsequence be xx; in the first version of the sequence of operations, we have x⟹2x⟹2x+k⟹x+k/2x⟹2x⟹2x+k⟹x+k/2, while in the second version of the sequence of operations, we have x⟹x+k/2x⟹x+k/2.
Since +k/2+k/2 is always less operations than ∗,+k,/∗,+k,/ and both subsequences correspond to the same function, the second subsequence never appears in an optimal solution.
Thus, we have proven that there is never a division that takes place after a multiplication operation, so the multiplication/division operations are of the form /,/,/,⋯∗,∗,∗/,/,/,⋯∗,∗,∗, with some number of addition operations added in between every two division/multiplication operations. We can divide the operations into 33 phases: the subsequence consisting of the last division operation and everything before it, the subsequence consisting of the addition operations after the last division and before the first multiplication, and the subsequence consisting of the first multiplication operation and everything after it. We label these subsequences S1,S2,S3S1,S2,S3.
These observations are good enough for partial credit. Suppose that we have found all numbers xx such that we need exactly kk operations to turn AA into xx, and suppose we have already done this for all k′≤kk′≤k. Then, we can find all numbers yy such that we need exactly k+1k+1 operations to reach yy from AA, as each of these yy must be a previous xx value after a division/multiplication/addition operation is applied. Additionally, each of these yy should be numbers that could not have been reached using kk or less operations.
It turns out that for the subtask, if we only consider small values of xx and yy in the above algorithm, we arrive at the correct answer. In other words, when turning AA into BB, the intermediate numbers do not get larger than 105+O(logA+logB)105+O(logA+logB). The first and second observations mentioned above imply this, because during S1S1, the only way we can increase is through ++ operations (but there are only a small number of total operations), and during S2S2 and S3S3, the number only increases, until it arrives at B≤105B≤105. Additionally, from the first observation, we only need to consider values of k≤O(logmax(A,B))k≤O(logmax(A,B)), which allows us to calculate the values of yy from the values of xx in linear time for each value of kk.
Depending on implementation, this approach can be either O(max(A,B)logmax(A,B))O(max(A,B)logmax(A,B)) or O(max(A,B))O(max(A,B)).
Observation 3: Just before a division operation, and just after a multiplication operation, there is at most one addition operation. "Just before" a division operation means before the division operation but after the previous division operation (if such a division operation exists); "just after" is defined similarly for multiplication.
We prove this for division (the proof for multiplication is similar). Suppose that we have a subsequence ⋯+2,/,⋯⋯+2,/,⋯. Then, we can replace this with ⋯/,+,⋯⋯/,+,⋯, as both subsequences represent the function x⟹x/2+1x⟹x/2+1.
(Although not needed for this problem, this observation actually implies that when turning AA into BB, the intermediate numbers do not get larger than 105105, which means in the brute force solution mentioned above, we could have only considered numbers ≤105≤105. The reason for this is that during S1S1, the sequence consists of // or +/+/ subsequences concatenated together. In // and +/+/, the original number cannot increase. Thus, the only way that an intermediate number is larger than AA is immediately after the first ++ operation, which only happens if AA is odd. So, all intermediate numbers are ≤105≤105.)
Observation 4: If we fix the total number of divisions as DD, then the sequence S1S1 is fixed, and if we fix the total number of multiplications as MM, then the sequence S3S3 is fixed.
We prove the first part of the statement (the second part is very similar). Suppose we make DD divisions in S1S1. Consider the first division; if AA is odd, then we must have added one before dividing (we could not have added more by the result of our third observation). If AA is even, then we could not have added anything before dividing, as we cannot add more than 11 by the third observation, and if we add exactly one, we cannot divide by 22. Now, let the value of the number after the first division be A2A2. We now know that we need to transform A2A2 into BB using exactly D−1D−1 divisions, so we can repeat the argument we just made. Repeating this down to D=0D=0, we arrive at a forced sequence of operations in S1S1 given the value of DD.
Similarly, given MM, we can arrive at a forced sequence of operations in S3S3.
Since the total number of operations is O(logmax(A,B))O(logmax(A,B)), we can iterate over all pairs of values for DD and MM and generate the forced sequences S1S1 and S3S3, which then force the number of addition operations that need to be in S2S2. Thus, given DD and MM, we can generate the entire sequence, count the number of operations used, and record the minimum such number of operations.
Depending on implementation, this is O(log3max(A,B))O(log3max(A,B)) or O(log2max(A,B))O(log2max(A,B)), both of which fit comfortably in the time limit.
In the below implementation, the outer loop "removed" represents the number of multiplications MM in S3S3, which fixes the number B′B′ that should be reached before applying the sequence of operations in S3S3. The inner while loop represents increasing the value of DD until the value of AA after applying the operations in S1S1 becomes less than or equal to B′B′.
Instead of directly simulating the process of generating the operations in S3S3, the number of such operations can immediately be determined by looking at the binary representation of BB and counting the number of bits in the last MM bits of the binary representation BB. In particular, the number of additions in S3S3 is equal to the number of 11 bits in the number B&(2M−1)B&(2M−1), where && denotes bitwise AND.
Danny's Implementation:
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class SearchingForSoulmates { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringBuilder out = new StringBuilder(); for (int t = Integer.parseInt(in.readLine()); t > 0; t--) { StringTokenizer tokenizer = new StringTokenizer(in.readLine()); long cow1 = Long.parseLong(tokenizer.nextToken()); long cow2 = Long.parseLong(tokenizer.nextToken()); long answer = Long.MAX_VALUE; for (int removed = 0; cow2 >> removed > 0; removed++) { long here = 0; long prefix = cow2 >> removed; long cow = cow1; while (cow > prefix) { if (cow % 2L == 1L) { cow++; here++; } cow /= 2L; here++; } here += prefix - cow; here += removed; here += Long.bitCount(cow2 & ((1L << removed) - 1L)); answer = Math.min(answer, here); } out.append(answer).append('\n'); } System.out.print(out); } }
Bonus: Solve this problem in O(logmax(A,B))O(logmax(A,B)) time.
[/hide]
翰林课程体验,退费流程快速投诉邮箱: yuxi@linstitute.net 沪ICP备2023009024号-1