YouKn0wWho's blog

By YouKn0wWho, 17 months ago, In English

Thanks for participating in the contest blobheart. We hope you liked the problems. We would love to hear your feedback in the comments.

If you find anything wrong in the editorial which is more likely to happen because we have written a rather long editorial to make you understand the solutions better, then comment below.

We also tried to write the thought process of how you can come up with the solution for the easier problems. The approach I followed is similar to what the current AI agents do. They first generate some thoughts, then do some actions, then gather observations, and repeat the process until they find the solution. Hope you will like this approach.

Also, don't forget to upvote the editorial. See you in the next contest!

Also, please rate the problems after checking the editorial. Because otherwise you might have solved it in a very cumbersome way that you will end up hating the problem.


How did you find the contest?
Which problem is your most favourite?
Which problem you hate the most?

Also, huge props to redpanda for creating awesome manim video editorials for problems A to D. Check it out here: https://www.youtube.com/watch?v=elRvvUbk1J4

2039A - Shohag Loves Mod

Author: YouKn0wWho

Tutorial
Code
Rate the Problem

2039B - Shohag Loves Strings

Author: YouKn0wWho

Tutorial
Code
Rate the Problem

2039C1 - Shohag Loves XOR (Easy Version)

Author: YouKn0wWho

Tutorial
Code
Rate the Problem

2039C2 - Shohag Loves XOR (Hard Version)

Author: YouKn0wWho

Tutorial
Code
Rate the Problem

2039D - Shohag Loves GCD

Author: YouKn0wWho

Tutorial
Code (Solution 1)
Code (Solution 2)
Rate the Problem

2039E - Shohag Loves Inversions

Author: YouKn0wWho

Tutorial
Code
Code (by wyrqwq, without initial casework)
Rate the Problem

2039F1 - Shohag Loves Counting (Easy Version)

Author: YouKn0wWho

Tutorial
Code (by LipArcanjo)
Rate the Problem

2039F2 - Shohag Loves Counting (Hard Version)

Author: YouKn0wWho

Tutorial
Code (unoptimized)
Code (optimized)
Rate the Problem

2039G - Shohag Loves Pebae

Author: YouKn0wWho

Tutorial
Code
Rate the Problem

2039H1 - Cool Swap Walk (Easy Version)

Author: wuhudsm

Tutorial
Code
Rate the Problem

2039H2 - Cool Swap Walk (Hard Version)

Author: wuhudsm

Tutorial
Code
Rate the Problem
  • Vote: I like it
  • -167
  • Vote: I do not like it

| Write comment?
»
17 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Even after solving A,B my rating went from 999 to 994 why?

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

Bitmask + XOR = Pain

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

Shohag loves OEIS (specially A079752)

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

LonggVuz bitmasks almost killed me, the problem D saved my rating ._.

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

F2: Cool problem, but why set the time limit so tight? The model solution (and most of the contestants' solutions) run in over 3s, and I couldn't get my solution to run much faster than 5s during the contest. :(

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

    I agree. It's a cool problem but the time limit is too tight

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

I liked B, C1, C2, E. Thanks for the round. H also looks interesting

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

Anyone else solved C2 using binary search ? 😂 😂

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

What's the approximate rating of D?

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

The problem E does not request even ability to think. We need only slow solution that can calculate the number of possible arrays without modulo for $$$n \lt 14$$$ (which is easy to implement) and Wolfram Mathematica function FindSequenceFunction, which attempts to find a simple function that yields the sequence, when given successive integer arguments. Using the slow solution, we get the following sequence $$$(0, 1, 2, 5, 19, 102, 682, 5321, 47071, 464570, 5057058, 60166913, 776595027)$$$ of the answers. Now, we can use FindSequenceFunction to obtain the solution:

FindSequenceFunction[{0, 1, 2, 5, 19, 102, 682, 5321, 47071, 464570, 
  5057058, 60166913, 776595027}]

It gives the answer:

DifferenceRoot[Function[{y, n}, 
    {1 + (2 + n)*y[n] + (-3 - n)*y[1 + n] + 
          y[2 + n] == 0, y[1] == 0, y[2] == 1, y[3] == 2}]]

It is the recurrence relation, which can be simply coded: 292998575.

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

Some learnings as an Author: Judging by the votes and comments it seems like people mostly liked the problems individually but didn't like that most problems were mathy and didn't have enough variations. So I will try to keep that in mind and will add more variations to the contest in the future.

And I am also not happy that during the contest we found out that some people found the second difference/derivative of the sequence on OEIS. I searched on OEIS before but couldn't find it (I saw that it's not there but I didn't notice that there are some deltas matching for another sequence that were written on the OEIS page, I guess I have to be better regarding finding sequences on OEIS). Otherwise, we would have definitely modified the problem. Sorry for this issue.

Hopefully next time I will try to bring a more diverse round. Thanks for participating in the round!

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

Why Shohag loves so much number theory, although problems are really good.

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

Hello every one I have been trying to learn more about mobius function and it’s application but I couldn’t find any resources Can some one help me in this matter ?

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

The time limit of F2 is very bad. The solution which runs in $$$\sum_i \sigma^2(i)$$$ should have passed easily. It should not be a close call. The optimisations into $$$\sum_i \sigma(i) \times p(i)$$$ do not add any new ideas, and the constraints suggest there is a solution faster than this.

The inclusion of both C1 and C2 may make sense in terms of round balance (which frankly, is still pretty bad), but I don't see it being a good idea to test the same skill-set twice.

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

There's a cute solution to D.

Let cnt(x) be the number of divisors of x, not including 1 and x.

Then, set answer[i] to be equal to S[m - cnt(i)].

That's it :)

And if the index is ever equal to zero or negative (assuming indexing starts at 1), then output -1.

The reasoning stems from the same thoughts as the editorial, though I am too tired to write out a formal proof.

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

My solution of how managed to solve C2 but also at the same time failed miserably at it. I am able to skip an observation, but with paid a serious price in terms of implementation.

We need to check for $$$x| x \ xor \ y$$$ or $$$y | x\ xor\ y$$$. The second case is only possible if $$$y \lt = 2x$$$. We focus on the first case, rewrite $$$x xor y$$$ as $$$x + y - 2 (x\ and\ y)$$$. Let $$$k$$$ be the smallest integer such that $$$2^k \gt x$$$. Then, the value of $$$y\ mod\ 2^k$$$ is enough to determine $$$x\ and\ y$$$. For each $$$0 \leq c \lt 2^k$$$, we consider $$$y = a2^k + c$$$, this becomes a certain divisbility condition imposed on $$$a$$$, which can be solved with a certain CRT. This solution is now $$$O(x \log x)$$$.

To optimise, It can be seen (by looking at the implementation), that the required extended modular inverse to calculate is same for every $$$c$$$, so the $$$log x$$$ time to calculate inverse is common. This gives a solution of $$$O(x)$$$.

Being able to come up with a highly technical alternative solution, but unable to actually follow through with it in implementation, turns out to costed me greatly in this round. Submission: 292984478.

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

    The condition $$$x|x \oplus y$$$ is equivalent to $$$x|y-2(x&y)$$$. Since $$$y=a2^k+c$$$, we just need to determine whether there exists some $$$a$$$ st $$$x|a2^k+c-2(x&c)$$$, or equivalently whether there exists some $$$a$$$ st $$$a2^k \equiv 2(x&c)-c \pmod x$$$. Could you please explain how you do this? I tried to understand from your code, but it has always been hard for me to read other's code, plus I don't know Kotlin. Any help would be greatly appreciated, thank you.

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

      you will be seeking some $$$a$$$ such that it is within some calculated range (depending on $$$m$$$), and $$$a2^k \equiv d$$$, where $$$d = 2(c\ and\ x) -x$$$, and thus fixed per $$$c$$$. It is not too difficult to solve that the possible $$$a$$$ must be something of the form $$$As+B$$$ for some integer $$$A$$$,$$$B$$$, which can be calculated with (extended) modular inverses. The formula is as follows:

      fun equateSolution(a:Int, b:Int, m:Int):Pair<Int,Int>?{
          // smallest positive integer k s.t. ka == b (mod m)
          //Return: (smallest positive integer, common differenc between solution)
          val g = gcd(a,m)
          if(b % g != 0) return null
          return ((extendedInverse(a/g, m/g)!! * (b/g).toLong()) % m).toInt() to (m/g)
      }
      
»
17 months ago, hide # |
 
Vote: I like it +8 Vote: I do not like it

xor is like subtraction without borrowing.(problem c2 editorial correction)YouKn0wWho

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

.

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

C2: I found a "cool" way to solve it using some insight in the pattern of valid y when y>x. In particular the pattern has length 2^k, where k is 32 - clz(x) - ctz(x). In this way we can then easily count by blocks of the given length and then iterate through the pattern untill there are enough numbers. This solution works in O(n), my submission took more time because I used larger upperlimits to be sure of choosing the right pattern and avoid silly mistakes.

Submission: 292969986

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

Did I think wrong or there exist a clerical error in the F1 question? If sequence a is a non decreasing sequence, then the maximum value of subinterval with length $$$k$$$ should be the last $$$n - k+1$$$ numbers instead of $$$k$$$ numbers?

»
17 months ago, hide # |
 
Vote: I like it -14 Vote: I do not like it

Shit E.Don't make this kind of problem again.

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

What is OEIS?

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

Thank you YouKn0wWho for such good editorials. I would love to see more such editorials.

Edit: I don't know why did the comment get so many dislikes, sorry if my comment looks irrelevant. I just wanted to appreciate the editorial as I got to learn something new from it.

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

I'd like to make C2 case 1 clearer:

Initial conditions:

  • $$$ 1\le y \le m $$$
  • $$$ p = x \oplus y \rightarrow y = x \oplus p $$$
  • $$$ p | x $$$
  • We need to find values of p such that $$$ 1 \le p \oplus x \le m $$$

Let's consider ranges $$$ [1;m-x]$$$, $$$(m-x;m+x]$$$ and $$$(m+x;\infty)$$$

  • All values of p in the first range $$$ p \le m-x $$$ all valid, because then we say $$$ p + x \le m $$$ and by property $$$ p \oplus x \le p + x $$$, so $$$ p \oplus x \le p + x \le m $$$. Thus, for this range, we add $$$ \lfloor\frac{m-x}{x}\rfloor $$$ to our answer.
  • For second range we can check manually if $$$p$$$ values in that range are divisible by $$$x$$$ and if $$$p\oplus x \le m$$$, and add those values to our answer.
  • For the last range, we know by property that $$$ p - x \le p \oplus x $$$, and as we are in the third range $$$ p-x \gt m $$$, so $$$ m \lt p - x \le p \oplus x $$$. As in this range $$$ m \lt p \oplus x $$$, we don't add anything to our answer.
  • »
    »
    17 months ago, hide # ^ |
    Rev. 3  
    Vote: I like it 0 Vote: I do not like it

    The first point, "... Thus, for this segment, we add $$$\lfloor \frac{m - x}{x} \rfloor$$$ to our answer.".

    I can't understand this last argument. What is the proof?

    Edit: Gotcha! We need to get the number of multiples of $$$x$$$ in the range $$$[1, m - x]$$$ so $$$p$$$ can be any of these multiples and there is always a valid $$$y$$$ for such $$$p$$$.

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

    thank you for your explaination,which helps me a lot QWQ

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

when x=2 y=4 lcm(x,y) = 4 but 2*max(x,y) = 8 it against the Solution in c2 how to solve it

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

The C2's case3 is wrong, x = 2 and y = 4 then boom lcm(x,y)≥2⋅max(x,y) is wrong

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

For C2, the editorial for case 3 says when x isn’t equal to y, lcm(x,y) >= 2*max(x,y) isn’t this not true for x=2, y=4 for example? How does this prove that only x=y works for this case?

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

    An alternative proof would be something like:

    Wlog let y > x. lcm(x,y) >= y. Assume (x ^ y)/lcm(x,y)=k (where k is some integer).

    k!=1, (x^y)/lcm(x,y) <= (x+y)/y < 2. So k = 0 which would mean x=y.

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

Is there a lower bound on the minimum number of operations needed for problem H? My randomized code can pass every test currently using no more than $$$n+3$$$ operations, but cannot squeeze the number to $$$n+2$$$ on test 25.

UPD: This code used at most $$$n-1$$$ operations and passed. Of course we have to use at least $$$1$$$ operation when $$$n=2$$$, so it seems that this code reached the lower bound?

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

    I don't feel like there is any reason why the theoretical lower limit of O(log n) cannot be reached. When solving the problem, I saw that for small n, 3 operations could get every permutation (n<=7). It seems that the operation is extremly versatile, but as usual such versatility is hard to made use of constructively.

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

Well, what's wrong with C2? imo it is indeed a nice problem (maybe nicer without C1). But currently it got 165 downvotes.

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

Shohag really loves GCD.

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

i feel shame for not solving D

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

D , solution 2 i understand that this solution achieve optimal sequence but i can't prove why when im at some index that have m or more divisors and these divisors use all m values so the answer -1 !

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

    at index i, u need to find the biggest number in S where a[i] ≠ a[divisors of i], so when there is no a possible number, the answer is -1.

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

      i mean why it's -1 if there's no possible number depending on my divisors values ,one could say my previous steps were bad that what i need to prove

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

        Because the optimal way is also to choose the max possible number,
        to make sure that a[divisors of i] ≠ gcd(a[divisors of i], a[i]), is to make sure that the values of a[divisors of i] is greater than a[i], then the values of gcd(a[divisors of i],a[i]) will never be equal to a[divisors of i].
        so the answer is -1 when there is no enough numbers.

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

Can someone explain what's going on with their solution to E? In their last paragraph ("And to count..."), their last sum goes from 1+2+3+..,+(m-2) to (m-1)(m-2)/2-1 which has an extra -1 term.

Also, I thought that there would be j places to put the 1 instead of j-1. You can put the extra 1 before the 1 and before any of the previous j-1 zero terms for a total of j instead of j-1 spots.

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

    Never mind, I figured it out. There's just a small typo. Instead of the sum starting at j = 2, it should start at j = 3.

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

Here is an explanation for the formula in G (no explanation was given), for anyone who wants it:

Denote S_g as the number of assignments of a's such that the a's have gcd divisible by g. Denote "a_u can not be a multiple of any prime number p <= h_u" by (*)

First, the formula of S_g only makes sense when considering g's that are good, because otherwise, for a vertex of index x that is on the longest path (so h_x = D), there will be a prime p less than D that divides a_x (since g divides a_x and there is a prime p less than D that divides g, so p divides a_x), so the condition (*) is broken.

The mobius function in there is used for Inclusion-exclusion principle purposes. To see that, let's consider all the primes between 1 and m, denote them by p_1,...p_r. Denote by B_i the set of assignments that have their gcd not divisible by p_i. Then what we desire is the size of the set Intersection(B_i, for i from 1 to r). From the inclusion-exclusion principle: |Inters(all B_i)| = |Union(all B_i)| — sum of all |B_i| + sum of all |B_i inters B_j| — ...

|Union(all B_i)| = S_1, and |for k many B_i's : Inters of B_i's| = S_(product of those k indexes), with its sign being the parity of k. Note that we don't need numbers that contain a prime twice in their factorization, so the mobius function is ideal for our use since it excludes them.

The answer thus is sum from 1 to m of whether_g_is_good(g) * mobius(g) * S_g.

How to compute S_g: Since g divides all a_i, and g is coprime with all numbers between 1 and D since it is good, we thus get that any number p <= h_i that doesn't divide a_i also doesn't divide a_i / g. There are floor(m/g) numbers divisible by g in the interval [1, m]. Hence, for each vertex with index i, we have f_(floor(m/g)),(h_i) ways to choose an a_i that is divisible by g and isn't divisible by anything less than h_i. Therefore, S_g = product for all i in [1, n] of f_(floor(m/g)),(h_i)

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

Have u proved in problem F that all valid sequences can be generated from a valid non-decreasing sequence?

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

    Maximum is on one of the ends of the array. Suppose we remove it. Then all gcd-s still need to be increasing, so the property that maximum is on one of the edges still holds. Suppose our original maximum was on the right. Then we can reverse the array. So when we remove maximum, we always remove the first element. If we wouldn't erase maximums, just reverse the suffix if the first element of that suffix is less than its last. Nothing changed and our array is non-increasing

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

The solution to problem C2 is really confusing.

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

    I have an another thought of that.

    Let $$$x' = 2^k$$$, and $$$\frac{x'}{2} \le x \lt x'$$$.

    For that $$$y \le x'$$$, we could find that the only valid $$$y$$$ satisfy $$$(x \oplus y) \mid y$$$, or $$$x \oplus y$$$ should be over $$$y$$$. So we can bruteforce in $$$[1, x']$$$.

    Let $$$n' = 2^t$$$, and $$$n' \le n \lt 2n'$$$.

    Then we can find that, which $$$p$$$ of $$$px \le n'(p \ge 1)$$$, $$$y = px \oplus x$$$ could be satisfy $$$y \le n$$$. So we can easily calculate it by $$$\left\lfloor\frac{x'}{x'}\right\rfloor / x - 1$$$.

    And for that over $$$n'$$$, bruteforce again.

    You can read my code to summary that.

    293451693

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

Can Someone tell why this solution of Problem-D is giving wrong answer.

For each index i I am checking the idx stored in the factors.

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

how is TC of D nlogn^2 is there a approximation of max no. of divisiors of a number to logn^2?

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

Great contest with a great editorial loved it

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

In Case 1 why can't we just add the number of divisors of x in [1,m+x] in the solution directly

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

in problem E sum of j-1 for j=[2, m-1] , = (m-2)*(m-1)/2, but why are we subtracting 1 here ?