https://mirror.codeforces.com/contest/1945/problem/H
Hey cfers hope all are doing well can anyone please tell me how to solve this please do share your code and logic thanks.
# | User | Rating |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 150 |
https://mirror.codeforces.com/contest/1945/problem/H
Hey cfers hope all are doing well can anyone please tell me how to solve this please do share your code and logic thanks.
Name |
---|
Hints for reaching the solution
1945H - GCD is Greater
Fact 1 : It is always better to just have 2 numbers in the gcd.
This can be easily proven, by the test cases.. uhm, we can just say that if there are greater than 2 numbers in the gcd then we can keep removing them from there and add them in AND, gcd might increase but can't decrease and AND might decrease but can't increase.
Let cnt[bit] be the number of elements in the array that have this bit off (0). We also store the elements for every bit in a vector[bit] as well.
Now, if there are more than 2 elements for this bit then no matter which 2 elements we chose for our gcd the AND will have this bit off.
Hence, we come to this idea that if there are <= 2 elements for this bit, then just check them manually. This will take O(2 * LG * n * LG) (LG = 19, and second LG is for computing gcd).
Now, if we found the answer there then good. But, if not then, we can now just ignore them, we will never keep them in our gcd (because we tried that). Let's say we have marked these elements in our boolean array mark.
Therefore, if cnt[bit] > 0 then this bit is surely off in our AND and otherwise it's surely on.
if cnt[bit] > 2, then since we are just choosing 2 elements for gcd the other elements will go in AND and turn it off.
if 0 < cnt[bit] <= 2, then we just checked those elements that if we put them in gcd, do they give us the answer, they did not, so we are now putting them in AND.
So, we are making every bit off which can be turned off.
So, now our AND is fixed. We just want gcd of two numbers to be greater than some X (X = Fixed_AND + x). Now, we can just do a cnt of multiples that how many multiples does this d have, not considering marked elements.
Now, we say (gcd = max_element; gcd > X; gcd--) and see if muls[gcd] >= 2, if yes then just find them (again don't consider the marked ones here) and print. Otherwise, if the for loop ends then the answer is NO.
Time Complexity : O(n * LG * LG + n * LG + MAX_element) = O(n * LG * LG)
n * LG * LG, for the brute force, manual check.
n * LG, for the sieve (to compute muls[d]).
MAX_element, for finding the answer, note that it is mentioned in the input of the problem that sum of max_a[i] <= 4 * 10 ^ 5.
we don't need to store the cnt[bit], we just say vec[bit].push_back(a[i]), if a[i] does not contain this bit.
Then we go over all the bits, and check if vec[bit].size() <= 2. If yes, then we iterate on those elements and add them in our GCD, and check if they give the answer. We get the other element which will be paired with it. Let the first element be at position i and the second element at position j, we can compute gcd(a[i], a[j]), but how to do AND(a[0..(i-1)]) & AND(a[(i+1)..(j-1)] & AND[(j+1)..(n-1)]. The first one is prefix AND, the last one is suffix AND but the middle one, uh oh, have to use sparse table, but wait we can just iterate on j in such a way that we don't need that. We first say j = i + 1 and keep moving forward while updating the middle AND each time we go forward, and then later we say j = i-1 and keep decreasing it and updating the middle AND.
I was in a hurry during the contest, so my implementation was not much neat, here is it anyway 252257639.
This was all but, if somebody have any questions, I will be happy to answer.
True. But if you do store it, your implementation will become a lot simpler. If you have the count of zeroes at each position, then, when you remove $$$a[i]$$$ and $$$a[j]$$$ from the set, you simply decrease the count by $$$0$$$ or $$$1$$$ or $$$2$$$ (depending upon whether the outgoing element has that bit set or unset). Then, if the count is zero, the bitwise AND would contain $$$1$$$ at that position. We can repeat for each bit.
Sample Submission
Thank you
I've added hints for H : GCD is Greater on CF Step
I loved the editorial I performed all the 4 soln that mention including the wa3 of nlogn too made my clear concepts thnks you