Блог пользователя ps06756

Автор ps06756, история, 9 лет назад, По-английски

Hello all, I have tried unsuccessfully to solve the problem Bear and the Polynomials

I am unable to understand the editorial posted for the problem, can some one please post their approach to solve the problem, or explain the approach discussed in the editorial clearly.

  • Проголосовать: нравится
  • +4
  • Проголосовать: не нравится

»
9 лет назад, # |
  Проголосовать: нравится +3 Проголосовать: не нравится

I'll try to explain my solution. Let's forget about the |x| ≤ k restriction at first, where x is our answer. Consider that every binary string used in this explanation is written from left to right from the least significant digit to the most significant one.

You are given a such that a020 + a121 + ... + an2n ≠ 0. One possibility to solve this problem is to find, for each coefficient, if it's possible to zero out the expression changing its value (this new value will be unique for each coefficient). Let's try to write down an equation which reflects that. Let c be the new value of the coefficient you will change and i be its index.

a020 + ... + c2i + ... + an2n = 0

So first of all our expression should be divisible by 2i, where i is the index of the coefficient we are changing. Of course all terms with index j ≥ i are divisible by 2i (that's because they are in the form ai + p2i + p, p ≥ 0). So if the sum of the first i terms of our expression is divisible by 2i, then we can change this coefficient and zero it out and, more over, our answer is c in the given equation. Well.. but |c| may be large enough to exceed k. It turns out that we can implement the whole thing in such a way that it will be very easy to overcome this.

Let's change our expression so

Unable to parse markup [type=CF_TEX]

for every i. I.e, we will get some sequence of -1s, 0s and 1s. You can check it won't grow too much. We can do that by adding and carrying each coefficient (similar to the sum of binary numbers). Since we are allowing -1s, we lose some properties from binary representation, but we still have one very important: there's only one way to represent the zero. Well.. how do we know that a number is divisible by 2i looking at its binary representation? Count if the size of the largest prefix which contains only zeroes is greater or equal than i! The same holds here, because our remainder of the division by 2i is still represented by the first i digits, like in usual binary representation (ofc, it may be negative and we need to fix, but in this problem we don't really need to care about this case).

So if we have 001001, then this number is divisible by 20, 21, 22.

Ok, now from the new representation we can answer the easier problem with no restriction in the coefficients: it's simply the size of the prefix. But let's finally take into account the k constraint. If the remainder of our division is represented by the first i digits, guess where is the quocient? Yeah, the digits that are left! So if we do a shift of size i, like in usual binary representation, we get the quocient of the division by 2i.

In 001001, if we divide by 22, then the quocient is 1001 as you may already know. (although i'm not writing down numbers with negative digits, they may exist)

That shift we got is a suffix of our string. One smart way to pick the quocients is going backwards in this string and stop as soon as it gets bigger than 232 in absolute value. Since this text is getting really huge, I invite you to check that it never goes below k again. After getting the quocient q, we just need to check if the complementary prefix is divisible by 2i. Then c = q - ai and answer should be incremented if |c| ≤ k.

You can check my code to see how to implement this last piece: 17089440

  • »
    »
    9 лет назад, # ^ |
    Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

    Thanks a lot for the detailed explanation. I didn't understand how are you able to change the coefficients of the polynomial to {-1,0,1}. Is there a specific name for this process.