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

Автор Igor_Parfenov, история, 18 месяцев назад, По-русски

2040A - Игра деления

Разбор
Решение C++
Решение Python

2040B - Раскрасить полоску

Разбор
Решение C++
Решение Python
Замечания

2040C - Упорядоченные перестановки

Разбор
Решение C++
Решение Python

2040D - Непростое дерево

Разбор
Решение 1
Решение 2 (zap4eg)
Замечания

2040E - Контроль случайности

Разбор
Решение
Решение (Замечания)
Замечания

2040F - Количество кубов

Разбор
Решение Phi
Решение DP
Разбор задач Codeforces Round 992 (Div. 2)
  • Проголосовать: нравится
  • +104
  • Проголосовать: не нравится

»
18 месяцев назад, скрыть # |
 
Проголосовать: нравится +49 Проголосовать: не нравится

can someone hack my submission for D, it works purely on hopes and dreams. 295631816

»
18 месяцев назад, скрыть # |
 
Проголосовать: нравится +19 Проголосовать: не нравится

fast editorial

»
18 месяцев назад, скрыть # |
Rev. 2  
Проголосовать: нравится -72 Проголосовать: не нравится

Problems like C add nothing to the knowledge of the participants. They just test IQ.

Also, it is exactly same as this problem: https://mirror.codeforces.com/contest/513/problem/B2

Edit: I understand that many of you had fun solving the problem. But there are several participants who just recognized the pattern for smaller n's and hoped it work for larger values of n as well. Isn't it unfair to the people who really come up with and proved their solution?

I mean the testers create randomized cases just to ensure that makeshift solutions do not pass and here anyone who recognized the "pattern" for n <= 6 could get an AC!

»
18 месяцев назад, скрыть # |
 
Проголосовать: нравится +4 Проголосовать: не нравится

oh wow, super fast editorial before system testing, thank you.

»
18 месяцев назад, скрыть # |
 
Проголосовать: нравится +27 Проголосовать: не нравится

Thanks for the really interesting problemset. It's satisfying to see that even E can be implemented so simply without need for advanced data structures.

»
18 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

E can be solved offline in O(Q log^3 N) by using parallel binary search and HLD.

»
18 месяцев назад, скрыть # |
 
Проголосовать: нравится +13 Проголосовать: не нравится

everyone failed E because they misread the constraints

»
18 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Bruh I had no idea how to solve B.

Then I remembered, "If nothing makes your solution work, put it in a binary search" — me

So, I tried binary searching on how many of 1st type operations would be used.

And I got wa. But I will try to solve it using binary search

»
18 месяцев назад, скрыть # |
Rev. 6  
Проголосовать: нравится 0 Проголосовать: не нравится

Here is the 2e5 version of problem E for python. It passed the test, and run within 1s with randomly generated cases on my local machine.

295640041

»
18 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится
»
18 месяцев назад, скрыть # |
Rev. 4  
Проголосовать: нравится +15 Проголосовать: не нравится

My solution of D with the proof:

  1. Find the center of the tree, do dfs that starts from it.
  2. Sort all vertices by (depth, dfs_visit_time) descending order as $$$v_1, v_2, \cdots, v_n$$$.
  3. Assignment $$$a_{v_i} = 2 \times (i + 1)$$$ for $$$v_1, v_2, \cdots, v_{n-1}$$$,and $$$a_{v_n} = 2$$$.
  4. If there is a conflict that $$$v_1 (a_{v_1} = 4)$$$ is a neighbor of $$$v_n (a_{v_n} = 2)$$$,assignment $$$a_{v_1} = 1$$$.

Proof:

Lemma: Since we do dfs from the center, every layer has at least two vertices if the tree has two centers. Otherwise only the layer of the center has only one vertex.

$$$v_i$$$ must be not the neighbor of $$$v_{i+1}$$$ if they have same depth. In the case they have different depth, they can't be adjacent after the sort because the lemma (except $$$v_n$$$ when it's a center). So $$$v_1, v_2, \cdots, v_{n-1}$$$ must be a legal solution.

So we just need to care with $$$v_n (a_{v_n} = 2)$$$. It can only conflict with $$$v_1 (a_{v_1} = 4)$$$. If they are adjacdent, we just assignment $$$a_{v_1} = 1$$$ because $$$v_1$$$ is always a leaf and not about other vertices ($$$v_2, v_3, \cdots, v_{n-1}$$$).

Code: 295631636

»
18 месяцев назад, скрыть # |
 
Проголосовать: нравится +8 Проголосовать: не нравится

B was nice!

»
18 месяцев назад, скрыть # |
Rev. 3  
Проголосовать: нравится 0 Проголосовать: не нравится

ok i am dumb i forgot about last 1 after "1"

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

    Place 1s at indexes 1 and 4

    Fill the indexes 1 through 4

    Place 1 at index 10

    Fill the indexes 1 through 10 (This solves your second question)

    Place a 1 at index 20

    Fill the indexes through 1 through 20

  • »
    »
    18 месяцев назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    3 from 10: initial: 0000000000 after 1: 1000000000 after 2: 1001000000 query type 2: 1111000000 after 3: 1111000001 query type 2: 1111111111

    20 does the same steps up to there but adds one more initial (3 operations done): 11111111110000000000 after 4: 11111111110000000001 query type 2: 11111111111111111111

  • »
    »
    18 месяцев назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    Consider this string: 1001000001 Where the flipped bit are those set using first operation. Now, you can flip the first two bits in between (because there are a total of 4 bits, 2 of which are 1, so the constraint ceil(4/2) >= 2 is satisfied). Then the string becomes: 1111000001 Now, just choose the first and the last bit; l=1, r=10, the constraint ceil(10/2) = 5 >= 5 is satisfied, so we can flip all other bits. Just do it iteratively and you have the solution.

»
18 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Maybe dreams are just form of illusion or they are the future

»
18 месяцев назад, скрыть # |
 
Проголосовать: нравится +14 Проголосовать: не нравится

E can be solved in O(n^2 + q). Link

»
18 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

dreams are illusion or future

»
18 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Cannt problem E be solved with $$$Q \le 10^5$$$ also.

»
18 месяцев назад, скрыть # |
 
Проголосовать: нравится +35 Проголосовать: не нравится

For problem D my solution simply chooses values randomly for each node of the tree in DFS order, repeating until the difference with the node's parent isn't prime.

This works because when $$$n$$$ is large enough, $$$n \, » \, \frac{n}{\ln n}$$$ (or even $$$\frac{2n}{\ln n}$$$), which means there's always a significant fraction of the remaining numbers that would result in a non-prime difference. During the contest I added some logic to potentially restart from scratch and re-randomize if needed for small $$$n$$$, but it seems like you don't even need that: 295643399

Another benefit of this solution is it will work for smaller bounds such as $$$1.5n$$$ instead of $$$2n$$$.

»
18 месяцев назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

nice problems, finally reached pupil!

neat solution to problem C... I found the greedy strategy for problem C but couldn't find a way to implement it other than calculating 2^(n-2), which would not work with the constraints. sad that I could not solve it even after finding the correct logic ;(

»
18 месяцев назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

Hey ladies and gentlemen, this is my code for verifying how the dynamics of the C's solution work https://www.ideone.com/fQB1zU

and following is the submission link of the AC verdict for the corresponding Que' https://mirror.codeforces.com/contest/2040/submission/295645589

hope you understand it, if not, ping me back freely!

»
18 месяцев назад, скрыть # |
 
Проголосовать: нравится +16 Проголосовать: не нравится

Alternative solution for problem E:

Let us solve the problem where $$$p=0$$$ for all queries. Consider two functions $$$A_v$$$ and $$$B_v$$$ where $$$A_v$$$ is the expected number of moves to reach $$$1$$$ if we are currently in $$$v$$$ and $$$i$$$ is odd and $$$B_v$$$ is the same as $$$A_v$$$ but $$$i$$$ is even. $$$A_1=B_1=0$$$, since we are already at $$$1$$$ so we require zero moves.

If $$$i$$$ is odd, then we move to the parent with probability $$$1$$$, hence $$$A_v=B_{\texttt{pr}}+1$$$, where $$$\texttt{pr}$$$ is the parent of $$$v$$$.

If $$$i$$$ is even, then we move to any neighbor vertex with the same probability, hence $$$B_v=\sum\limits_{u \in N(v)}\frac{A_u}{deg[v]}+1$$$. Let us simplify this formula as $$$B_v=\frac{A_{\texttt{pr}}}{deg[v]}+\sum\limits_{u \in \texttt{children}(v)}\frac{A_u}{deg[v]} + 1$$$. Notice, that $$$A_u=B_{\texttt{v}}+1$$$ from the $$$A_v$$$ recurrence, hence $$$B_v=\frac{A_{\texttt{pr}}}{deg[v]}+\sum\limits_{u \in \texttt{children}(v)}\frac{B_v+1}{deg[v]} + 1$$$ The sum does not depand on $$$u$$$ anymore, so we have $$$B_v=\frac{A_{\texttt{pr}}}{deg[v]}+\frac{deg[v]-1}{deg[v]}\cdot (B_v+1) + 1$$$.

Solving this equation for $$$B_v$$$ we have a formula $$$B_v=A_{\texttt{pr}}+2\cdot \texttt{deg}[v]-1$$$.

We have formulas for $$$A_v$$$ and $$$B_v$$$ that depend only on the parent of $$$v$$$, so can run a simple dfs to compute the values for all vertices. The answer for the query is $$$A_v$$$, since we start from the odd $$$i$$$.

But in the original problem we have some amount of coins. The solution doesn't change at all, we just add another parameter to our functions that responds to the number of coins we have, $$$A_{v,p}$$$ and $$$B_{v,p}$$$. The transitions are the same, but we can also spend a coin when updating $$$B_{v, p}$$$, so $$$B_{v, p}$$$ may be $$$A_{\texttt{pr}, p-1}+1$$$.

Precomputing $$$A_{v,p}$$$ and $$$B_{v,p}$$$ for all $$${v,p}$$$ takes us $$$O(n^2)$$$ and answering the queries $$$O(1)$$$.

Implementation: 295642233

»
18 месяцев назад, скрыть # |
Rev. 2  
Проголосовать: нравится +1 Проголосовать: не нравится

C was tragic for me, I actually found the "left or right " construction, I just couldn't get it to work properly, i need to work on constructives

»
18 месяцев назад, скрыть # |
 
Проголосовать: нравится +8 Проголосовать: не нравится

Damn, in D I thought that 1 is considered to be a prime number...

»
18 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I am failing to understand E solution, if anyone could explain in easier words would be really helpful. Thanks beforehand

»
18 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I am failing to understand E, if anyone could help me understand it in easier way. Thanks before hand

»
17 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Damn! I just had to submit this code with C++ 27 to become accepted instead of C++17...

»
17 месяцев назад, скрыть # |
Rev. 3  
Проголосовать: нравится 0 Проголосовать: не нравится

problem E: I thought to build a vector of expected values $$$d$$$, where $$$d_N$$$ is the expected number of moves from a node with $$$N$$$ neighbors to go up (towards vertex 1) of exactly one step, so the parent, when the current counter parity is even. So:

$$$d_N = \frac{1}{N} + \frac{N-1}{N} (2 + d_N)$$$

the first term is the case in which the robot is lucky and goes up, that takes 1 step with probability 1/N; the second one is the probability of going down to one of the $N-1$ below standing children, than going up (2 moves) and the expected value $$$d_N$$$ itself. So, at the end the expression for $$$d_N$$$ is:

$$$d_N = N\frac{2N-1}{N-1}$$$

But it is not correct. Can someone explain me why please?

UPD: the first equation is correct, the final computation is not. As harshaa005 sais:

$$$d_N = 2N - 1$$$
»
17 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

On C I calculated the S(p) up to n = 8 to see the pattern. I did realize that the smaller numbers always ended up in one of the ends of the array and that they had a sort of logarithmic behaviour, like, the permutation changed based on powers of 2.

Though I had no idea how to enumerate them, and to realize afterwards that you can just decompose K into bits and just decide the position based on that is just crazy. Awesome problem, even if it costed me my Specialist.

  • »
    »
    17 месяцев назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    literally same, i found the pattern, not by proof, but in a sort way where i was like, the more big stuff on one side the better, this is probably right and we cant make both sides big for each number, and in a notes document i wrote: "case on dropping to the left or the right, left makes smaller, right makes bigger." however, when I went to check it against samples for "4 6" i considered binary 6 instead of binary 5, so it wasnt right, I gave up on the idea, bricked my mental, and failed the contest

»
17 месяцев назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

For Problem C I printed set of all permutations and tried to recognize a pattern. But Had a very difficult time imaplementing it, and also totally overlook that n is large and I cant use 1<<(n-1). That was my first experience handling such tricky constraints... Really learned so much in this single problem!!!!

»
17 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I solved C in a different way, I just generated all of the permutations with the score of the maximum and the score of the maximum is the score of this permutation that you put the highest one in the middle then on its left the next maximum the on its right the next maximum and so on, so something like this will happen if n = 7

1 4 6 7 5 3 2

then this gave me this result when used n = 5

(1, 2, 3, 4, 5) (1, 2, 3, 5, 4) (1, 2, 4, 5, 3) (1, 2, 5, 4, 3) (1, 3, 4, 5, 2) (1, 3, 5, 4, 2) (1, 4, 5, 3, 2) (1, 5, 4, 3, 2) (2, 3, 4, 5, 1) (2, 3, 5, 4, 1) (2, 4, 5, 3, 1) (2, 5, 4, 3, 1) (3, 4, 5, 2, 1) (3, 5, 4, 2, 1) (4, 5, 3, 2, 1) (5, 4, 3, 2, 1)

and from here I saw they are being repeated by powers of 2 until they reach n

look at the first item in the first 8 places it is 1 and then the others will be 2 in the next 4 perms

and so on, so I coded this pattern

then any number less than n will be printed by just looping from n to 1 and add any not-added-yet numbers

My Submission

»
17 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

For problem E I can't figure out what is wrong with my approach. I see the editorial did something different, but I cam up with geometric distribution to solve it, and it gets till test case 4 and fails on the 138th query.

submission

  • »
    »
    17 месяцев назад, скрыть # ^ |
     
    Проголосовать: нравится +3 Проголосовать: не нравится

    You have a problem with dp. When you have several paths to the state, you set the value from the last path. Instead, assign the minimum value.

    Change dp[u] = dp[v] + x to dp[u] = min(dp[u], dp[v] + x)

    Also, you don't need modulo here, as the answer is always $$$\le$$$ n.

»
17 месяцев назад, скрыть # |
 
Проголосовать: нравится +10 Проголосовать: не нравится

In the Editorial of D:

$$$\text{We will write the values } n\cdot 2,n\cdot 2−1,n\cdot 2−2,\cdots \text{ to the vertices with odd depth in breadth-first order.}$$$

Maybe it is a mistake and should it be:

$$$\text{We will write the values } n\cdot 2,n\cdot 2−2,n\cdot 2−4,\cdots \text{ to the vertices with odd depth in breadth-first order.}$$$

»
17 месяцев назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

Hi, could I check why in the first if block for C we must check for n <= 60? I'm unsure of the intuition behind it.

»
17 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

In B if n=11 and i make arr1=1 and then i make arr5=1 so for now two first type operations are needed and then i apply second type of operation(5/2<=2) and then applying in arr11=1 cant i get it in 3 operations only why the answer is 4 plz someone tell

»
17 месяцев назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

Here is how I solved problem C. Initially, I made some observations:- How will i get the max S(p)? -> By making the contributions of smaller numbers the least possible. How will i do this? -> starting with the current smallest no(which initially is 1) i can prove that it has to be placed either on the extreme left or to the extreme right of the current empty array. This can subsequently be done for the next current smallest values, all follow the same thing.

So getting the kth permutation:- When will the ans not exist? -> when k > no of perm which gives the max value (2 ^ n-1); As k<=1e12, this is only when n-1<40.

Now for each number in the permutation, we have two possibilities either place it in the first or last of the empty array. When right? -> temp = (1LL<<(n — cursmallest — 1)) if(k>temp) then cursmallest will be at the last and simultaneously update k = k — temp;

Elsewhere cursmallest goes to the left position.

Implementation 295706641

»
17 месяцев назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

The Editorial of problem D contains a typo in my opinion. The second solution should have "n.2, (n-1).2, (n-2).2,..." instead of "n.2, n.2-1, n.2-2,...". Please verify this once Igor_Parfenov

»
17 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

can someone explain C in more detail? i recognized the pattern but don't know how to generate the kth one. these are some first permutations for n = 5

permutations for n = 5

I might've missed some here...

  • »
    »
    17 месяцев назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    Here are all the permutations that work for n = 5

    (1, 2, 3, 4, 5)
    (1, 2, 3, 5, 4)
    (1, 2, 4, 5, 3)
    (1, 2, 5, 4, 3)
    (1, 3, 4, 5, 2)
    (1, 3, 5, 4, 2)
    (1, 4, 5, 3, 2)
    (1, 5, 4, 3, 2)
    (2, 3, 4, 5, 1)
    (2, 3, 5, 4, 1)
    (2, 4, 5, 3, 1)
    (2, 5, 4, 3, 1)
    (3, 4, 5, 2, 1)
    (3, 5, 4, 2, 1)
    (4, 5, 3, 2, 1)
    (5, 4, 3, 2, 1)
    

    Here are a couple of observations that I made, bolding the ones that were fairly important

    • A reversed permuation has the same score as the original

    • An element can only appear in the final equation at most (max_element-n+1) times, e.g if the permutation was of length 5, 5 could appear at most once, 4 could appear at most 2 times, etc. Therefore, in order to ensure that the sum is maximized, an element must appear the maximum amount of times that it is able to.

    • the first element from the back will be fixed for 1 permutation, the second element from the back will be fixed for 1 turn, third element 2 turns, fourth element 4 turns, and fifth element 8 turns (with fixed meaning up to that point, it's in the same position that it would be if the permutation was sorted)

    • An element will be a prefix or a suffix of the elements up to that point, e.g in the case where n = 5, 4 could either appear before or after 5, so the possible values of that subarray are 4 5 or 5 4, then once we get to 3, it can be 3 4 5, 3 5 4, 4 5 3, 5 4 3

    • Whether the nth element from the back is at the start or end of the elements up to that point depends on if the modulo value of k by 2^n is greater or less than 2^(n-1) (if it's less than 2^(n-1), it should be added at the front, otherwise at the back.

    I hope this helped (sorry if it's hard to understand, I know I'm not the best at explaining)

    • »
      »
      »
      17 месяцев назад, скрыть # ^ |
       
      Проголосовать: нравится 0 Проголосовать: не нравится

      really didn't understand the 5th point. i couldn't make the 4th observation but i had all the work done before that

      • »
        »
        »
        »
        17 месяцев назад, скрыть # ^ |
         
        Проголосовать: нравится 0 Проголосовать: не нравится

        I'll use the example of n = 5 and k = 9

        Let's start with an empty list (I used a deque in my code, but a list will work to show how it works)

        (I'll mention that I subtract 1 from k to perform the calculations, so k is equal to 8 in my calculations)

        Now let's iterate from 5 to 1 and add the elements to the list

        Since 5 is the first element from the back, we will check if k % 2^0 < 2^-1 (8 % 1 < 0.5). Since it's less than 0.5, we add it to the front, so the list is now [5]

        Now let's go to 4, and check if k % 2^1 < 2^0 (8 % 2 < 1). Since this is true, we add it to the front, and the list is now [4, 5]

        For 3, k % 2^2 < 2^1 (8 % 4 < 2) is true, so we add it to the front, and the list is now [3, 4, 5]

        For 2, k % 2^3 < 2^2 (8 % 8 < 4) is true, so we add it to the front, and the list is now [2, 3, 4, 5]

        For 1, k % 2^4 < 2^3 (8 % 16 < 8) is false, so we add it to the back, and the list is now [2, 3, 4, 5, 1]

        Our final list is [2, 3, 4, 5, 1]

»
17 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Problem 2 — Can someone explain me the 4 cuts for n=20? My understanding says we require atleast 5 operations of type A performed on i=1,4,8,16,20.

»
17 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

fun fact: problem E can be solved without modulus operation.

Submission: 295789260

»
17 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I learned something new from problem F

»
17 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

can somebody explain c in detail

  • »
    »
    16 месяцев назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    Consider how many times each element appears in formula. For example, for n=9 it's [9, 16, 21, 24, 25, 24, 21, 16, 9]. This means that smallest number should be placed at the edges, to affect less segments. After you placed $$$x$$$, it will change number of segments $$$x+1$$$ will affect, so this can be viewed as placing numbers starting from $$$x+1$$$ in empty array of length $$$n-1$$$.

»
17 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Late note: problem E can be solved in $$$\Theta(n + q)$$$ using the fact that $$$\sum d_u = \Theta(n)$$$.

»
17 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

How do you even think of C? I can understand the editorial and follow along, but the idea is just so unexpected.

»
11 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится
»
11 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

For D, I solved it by finding the diameter of the tree. Say we got a diameter from node u to node v with $$$m$$$ vertices on it, then we just assign the values 1, 2, 3, ..., m to the vertices on the tree. After that, we loop through the diameter's vertices, explore all branches, and assign the remaining values (i.e., from $$$m + 1$$$ up to $$$n$$$) to vertices on those branches. The assignment is simple: at each vertex, we find the first remaining value that satisfies our condition and assign it to it.

Solution implementation: 325626482