omsincoconut's blog

By omsincoconut, 13 months ago, In English

I hope everyone enjoyed the tasks, and thank you for participating.

Thank you to the coordinators and testers for suggesting solutions and modifications to the tasks, as I alone wouldn't be able to solve my own tasks or make it to how it is right now. Also thank you to them for dealing with me since July.

Please tell me in the comments if the editorial is written incorrectly or unintelligibly somewhere. I'm not the best at phrasing some things, and would appreciate amendments to the editorial.

2078A - Final Verdict

Hint
Solution

2078B - Vicious Labyrinth

Hint 1
Hint 2
Solution
Extra

2077A - Breach of Faith

Hint 1
Hint 2
Solution
Note

2078D - Scammy Game Ad

Hint 1
Hint 2
Hint 3
Solution 1
Solution 2
Note

2077B - Finding OR Sum

Hint 1
Hint 2
Solution

2077C - Binary Subsequence Value Sum

Hint 1
Hint 2
Hint 3
Solution

2077D - Maximum Polygon

Hint 1
Hint 2
Solution

2077E - Another Folding Strip

Hint 1
Hint 2
Solution

2077F - AND x OR

Hint 1
Hint 2
Solution

2077G - RGB Walking

Hint 1
Hint 2
Hint 3
Solution

Some ending notes

Rating predictions
A small (?) story of this contest.
About Arcaea and the soundtracks
  • Vote: I like it
  • +96
  • Vote: I do not like it

| Write comment?
»
13 months ago, hide # |
 
Vote: I like it +5 Vote: I do not like it

For 1A/2C how does the given solution ensure that the missing number produced satisfies the given constraint of being less than 10^18? Wasn't that also a constraint for the missing number? Am I missing something obvious here?

  • »
    »
    13 months ago, hide # ^ |
    Rev. 2  
    Vote: I like it 0 Vote: I do not like it

    We see that the right hand side of the equation in the editorial is the sum of $$$n+1$$$ terms which are no greater than $$$1e9$$$, so this can't be larger than somewhere around $$$2e14$$$.

    • »
      »
      »
      13 months ago, hide # ^ |
      Rev. 2  
      Vote: I like it 0 Vote: I do not like it

      Ahh right. I forgot to see the bounds for b_i and just assumed they were from 1 to 1e18. Thank you for the clarification!

  • »
    »
    2 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    i have a question why does rearranging the equation works ? like i am assuming the a2n is missing and then rearranging the eqn to find that , but what if i just assume that a1 is missing and calculate ? why is it not the correct solution ? am i missing something ?

»
13 months ago, hide # |
Rev. 2  
Vote: I like it 0 Vote: I do not like it

can someone tell me why this div2 D solution doesnt work https://mirror.codeforces.com/contest/2078/submission/310325385 , its close but i dont know what is wrong with it

maybe i misread the problem at some point

thank you .

»
13 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

can someone please help me understand div2D DP editorial I just do not get it how is it working ?

  • »
    »
    13 months ago, hide # ^ |
    Rev. 4  
    Vote: I like it +1 Vote: I do not like it

    The key idea is that you must decide where to assign your bonus people based not just on the very next gate, but on all the gates that come later. At first, it might seem that looking just one step ahead would work, but that only gets you close—not the full answer as you can see here https://mirror.codeforces.com/contest/2078/submission/310322360.

    Instead, you need to see the entire future. By processing the multiplication operations from the end (backward), you determine the best multiplier chain available for each lane. In simple terms:

    When you add bonus people at a gate, every multiplication that comes after will boost that bonus.
    By working backward, you figure out the maximum boost (or “suffix multiplier”) each lane can get.
    This future boost tells you how valuable it is to add bonus people at a specific moment, so you can make the best decision right then.

    In short, knowing the full future multiplier effect lets you reliably choose the best lane to assign your bonus people—because once they’re placed, you can’t move them later

    you can check my code here https://mirror.codeforces.com/contest/2078/submission/310327912

    where i just compute the greatest multiplying factor using a prefix sum-like approach

    so that i can greedily and reliably decide which lane to insert the bonus people and by having that you can do a normal dynamic programming approach

    i would also encourage you to try all the ideas in your head and see them fail so that you can clear the doubts and understand it perfectly

    Hope this helped :)

»
13 months ago, hide # |
Rev. 2  
Vote: I like it +16 Vote: I do not like it

In problem 1E, I don't think we can prove both $$$l,r$$$ should be in $$$s$$$.

We can only prove $$$l \in s$$$ and the last element $$$k \in s$$$ in $$$[l,r]$$$ has the same parity as $$$r$$$. For example, consider $$$a = [1,0,1]$$$, then $$$s = [1]$$$ but $$$l=1,r=3$$$. Of course in this case the following proof still holds so it doesn't matter much :)

  • »
    »
    13 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    We can only prove $$$l \in s$$$ and the last element $$$k \in s$$$ in $$$[l, r]$$$ has the same parity as $$$r$$$.

    Yes, I had even mentioned it in the editorial but mistakenly wrote that $$$r$$$ should be present in $$$s$$$ as well. I have fixed it.

»
13 months ago, hide # |
 
Vote: I like it +17 Vote: I do not like it

In case someone struggles to understand the $$$\binom{n}{i+cnt_0}$$$ thing like me, we can think it this way.

For a sequence A selected by $$$\binom{n}{i+cnt0}$$$, we form our sequence B by taking the 1s in A and the 0s not in A. Say A has $$$x$$$ 0s. So we have $$$(i + cnt_0 - x)$$$ 1s and $$$(cnt_0 - x)$$$ 0s in B. The score would be $$$(i + cnt_0 - x) - (cnt_0 - x) = i$$$. Namely what we'd like to achieve.

  • »
    »
    13 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    Ohhhh, how is someone supposed to come up with something like this in contest tho?? Thank you so much, I've been trying to get deepseek to explain that derivation to me for the last half hour :_)

    • »
      »
      »
      13 months ago, hide # ^ |
       
      Vote: I like it +27 Vote: I do not like it

      A more mathematical way to prove the binomial coefficient $$$\binom{n}{i + cnt_0}$$$ in div2F:

      Suppose you take $$$o$$$ ones, then the number of zeros required to get score = $$$i$$$ should be $$$o - i$$$. Hence for a fixed $$$o$$$ (number of ones) that yield $$$i$$$ (score), the number of subsequences is equivalent to $$$\binom{\text{cnt}_1}{o} \cdot \binom{\text{cnt}_0}{o - i}$$$. Summing up over all all values of $$$o$$$ yield: $$$\sum_{o = i} ^{\text{cnt}_1} \binom{\text{cnt}_1}{o} \cdot \binom{\text{cnt}_0}{o - i} = \sum_{o = i} ^{\text{cnt}_1} \binom{\text{cnt}_1}{o} \cdot \binom{\text{cnt}_0}{\text{cnt}_0 - o + i}$$$. This is a famous identity and can be calculated by summing upper and lower variables resp. i.e. required value is $$$\binom{\text{cnt}_1 + \text{cnt}_0}{\text{cnt}_0 + i} = \binom{n}{i + \text{cnt}_0}$$$.

      • »
        »
        »
        »
        13 months ago, hide # ^ |
         
        Vote: I like it 0 Vote: I do not like it

        I think you are talking about Vandermonde's identity, $$$\sum_{i=0}^{r}\binom{n}{i}\binom{m}{r-i} = \binom{n+m}{r}$$$.

        In order to make it more complete, I added several steps.

        For $$$\sum_{o=i}^{cnt_1}\binom{cnt1}{o}\binom{cnt_0}{o-i}$$$, let $$$p=o-i$$$, we have $$$\sum_{p=0}^{cnt_1-i}\binom{cnt_1}{p+i}\binom{cnt_0}{p} = \sum_{p=0}^{cnt_1-i}\binom{cnt_1}{cnt_1-i-p}\binom{cnt_0}{p} = \binom{cnt_1+cnt_0}{cnt_1-i} = \binom{n}{cnt_1-i} = \binom{n}{n-cnt_1+i} = \binom{n}{cnt_0+i}$$$.

        Thanks for bringing this up.

»
13 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

I solved Div2 D like "mathematically on paper", storing "variables" at each step and, in the end, maximizing a certain function based on the upper bounds of those variables.

Let's say that in the i-th step, the total number of added people is x. In the next step, I assign y to the left gate and x — y to the right gate, ensuring that 0 <= y <= x. This process continues until the last step, where I sum up the values in the left and right gates and maximize the function.

To achieve that maximization, I looped through the coefficients from the last to the first, setting the variable to 0 if the coefficient was ≤ 0 or to its upper bound if it was ≥ 0

»
13 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Div2 D solution : "With the current number people of l+y on the left side and r+x−y on the right side, we can see that maximizing y is the optimal choice, because there always exist an allocation such that both number of people on the left and right side will exceed those with lower selection of y."

When y gets bigger, r+x-y gets smaller. I cannot understand why poeple on right side can be bigger when y is bigger.

»
13 months ago, hide # |
 
Vote: I like it +3 Vote: I do not like it

I couldn't solve Div2 D during the contest because of the following simplification that I only realized upon reading the editorial.

Let's create an array for each lane: results[i] that states the total number of people we'd have at the end if 1 person entered at the ith gate. Then, if we have N people entering the ith gate then the total count would simply be N * results[i]. However, this only works in case of multiplication but not addition.

Let's try to understand the addition result better. It doesn't matter if X people enter or X+1, if the gate is of type +Y, then we'd get exactly Y extra people. Also, there is one person in each lane at the beginning and they cannot move to another lane (only additional ones can). Therefore, each of the addition operations in both lanes will be triggered no matter what choices we make.

Thus, we can compute an array where suffix_prod[i] denotes the total number of people at the end if exactly one person enters gate i. We can compute which of the next two lanes have a better multiplier (by processing the gates in reverse order) and the additional people can go through that lane, while the one person we had at start continues down the same lane. Then, we can process all the addition events in another loop and each of the additional people generated through them can go through the lane with better multiplier as discussed above.

O(N) implementation

  • »
    »
    13 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    so is my understanding correct that

    • suffix_prod is similar to what results was doing but we can't use it to compute the final result
    • but we can use it to simulate the process and at every gate make a greedy choice for newly generated people and people who entered the gate continue on the same path?
»
13 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

div2-D greedy editorial says When we get more people, place all to where the + will occur first.

is it supposed to be + (plus ) or it should be x ( multiply ) ?

»
13 months ago, hide # |
 
Vote: I like it +10 Vote: I do not like it

2077F — AND x OR
For a<b there are 2 ways: increase a so that a becomes submask of b; increase b so that b becomes supermask of a.
For submask, we can iterate b in descending order, check if the current value is marked, find smallest larger value that is marked and mark all submask of v.
For supermask, we can iterate b in ascending order, find smallest larger value that is marked and mark all supermask of v.
Code: https://mirror.codeforces.com/contest/2077/submission/310448037

»
13 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

My solution to Div1 E is fairly different from the tutorial, so I'm not sure how they are equivalent:

Since the folding operation makes it such that if you increase the darkness of $$$i$$$, you can fold in such a way such that you the increase the darkness of $$$j$$$ if the parity of $$$j$$$ and $$$i$$$ are different. This leads to a dp-like method: Let $$$x$$$ be the operation that can be applied now, in 2 spaces, in 4 spaces, etc, and $$$y$$$ be the operations that can be applied next and in 3 spaces, in 5 spaces, etc. If we are currently at space $$$i$$$, try using as many of the operations we can use, and if not, we will have to use extra operations increasing $$$x$$$. In other words, $$$x = \max(x, a_i)$$$. Now if all of the operations we used we can use again next space (since it has a different parity), along with all of $$$y$$$. Therefore, $$$x = a_i + y$$$. The operations we didn't use, $$$x - a_i$$$, are put in $$$y$$$, since we can't use them next space. This leads to if we loop and do $$$x = a_i + y$$$, $$$y = \max(x - a_i, 0)$$$ (simultaneously), $$$x + y$$$ turns out to be the answer. Now if we index these $$$x$$$ and $$$y$$$, where $$$x_{i,j}$$$ and $$$y_{i,j}$$$ are what results when looping from $$$a_i$$$ to $$$a_j$$$, the formulas turn into $$$x_{i,j} = a_j + y_{i, j-1}$$$, and $$$y_{i, j} = \max(x_{i, j - 1} - a_j, 0)$$$. Now by substituting the $$$x$$$ formula into $$$y$$$ formula, we end up with $$$y_{i, j} = \max(a_{j - 1} - a_j + y_{i, j - 2}, 0)$$$. What we have to find is $$$\sum (x_{i,j} + y_{i,j}) = \sum (y_{i,j-1} + y_{i,j} + a_j) = \sum ja_j + \sum (y_{i,j-1} + y_{i,j})$$$. Computing $$$\sum y_{i,j}$$$ can be done my maintaining a stack of $$$y_{0, i}, y_{1, i}, \dots, y_{i,i}$$$ and popping all of the things below $$$0$$$. Using amortization this turns into $$$O(n)$$$. Code here: 310458499

»
13 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Can someone help me out for the transition of the dp state in D if we consider negative gate values with k skips of gate.

  • »
    »
    13 months ago, hide # ^ |
    Rev. 2  
    Vote: I like it 0 Vote: I do not like it

    yeah its just pretty tough to do it in a sane way , cause with k skips how would you maintain all bonuses contributions Sorted , for each state possible , cause the bonuses added will differ from state to state (i think you can make the sorted bonuses contribution as part of your dp state but still its pretty hard ), if you know a way please share it with me !!

»
13 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Now we know how to get the max score in those games

»
13 months ago, hide # |
Rev. 2  
Vote: I like it +10 Vote: I do not like it

In Div. 1 C, we can solve the problem after Hint. 2.

$$$\sum {\frac{(cnt_0-cnt_1)^2}{4}+\frac{((cnt_0-cnt_1) \bmod 2)}{4}}$$$

$$$=\frac{\sum {cnt_0}^2+\sum {cnt_1}^2+2\sum cnt_0cnt_1+len\bmod 2}{4}$$$

We can use matrix to solve it.

»
13 months ago, hide # |
 
Vote: I like it +8 Vote: I do not like it

1F is very similar to problem K in The 2nd Universal Cup Finals, and I think their main ideas are also almost the same.

»
13 months ago, hide # |
Rev. 4  
Vote: I like it +18 Vote: I do not like it

I find a possible reason why fewer people passed 1D than 1E.

If we judge the difficulty of 1D through its editorial, we might think 1D is quite easy.

But if we don't know the right solution, we often consider how to maximize the lexicographical order at first.

Then a serious problem would appear: many participants don't try to find the subsequence when $$$\max(s_1,s_2,\dots,s_{|s|})$$$ is sure.

»
13 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

For 1F I calculate the possible difference between 2 of the 3 colors, and try to fit one color inside the other 2.
For example:
4 3 3
1 2 6 r
2 3 10 g
3 4 15 b
r=6+12kr; g=10+20kg; b=15+30kb
diff(r,g)=0+4x; diff(g,b)=5+10y; diff(b,r)=3+6z
We can order r,b,g so that b-r=3; g-b=5; g-r=8
https://mirror.codeforces.com/contest/2077/submission/310700347 The solution is accepted but I can't prove it

»
13 months ago, hide # |
 
Vote: I like it +3 Vote: I do not like it

Not a big deal, but in the editorial of Div1E the final formulation should start at $$$l=0$$$ not $$$l=1$$$.

»
13 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

I am struggling in the prove of 2-d Scammy game ad. My Question is why are we solely deciding were to put the gained people on the next multiplier for example we might have x 2 x 3 then x 3 x 2 then x 3 x 2. according to the solution the number of people gained in the starting of the series should be put on the right side but we completely ignored that it could have been much better if we put them on the left one as: 2*3*3>3*2*2. I am not completely sure that is it that I am missing something or I have misunderstood the question.I would appreciate someone clearing my doubt.

Thank You

»
13 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Replacement of $$$|$$$ with $$$\&$$$ in E makes for another interesting problem!

  • »
    »
    10 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    Can u tell me how to solve the problem with OR.Like what the editorial means by removing the contribution of the carry ons??

»
12 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

In the solution for Div 1A/Div 2C, the missing number is assumed to be in the even index what if the index is odd like a2n+1? a2n=a1+(a3−a2)+(a5−a4)+…+(a2n−1−a2n−2)+a2n+1

  • »
    »
    12 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    We can choose where the missing number is, as we only need to recover one of the possible solutions.

    As for finding a way to make the missing number at the odd position, you can do something similar but it doesn't always work.

»
12 months ago, hide # |
Rev. 5  
Vote: I like it +8 Vote: I do not like it

Problem Div2-C/Div1-A can be solved within int limit, no need to go up to 10^18. just sort the number in descending order, let missing number be X, the eqn becomes X=arr[0]-arr[1]+arr[2] ... so X is less than the largest number in the array i.e. arr[0] i.e < 10^9 & greater than 0. Now problem of duplication comes. To counter that,

int diff = arr[0]-X, X=arr[0], arr[0] += diff,

now X is not the new number, its the largest number in the array, the new number is X+diff or arr[0]+diff which is at arr[0] so new number is largest number + diff, but diff<largest number, so new number <2*largest number i.e. arr[0]<2*10^9.

also diff can't be equal to 0, U figure out why :)

»
12 months ago, hide # |
 
Vote: I like it +8 Vote: I do not like it
»
11 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

for Breach of Faith https://mirror.codeforces.com/contest/2077/submission/322129994 this is my approach

sort such that a1 > a2 > a3 < a4 < a5 < a6 < a7 ...

where a1 first largest number a2 second largest number a3 < a4 < a5 ... are sorted in ascending

now i insert element after a1

a1 = x — a2 + ( a3-a4 ) + (a5-a6) + (a7-a8) .... since (-a2) + ( a3-a4 ) + (a5-a6) + (a7-a8) .... = (-ve) + (-ve) + (-ve) + (-ve) ..... [acsending order is the cause of this negative value] moving the -ve's to lhs result in a1 + (+ve) + (+ve) ... = x since a1 is the max element in the array x [second element] is > a1 + (+ve) + (+ve) + (+ve) ...

thus a unique number greater the max(array b) is generated
»
10 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

In the Problem C , what is the proof that the final element is smaller than 10^18 , since it cam be larger than 10^18 , if our array is like this [1,2,3,..,n, 10^18-n, 10^18-(n-1) , .. ,10^18-2, 10^18-1,10^18 ]

Can anyone pls tell .

»
7 months ago, hide # |
Rev. 3  
Vote: I like it +5 Vote: I do not like it

An alternative solution to 1C:

Think of each $$$1$$$'s to have a value $$$+1$$$ and $$$0$$$'s to have value $$$-1$$$. Let $$$a$$$ be the number of $$$1$$$'s in a binary string (so there are $$$n-a$$$ number of $$$-1$$$'s). For each $$$a$$$, there are $$$\binom{n}{a-s}$$$ subsequences with $$$s$$$ number of $$$1$$$'s and a total sum $$$a$$$. We can prove this by applying Vandermonde's Identity on

$$$\sum_{k=0}^{a-s} \binom{a}{s+k} \binom{n-a}{k} = \sum_{k=0}^{a-s} \binom{a}{a-s-k} \binom{n-a}{k}$$$

Then we define

$$$dp(a) = \sum_{s=1}^{a} \lfloor \frac{s}{2} \rfloor \lceil \frac{s}{2} \rceil \binom{n}{a-s}$$$

Note by the discussion in the editorial that this is the answer to the query when there are $a$ $$$1$$$'s in the string $$$s$$$. Then by telescoping sums, we have

$$$dp(a+1) - dp(a) = \sum_{s=1}^{a} \lceil \frac{s}{2} \rceil \binom{n}{a-s}$$$

We then define

$$$p(a) = \sum_{s=1}^{a} \lceil \frac{s}{2} \rceil \binom{n}{a-s}$$$

Then by telescoping sums we have

$$$p(a+1) - p(a) = \binom{n}{a} + \sum_{s=1}^{a} [s \text{ even}] \cdot \binom{n}{a-s}$$$

where $[s \text{ even}]$ is true if $$$s$$$ is even and false otherwise.

We then define

$$$q(a) = \sum_{s=1}^{a} [s \text{ even}] \cdot \binom{n}{a-s}$$$

Then

$$$q(a+2) = q(a) + \binom{n}{a}$$$

and $q(0) = 0, q(1) = 0$ by definition.

We can precompute compute $$$dp, p, q$$$ and the binomials in $$$O(n)$$$ time. The answer to each query is $$$dp(a) + dp(n-a)$$$ because $$$dp(a)$$$ is the answer to the problem if we only consider the subsequences with positive sums. For the negative case, we can flip all $$$0$$$'s to $$$1$$$'s and we will arrive to $$$dp(n-a)$$$ for the subsequences with negative sums. After each query, we update the value of $$$a$$$ accordingly.

»
3 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

The $$$O(1)$$$ formula for 1C can also be proved with generating functions. My approach:

First, notice that $$$F(v,l,r)$$$ is simply the count of $$$1$$$'s in the substring minus the count of $$$0$$$'s, which we can call a "balance", where a $$$1$$$ means add $$$1$$$ to the balance and a $$$0$$$ means subtract $$$1$$$.

Let's denote this "balance" as $$$k$$$. We can prove that from any binary string, the maximum value of

$$$ F(v,1,i)\cdot F(v,i+1,|v|) $$$

over all $$$i$$$ from $$$0$$$ to $$$|v|$$$ will be $$$\frac{k^2}{4}$$$ if $$$k$$$ is even, and $$$\frac{k^2-1}{4}$$$ if $$$k$$$ is odd.

Proof

For simplicity, let's calculate the answer with "optimal scores" of $$$k^2$$$ and $$$k^2-1$$$, then multiply our final answer by the inverse of $$$4$$$ later. Actually, let's take it one step further and just make the score of a binary string $$$k^2$$$. How do we account for the $$$-1$$$ for odd $$$k$$$?

Notice that for any odd $$$k$$$, the respective binary string producing this must also have odd length. This is true because parity is preserved:

$$$ k=\#1-\#0 \equiv \#1+\#0 = |v| \pmod{2}. $$$

So "odd $$$k$$$" $$$\iff$$$ "odd length".

We can count odd subsequences as follows:

$$$ \binom{n}{1}+\binom{n}{3}+\binom{n}{5}+\cdots = 2^{n-1}. $$$
Proof

Given this, we can now treat the score of each subsequence as $$$k^2$$$, then subtract $$$2^{n-1}$$$ from our final answer, and then multiply by $$$4^{-1}$$$.


Let's write a trivial formula for finding how many subsequences will produce a balance of $$$k$$$. Denote $$$a$$$ as the total count of $$$1$$$'s in the string, and $$$b$$$ as the total count of $$$0$$$'s. Since balance is ones minus zeroes, the number of subsequences with balance $$$k$$$ is

$$$ \sum_{i-j=k} \binom{a}{i}\binom{b}{j}. $$$

Now, note that

$$$ (1+x)^a=\sum_{i=0}^a \binom{a}{i}x^i, \qquad (1+x^{-1})^b=\sum_{j=0}^b \binom{b}{j}x^{-j}. $$$

When we multiply these sums, the coefficient of $$$x^k$$$ is exactly the sum of the products of the respective binomial coefficients over all $$$i-j=k$$$. So:

$$$ [x^k](1+x)^a(1+x^{-1})^b. $$$

Rewrite:

$$$ \begin{aligned} (1+x)^a(1+x^{-1})^b &=(1+x)^a\left(\frac{1+x}{x}\right)^b\\ &=(1+x)^a(1+x)^b x^{-b}\\ &=(1+x)^{a+b}x^{-b}. \end{aligned} $$$

Since every exponent is shifted down by $$$b$$$, we just look for the $$$(k+b)$$$-th coefficient of $$$(1+x)^{a+b}$$$:

$$$ [x^{k+b}](1+x)^{a+b}=\binom{a+b}{k+b}. $$$

So the number of subsequences with balance $$$k$$$ is simply $$$\binom{a+b}{k+b}$$$.


Now, back to the task. Our real goal is to find the sum over all possible balances and factor in the score. Since the smallest balance is $$$-b$$$ and the largest is $$$a$$$, this gives:

$$$ \sum_{k=-b}^{a} \binom{a+b}{k+b}\,k^2. $$$

Reindex with $$$i=k+b$$$. Let $$$N=a+b$$$:

$$$ \sum_{i=0}^{N} \binom{N}{i}(i-b)^2. $$$

Expand:

$$$ (i-b)^2=i^2-2bi+b^2. $$$

So the sum becomes:

$$$ \sum_{i=0}^{N} \binom{N}{i} i^2 \;-\;2b\sum_{i=0}^{N}\binom{N}{i} i \;+\;b^2\sum_{i=0}^{N}\binom{N}{i}. $$$
First Term
Second Term
Third Term

Plugging in:

$$$ \begin{aligned} S&:=\textstyle\sum_{k=-b}^{a} \binom{N}{k+b}\,k^2 =N(N+1)2^{N-2}-2b\cdot N2^{N-1}+b^2\cdot 2^{N}. \end{aligned} $$$

Factor $$$2^{N-2}$$$:

$$$ \begin{aligned} S &=2^{N-2}\left(N(N+1)-4bN+4b^2\right)\\ &=2^{N-2}\left((N-2b)^2+N\right). \end{aligned} $$$

Now apply the odd-$$$k$$$ correction and divide by $$$4$$$.

The final answer is:

$$$ \text{Ans} = \left(S-2^{N-1}\right)\cdot 4^{-1}. $$$

Substitute $$$S$$$:

$$$ \begin{aligned} \text{Ans} &=\left(2^{N-2}\left((N-2b)^2+N\right)-2^{N-1}\right)\cdot 4^{-1}\\ &=\boxed{ 2^{N-2}\left((N-2b)^2+N-2\right)\cdot 4^{-1} }. \end{aligned} $$$

This is equivalent to the one in the official editorial.

Thanks for reading!!!

»
3 months ago, hide # |
Rev. 2  
Vote: I like it 0 Vote: I do not like it

Can Anyone help me understand why my solution is wrong? submission: 360822939

Edit: Understood