We hope you enjoyed solving the problems, and thank you for participating! We are very sorry about the error in constraints for D.
The traveler’s position after any number of moves is constrained on a fixed circle which is the circle that passes through the three points: $$$P$$$, position after first move, position after second move. For the traveler to come back to $$$P$$$, he will have to head in the same direction as the initial direction and if he is heading in the same direction as the initial direction then, he must be at $$$P$$$(else his subsequent positions wont lie on the same circle). Thus, $$$n\cdot a$$$ must be a multiple of $$$360$$$. The first time this happens is when he turns a total angle of $$$lcm(a,360)$$$, i.e, the number of moves is $$$\frac{lcm(a,360)}{a}$$$. Thus the total distance is $$$d \cdot \frac{lcm(a,360)}{a}$$$.
Let the direction along which the traveler moves at $$$P$$$ be the $$$y$$$-axis and $$$P$$$ be the origin. Suppose after $$$n$$$ moves, the person is at $$$(x,y)$$$. Then, $$$x=\sum^{n-1}_{i=0}d\cdot \sin(i\cdot a)=\frac{d\cdot \sin(\frac{n\cdot a}{2})\sin(\frac{(n-1)a}{2})}{\sin(\frac{a}{2})}$$$ and $$$y=\sum^{n-1}_{i=0}d\cdot \cos(i\cdot a)=\frac{d\cdot \sin(\frac{n\cdot a}{2})\cos(\frac{(n-1)a}{2})}{\sin(\frac{a}{2})}$$$.
For both the $$$x$$$ and $$$y$$$ positions to be simultaneously $$$0$$$, $$$\frac{n\cdot a}{2}$$$ must be a multiple of $$$180$$$ thus, $$$n\cdot a$$$ must be a multiple of $$$360$$$.
The first time this happens is when total angle turned equals $$$lcm(a,360)$$$, i.e, the number of moves is $$$\frac{lcm(a,360)}{a}$$$. Thus the total distance is $$$d\cdot \frac{lcm(a,360)}{a}$$$.
Note that if $$$n=1$$$, then Alice always wins, since Alice has 1 point and Bob has 0.
Suppose $$$n > 1$$$, we can greedily assume that Alice and Bob always pick a number that gives $$$n$$$ points whenever possible. Thus, if there are $$$k$$$ numbers such that $$$gcd(i,n) = 1$$$, then Alice picks $$$\lceil \frac{k}{2} \rceil$$$ such numbers and Bob picks $$$\lfloor \frac{k}{2} \rfloor$$$ such numbers. Hence, if $$$k$$$ is even, then Alice wins if $$$n$$$ is odd and it is a tie if $$$n$$$ is even. However, if $$$k$$$ is odd, then Alice always wins.
Turns out, for $$$n>2$$$, $$$k$$$ is always even. This can be showed in multiple ways, such as the formula for the Euler Totient function. One other method is to observe that $$$gcd(i, n) = gcd(n-i, n)$$$. This implies that there always an even number of $$$i$$$ such that $$$gcd(i,n) = 1$$$ (Note that $$$i=n-i$$$ implies $$$i = \frac{n}{2}$$$, and thus $$$gcd(i, n) \neq 1$$$.).
Problem C1 — Bargaining (Easy Version)
Since we're given that $$$a_i \leq 2^{15}-1$$$, all $$$a_i$$$'s are 15 bit numbers. Thus, if $$$n > 15$$$, we can set the $$$i$$$-th bit in $$$a_i$$$ by performing the operation on $$$a_i$$$ and $$$a_{16}$$$ if the bit is 0. Thus in this case, the answer is always $$$2^{15}-1$$$.
Now for $$$n \leq 15$$$, we can simply brute force what elements should be flipped. We just need to ensure that the total number of elements whose bits are flipped is even.
Problem C2 — Bargaining (Hard Version)
Claim: For $$$n \geq 8$$$, answer is always $$$2^{63}-1$$$.
Proof: You need to set a total of 63 bits in order to reach $$$2^{63}-1$$$. We can try to set half of the required bits in each step as follows:
- Look at $$$a_1$$$. If less than 32 bits are set in $$$a_1$$$, then flip $$$a_1$$$ along with $$$a_8$$$. Otherwise, do nothing. Now, we've set at least 32 of the required 63 bits. So remaining number of bits we should set is at most 31.
- Look at $$$a_2$$$. If less than 16 of the remaining bits (the ones which were not set in $$$a_1$$$) are set in $$$a_2$$$, then flip $$$a_2$$$ and $$$a_8$$$. Otherwise, do nothing. Now we've set atleast 32 + 16 = 48 bits.
We can continue on in this manner until we are left with no more bits to set. This takes at most $$$\lceil logn \rceil$$$ steps, and we can reach an optimal solution in under 7 operations. We can simply brute force for $$$n \leq 7$$$.
Note that this means we can solve the problem in at most 4 operations as well. We can eliminate all numbers of the array except the first 8, and then brute force on this array. Since a bitmask of size 8 can have at most $$$4$$$ pairs of $$$1$$$, we can solve it in 4 operations.
Problem D — Depression Will be updated soon.
Let us fix the number of non-zero elements in the final array. Let us call this number $$$M$$$ ($$$0 \leq M \leq n)$$$. We will try to solve this problem for this fixed $$$M$$$.
Notice that the minimum sum that you can construct with $$$M$$$ non-zero elements is by choosing the first $$$M$$$ elements of the array. The maximum sum is by choosing the largest $$$M$$$ elements.
Claim: We can make any target sum which lies between the minimum and maximum sum using these operations.
Proof: Let us start by choosing the smallest $$$M$$$ elements of the array, say $$$a_1, a_2, \ldots, a_m$$$. If the sum of these elements is less than $$$k$$$ (our required sum), then we can choose $$$a_1, a_2, \ldots, a_{m-1}, a_{m+1}$$$ and increase our current sum by 1. We can keep on moving the last element until it is $$$a_n$$$. Suppose our current sum is still less than $$$k$$$, then we start moving $$$a_{m-1}$$$ towards the end one step at a time. We can keep on repeating this process for $$$a_{m-2}$$$, $$$a_{m-3}$$$, $$$\ldots$$$ until we reach our target sum. Since we always increase our current sum by $$$1$$$, we are guaranteed to reach every possible sum between the maximum and minimum.
Notice that during this process, the number of subarrays that we set to zero is at most $$$2$$$ at every step. Thus, we can guarantee that the answer will be $$$\leq 2$$$ if it is possible. We can brute force check for answer = $$$0,1$$$. If we don't find a valid solution, then we iterate over $$$M$$$, and check if $$$k$$$ lies between the corresponding minimum and maximum possible sum. If it does, then we can find the answer for this $$$M$$$. If no such $$$M$$$ exists, then output -1, as we have checked all possible cases of $$$M$$$.