I did the most random thing that came to my mind- Keep applying a[i] += a[i] for some steps and check if it becomes divisible by k. If it does, then continue, else the answer is No. Not sure if it is correct or not, but it got accepted anyway.
Link: CodeChef: Practical coding for everyone
I found this problem much more hard then the whole problem set of Div 2 , I upsolved last one just now , and would have done it in contest if I wouldnt have wasted 1:30 hour in this question . Moreover I found GOOD GRID harder than SHSPOONS
I did the same thing. Had just written a few examples and seen that if a[i] \mod k is not reachable after some finite number of operations and started repeating itself, then we can’t have a valid answer.
After that, I defined two sets R and R' R denotes the set of reachable numbers, and R' is its complement.
We precompute the number of reachable numbers. The recursive formula is very simple to understand. It is, suppose we have a number y, it can either come from y / 2 or it can come from (y + k) / 2. Note, if y is odd, then you can just stop the process, because it can’t come from y / 2, or (y + k) / 2. The latter is not applicable when k is odd, because the process will stop at k itself. There is no k / 2, and (k + k) / 2 = k, which we have already found.
So we precompute R, as it is finite, because of the y is odd, you stop the process factor.
Not sure how big the set can be, but shouldn’t be > 10^3
Now, a_i * 2 can be written as (a_i \mod k) * 2
Thus, for all i from 1 to n, we make a_i = a_i \mod k.
So the task reduces to simple checking whether all a[i], whether its in R or not. If all are, the answer is Yes, else its No.
I worked backwards and felt that this was the correct approach(Didn’t prove it properly). Ran the code and was correct. I will be grateful if you can provide a clear proof.
Claim
For the answer to be yes, each number must be either 0 or something that when repeatedly added to itself, gives 0 (in modulo K). Proof
For numbers that are already 0, the proof is trivial. So we only consider those that are non zero.
Let us denote S as the set of all numbers modulo K where S satisfies the property (i.e. repeated addition of any element in S to itself gives 0 modulo K). Denote the rest of numbers as S'
Now, no matter how many operations we do, at least one element will remain in S’. Why? Assume we add two numbers from S’, one of them remains. If we apply the operation from 1 number in S and one in S’, the result is in S’ (easy to prove by contradiction).
Therefore, all elements must be in S, otherwise at least 1 element wont be reducible.
How to construct S?
The construction of S needs some observation.
When we add an element x of S to itself repeatedly, we get x, 2x, 4x, 8x… and so on.
If any of these 2^{n-1}x becomes 0, all numbers after it will be zero as well.
x cannot be 0, as its between 1 and K-1
2x is between 0 and 2K, it can only be 0 at K/2 (if K divides 2)
4x is between 0 and 4K, it can be 0 at K/4, 2K/4, 3K/4 (if K divides 4).
and so on.
Therefore, find the highest power of 2 that divides K, say the power is N.
Then S is the set of all multiples of \frac{K}{2^{N}}.
So can just check the divisibility of each element of A by \frac{K}{2^{N}}, which completes the answer