SashaT9's blog

By SashaT9, 9 months ago, In English

Hello everyone!

I invite you to the contest where I authored all the problems. There are tasks that couldn't make it to the Official Codeforces Round. But they are still not that bad :)

I hope you enjoy the problems. Thanks for participating!

If you have any feedback or suggestions, please feel free to leave them in the comments. Your feedback will help improve my future contests.

UPD: My apologies! The author's idea for problem D was incorrect. I hope that the solution is now correct. All the submissions for this problem were rejudged, and three samples were added.

UPD2: Problem D has been hopefully fixed. Many thanks to Yam for detecting and correcting the problem.

Announcement of SashaT9 Contest 1
  • Vote: I like it
  • +61
  • Vote: I do not like it

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

It's interesting how awful problems like B, F and H are in the same set as D and G. Were they rejected for CF round? Why? And why did you decide to just post them instead of keeping them for some other contest?

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

    Firstly I was preparing these problems for CF. However, I received a proposal to create a contest for Algotester, and some of these problems were used there (but not all). I then decided to also post them on CF to make them accessible beyond Ukraine.

  • »
    »
    9 months ago, # ^ |
      Vote: I like it +5 Vote: I do not like it

    Just curious, why do you think that B and H are awful?

    Here are my opinions on the problems (I didn't read D because I probably won't be able to solve, or it would take too long):

    F
    H
    B
    G
    • »
      »
      »
      9 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Thank you for the feedback!

      I fixed the statement in problem B.

    • »
      »
      »
      9 months ago, # ^ |
        Vote: I like it +37 Vote: I do not like it

      They are very boring and standard. I'm sorry if it's offensive. From the announcement I got the impression that the contest is constructed from problems that were rejected for the round, and most of the problems look like it. I would reject everything but D and G, and for B/F/H I wouldn't feel like I need to explain the reason. But G seems like a decent problem for div2 round (maybe even for div1), and D is actually very nice. So that seemed weird and like a waste of good problems, that's why I asked.

      Actually, D seemed so out of place that I had a suspicion that the author's solution might be incorrect since I certainly wouldn't waste a problem with such a nice solution. But I might have missed some simpler solution, so it didn't seem fair to suggest this. Looks like I was correct though.

»
9 months ago, # |
  Vote: I like it +26 Vote: I do not like it

It's funny that when we were making a round, we thought independently of task F and tried to make it D2B)))

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

Just amazing SashaT9, Good Going Buddy!! Just contacted you when I was specialist & you were Expert & now we both are one step ahead!!! Lets hope for the best and very best of us both...

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

If possible provide tutorial as well. It will be helpful for people who are new to competitive programming like me.

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

    I haven't done it yet. Maybe I'll work on the editorial soon)

»
9 months ago, # |
  Vote: I like it +15 Vote: I do not like it

enjoyed the contest, however felt like there was a big difficulty gap between DG and rest. Thanks anyways

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

problem F is reversed of an ABC problem, where author asked to maximized diameter.

»
9 months ago, # |
  Vote: I like it +5 Vote: I do not like it

How to solve G?

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

    $$$gcd(a_i + j, a_j + i) = (i+j)$$$ means that

    • $$$a_i + j$$$ can be written as $$$(i+j) \cdot k$$$
    • $$$a_j + i$$$ can be written as $$$(i+j) \cdot y$$$
    • $$$(k,y)=1$$$

    Thus we know that $$$a_i = i \cdot k + j \cdot (k-1)$$$ for some $$$k$$$. Let's itterate through all possible pairs of $$$(i,k)$$$, if $$$k > 1$$$ we already know $$$j$$$ so we can test if the found pair works indeed.

    Now, if we save all pairs that work in a set, we can note that pairs that are never counted are the ones in which both $$$k=1$$$ and $$$y=1$$$, which means $$$a_i = i$$$ and $$$a_j = j$$$, so just count all pairs of fixed points.

    Complexity is $$$O(vmax \cdot \sqrt{vmax} \cdot log)$$$, but magically, it runs in < 100ms ( probably testcases are not very strong ). You can view my code here.

    • »
      »
      »
      9 months ago, # ^ |
        Vote: I like it +8 Vote: I do not like it

      Nice solution! I have another approach:

      $$$\gcd(a_i+j, a_j+i) = i+j \leftrightarrow \gcd((a_i-i)+i+j, (a_j-j)+i+j) = i+j$$$

      Now, if we assign a new array $$$b_i = a_i - i$$$, the equation becomes $$$\gcd(b_i + i + j, b_j + i + j) = i + j$$$

      Here, we notice that $$$i+j$$$ may be a divisor of $$$b_i$$$, so we can iterate through all the divisors of $$$b_i$$$. If the divisor is $$$d$$$, we get $$$i+j=d \leftrightarrow j=d-i$$$, and all we need to do is check if such $$$j$$$ satisfies the equation. Complexity is $$$O(n\sqrt{maxA}\log n)$$$.

      My code
      • »
        »
        »
        »
        9 months ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Really liked this problem personally, Thanks for sharing !!!

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

How to solve E?

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

    Declare a new array $$$c$$$ of length $$$2 \cdot n$$$, where $$$c_{2i} = \max(a_i, b_i)$$$ and $$$c_{2i+1} = \min(a_i, b_i)$$$. After that, you can find LIS on this array and obtain the answer to the problem.

    This algorithm works because in such constructions, we can't take both $$$a_i$$$ and $$$b_i$$$ simultaneously (since $$$\max(a_i, b_i) \geq \min(a_i, b_i)$$$).

    My code
»
9 months ago, # |
Rev. 5   Vote: I like it +3 Vote: I do not like it

How to solve problem D. SashaT9 can you give me some hints/solution idea thanks in advance :)

  • »
    »
    9 months ago, # ^ |
    Rev. 3   Vote: I like it +5 Vote: I do not like it
    Hint 1
    Hint 2
    Hint 3
    Solution
    Code
    • »
      »
      »
      9 months ago, # ^ |
      Rev. 2   Vote: I like it +18 Vote: I do not like it

      It won't really make any difference in practice since the domitaing part of the code is reading input, but we can optimize the dp to $$$O(A^3)$$$. Instead of calculating the dp separately for each starting point, we can calculate the dp once for a cyclic array.

      My dp is similar to the one you explained, but possibly slightly different.

      Code:
»
9 months ago, # |
  Vote: I like it 0 Vote: I do not like it

can somebody provide solution for problem A and B

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

    Sure : So I have observed a pattern by writing bruteforce solution and running for 1-100

    Bruteforce Code
    Optimised Code
»
9 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Can somebody explain how to solve problem C.

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

    Let's find the subsequence with maximal length for which the greatest common divisor is equal to $$$d$$$. We can use numbers $$$d, 2d, \dots, sd$$$, and their GCD is guaranteed to be at least $$$d$$$.

    Now, the observation is that if there exists a subsequence of length $$$k$$$ with GCD $$$d$$$, then there is also a subsequence with a length of $$$k-1$$$ and a GCD of $$$d$$$ (since we may choose not to include one of the elements from the set $$$d, 2d, \dots, sd$$$).

    The algorithm is as follows:

    1. Create an array $$$c_x$$$ to store the number of occurrences of $$$x$$$ in the array.
    2. Iterate through all values of $$$d$$$ and find the maximum $$$k$$$ for which the GCD equals $$$d$$$. For each value of $$$k$$$, update the corresponding entry with value $$$d$$$ in an array called $$$mx$$$.
    3. Iterate through the array $$$mx$$$ from the end, and for each index $$$i$$$, update $$$mx_i$$$ to be the maximum value between $$$mx_i$$$ and $$$mx_{i+1}$$$.
    4. Output the array $$$mx$$$.

    Complexity is $$$O(n+A\log A)$$$.

    Code
»
9 months ago, # |
  Vote: I like it +7 Vote: I do not like it

I know the solution to problem A

solution

Can anyone help me why this is the solution?

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

    maybe you cant find it with condradiction

    if x <= n, then after xor-ing every permutation, there is always 0 in the end

    if x > n, then exist at the end of operation, exist 1 ⊕ x, which equal to x+1, if x is even, or x- 1, if x is odd. of course we cannot use x even, because it will produce x+1, which more than n, therefore, the array after operation wont be a permutation

    so the only possible x is n+1.

    Now, why the only possible solution for x is (2^n — 1, a.k.a 111...111₍₂₎). If x is not 2^n — 1, then x contain 0 in its biner representation, therefore, you can pick a number "i" that's reversal of x in its biner representation. i <= n < x

    Example

    x = 111110₍₂₎, then pick i = 000001₍₂₎, i ⊕ x = 111111₍₂₎, therefore i ⊕ x is bigger than n

    So, the only solution is where x = n+1, and x = 111...111₍₂₎

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

How to solve problem F ?

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

    let a = diameter of first tree, and b = diameter of second tree

    ans = max(max(a, b), ceil(a/2) + ceil(b/2) + 1)

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

Any hint to solve F? My idea got WA. My idea is that we find node in both tree where the longest path starting from that node. After that, we can add an edge between both node and start doing dfs to find the diameter of the merged tree.

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

    You should use dsu to solve this problem

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

      Care to give me more hint about it? hehe

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

        When you merge two trees it is enough to know diameter of each tree to find the minimum diameter

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

          Sorry. I thought that there was queries. So you don't need dsu. You only need to know the diameters of each tree to find result

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

          Why is it simply enough to know the two diameters? What is the intuition behind this?

»
2 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

how do you manage to solve more than 5k problems within 3 years?

  • »
    »
    2 weeks ago, # ^ |
      Vote: I like it +11 Vote: I do not like it

    It was so exciting that I didn't even notice the number of tasks I solved:)