PROBLEM LINK:Author: Istvan Nagy DIFFICULTY:MediumHard PREREQUISITES:Bitwise operations, sieve, factorization PROBLEM:Given $A, B, C, N$, compute all solutions $(x,y)$ to the following equation: $xy = (x\lor y)(x\land y) + Ax + By + C$ where $\lor$ and $\land$ are bitwise OR and AND, respectively. Compute the sum of all $x$ and sum of all $y$ across all solutions $(x,y)$ with $0 \le x, y \le N$. QUICK EXPLANATION:Let $n = x\land y$, $a = xn$ and $b = yn$. Then there's a onetoone mapping between solutions $(x,y)$ and triples $(a,b,n)$ such that:
The mapping is given by $(x,y) = (a+n,b+n)$. Let $L = (A+B)N+AB+C$. Notice that $(aB)(bA) \le L$, and the smaller of $(aB)$ and $(bA)$ is $\le \sqrt{L}$. To compute all solutions $(a,b,n)$ such that $(aB) \le (bA)$:
Similarly, to compute all solutions $(a,b,n)$ such that $(aB) > (bA)$:
Using this, every solution will be generated exactly once, but you need to check each candidate $(a,b,n)$ if it satisfies all conditions above. EXPLANATION:ReparameterizationThe equation $xy = (x\lor y)(x\land y) + Ax + By + C$ is very weird because of the bitwise operations. You don't normally see those, which makes this problem unusual. Nevertheless, bitwise operations still behave somewhat well algebraically, so let's see what we can do. The first thing we note is that if two numbers $a$ and $b$ don't share any bits, then $(a\lor b) = a+b$ and $(a\land b) = 0$. But what if they don't share any bits? Then the first one is incorrect, because it fails to take into account the bits shared by $a$ and $b$. Specifically, these bits are counted twice in $a+b$ but only once in $(a\lor b)$. But we can easily know which bits two numbers share: it's simply $(a\land b)$! Thus, this gives us the following general identity: This is great, because it eliminates one nasty bitwise operator! Now we have one more left. Our equation is now: (You'll notice that there are too many parentheses in these equations. This is because bitwise operations are that uncommon among math equations.) If we let $n = x\land y$, then the equation becomes: Now we're getting somewhere. Notice that the bitwise operations are disappearing. In fact, let's take this idea further, and reparameterize the solutions so that our variables don't have bits in common. Currently, we have the values $(x,y,n)$, but these numbers may have bits in common. So let's define new variables $a = xn$ and $b = yn$ to denote the bits unique to $x$ and $y$, respectively. This way, for a triple $(a,b,n)$, we have $(a\land b) = (a\land n) = (b\land n) = 0$ and $$(a+n)(b+n) = ((a+n)+(b+n)  n)n + A(a+n) + By + C$$ Let's manipulate this further: $$\begin{align*} (a+n)(b+n) &= ((a+n)+(b+n)  n)n + A(a+n) + B(b+n) + C \\\ (a+n)(b+n) &= (a+b+n)n + A(a+n) + B(b+n) + C \\\ ab+an+bn+n^2 &= an+bn+n^2 + A(a+n) + B(b+n) + C \\\ ab &= A(a+n) + B(b+n) + C \end{align*}$$ Notice the nice cancellation going on! This is certainly easier to solve. At this point though, it isn't clear how to proceed. But if you assume for a while that $n$ is a constant, then we notice that the equation becomes a conic section on the variables $a$ and $b$, and it turns out that such equations are usually reducible to one of the following:
Let's see what we got by assuming $n$ is constant and throwing all "constants" to the right side:
$$\begin{align*}
ab &= A(a+n) + B(b+n) + C\\\
ab  Aa  Bb &= (A+B)n + C
\end{align*}$$
Here, we can try to "complete the factorization": This means we have reduced the problem to factorizing all numbers of the form $(A+B)n + AB + C$, where $n \le N$ :) Enumerating factorsNow we're really getting somewhere. We want to enumerate the triples $(a,b,n)$ satisfying all the following things:
The remaining two conditions can just be checked afterwards, so let's focus on the first, i.e. enumerating factorizations of $(A+B)n + AB + C$. This number is always positive, but the factors on the left side may be negative! Thankfully, we can exclude that possibility because if both factors are negative (and $a, b \ge 0$), then $(aB)(bA) \le AB < (A+B)n + AB + C$. Thus, we can focus on positive divisors. Next, notice that the largest possible righthandside value is $L := (A+B)N + AB + C$. This means that the smaller factor among $(aB)$ and $(bA)$ is at most the square root of this number. But the square root of this number is a smallish number considering the problem constraints, which makes it feasible for enumeration! Specifically, assume first that $(aB) \le (bA)$. This means that $(aB) \le \sqrt{L}$. So let's enumerate all these possible values $d := aB$. The next step is to enumerate all indices $n \le N$ such that $d$ divides $(A+B)n + AB + C$. Which such indices are there? If we let $P = A+B$ and $Q = AB + C$, then the following are equivalent: $$\begin{align*} d &\mid (A+B)n + AB + C \\\ d &\mid Pn + Q \\\ Pn &\equiv Q \pmod{d} \end{align*}$$ Let $g = \gcd(P,d)$. Then if $g$ doesn't divide $Q$, this has no solutions. Otherwise, we can continue by letting $P' = P/g$, $Q' = Q/g$ and $d' = d/g$: This means that it's easy to "loop" across all valid indices $n$! The following is an illustration on how this can be done, using a C++style loop:
Having access to $d$ and $n$, we can now compute a candidate solution $(a,b,n)$ as:
(You should only count this if $(aB) \le (bA)$ is true.) Using this method, we can enumerate all candidate solutions $(a,b,n)$ satisfying $(aB) \le (bA)$. We can enumerate candidates $(a,b,n)$ satisfying the other inequality $(bA) < (aB)$ similarly:
(You should only count this if $(bA) < (aB)$ is true.) Since every solution $(a,b,n)$ determines a unique value $(aB)(bA)$, this means each solution will be generated exactly once! We just need to filter out the candidates with the remaining requirements:
Running timeThe question now is, how fast does this solution run? Notice that for every number $d \le \sqrt{L}$, the number of candidates we're considering is at most:
$$1 + \frac{N}{d/\gcd(A+B,d)}$$
Thus, the running time is proportional to the following sum: Thus, the time complexity is $O\left(\sqrt{L} + N \log L \cdot d(A+B)\right)$. Note that this is a pretty loose bound, but it still gives an idea of how fast this runs. Time Complexity:$O(\sqrt{L}+N \log L \cdot d(A+B))$, where $L = (A+B)N + AB + C$, and $d$ is the divisor count function. AUTHOR'S AND TESTER'S SOLUTIONS:
This question is marked "community wiki".
asked 20 Nov '16, 21:42
