chokudai's blog

By chokudai, history, 2 weeks ago, In English,

We will hold AtCoder Beginner Contest 133.

The point values will be 100-200-300-400-500-600.

We are looking forward to your participation!

 
 
 
 
  • Vote: I like it
  • +36
  • Vote: I do not like it

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

Thanks for organizing!

I hope this will be my last (rated) ABC. :)

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

How to solve F?

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

    I solved it using Persistent Segment Tree.

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

    We essentially use LCA with square-root decomposition. A more detailed description of my solution is at https://codeforces.com/blog/entry/68231.

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

    I solve by sqrt-decomposition of path (something like LCA).

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

    We can also solve it using BIT maintaining the Eulerian order of the tree.
    Let $$$dis(u,v)$$$ be the sum of $$$d$$$ from $$$u$$$ to $$$v$$$ on the original tree. We have

    $$$Q(x,y,u,v) = dis(u,v) + \sum_{e \in path(u,v),color(e) = x} cst(e) + \sum_{e \in path(u,v),color(e) = x} 1$$$

    Thus, we can solve queries with the same $x$ together. Use two BITs to maintain $$$\sum_{e} cst(e)$$$ and $$$\sum_e 1$$$. Modify it before and clear it after printing answers to them.
    It works in $$$nlogn$$$ time.

    Actually, my solution is the fastest so far:D

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

    I think it can be solved in $$$O(n\ log(n))$$$.

    like this My Submission

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

    I had an online solution with LCA, Eulerian tour and prefix sums. The idea seems to be similar to those of 11235813213455.

    Let's write down an Eulerian tour over edges (counting them with "+" when going down and with "-" when going up). We store prefix sums for all the edges ("large" array), and for edges of each specific color ("small" arrays). When answering the query, we split the path into two parts and consider only paths from LCA to each vertex. To find an answer, we need total path length and the contribution of the specific color. But it's just a sum on a segment (on "large" array or on one of the "small" arrays). Also, to find the bounds of the segment on a "small array", we can store which edges appear in this array and then perform a binary search.

    The code is here.

    This solution is $$$O(N\log N)$$$. It's neither the slowest, nor the fastest :)

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

How to solve E?

I solved F with sqrt-decomposition but didn't find any ideas in E

  • »
    »
    2 weeks ago, # ^ |
    Rev. 5   Vote: I like it +8 Vote: I do not like it

    For E you could use multiplication principle. Start with the root. We have k choices here. Now the first child will have k — 1 choices, second will have k — 2 and so on. But while calling dfs for children keep track of how many siblings of the node you traversed. Because for node x all its siblings wont have an effect on children of x. Just work it out you will get it. One more thing for depths greater than 3 the dependency on the node higher than current nodes by 2 will not be there.

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

    Root the tree arbitarily. If you are at the root. You can assign the color for the root in K ways and its children in (K-1)P(degree[root]) ways. For the other vertices you can assign the colors for its children in (K-2)P(degree[vertex]-1) ways.

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

How to solve Problem D??

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

    It was a system of linear equation. We had equations in the format. x1+x2=a1 x2 +x3=a2 . . . xn + x1=an But if you add and subtract first n-1 terms alternately. You will always get x1-xn = alternate sum of n-1 terms. Using this and last equation we can get x1 and now x2 and x3 and so on till xn can be ascertained

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

    Let's denote the water in dams from 1 to $$$n$$$ as $$$x_1,x_2...x_n$$$ and the water on each mountain as $$$y_1,y_2...y_n$$$.Then the equations read as $$$x_1=(y_1+y_2)/2$$$, $$$x_2=(y_2+y_3)/2$$$, . . . $$$x_n=(y_n+y_1)/2$$$ Now see that $$$x_1-x_2+x_3-x_4+...x_n=y_1$$$ From $$$y_1$$$ we can successively find $$$y_2,y_3$$$ so on by substituting in the original equations.

»
2 weeks ago, # |
Rev. 2   Vote: I like it +9 Vote: I do not like it

Two things.

  1. Since in the last few ABCs some people have volunteered for editorials it would be nice if you can add the link of such editorials in the announcement.

  2. Was E a case of coincidence?

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

I got C wrong, Test case 16, stride_zero_01. I dont get it. Can somebody tell me what is wrong? Thanks

result

  • »
    »
    2 weeks ago, # ^ |
    Rev. 3   Vote: I like it 0 Vote: I do not like it
    2 2020
    

    Your answer is 2, but the correct answer should be 0.

    I guess that you can't just swap l and r after l %= 2019 and r %= 2019.

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

    Swapping l and r if l > r is incorrect. 6 2024 is a countercase: your solution prints 30; the correct answer is 0.

    Adding 2019 to r in this case should fix the error.

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

    Modulos is circular. After taking modulo of l and r with 2019, if r > l then also answer can be zero. For ex. lets take l = 2 and r = 29 and taking mod with 5 , then after taking modulo with 5 , r = 4 and l = 2 , but here answer will be 0 as r became 4 after 3 or 4 round of modulo circle.

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

    Your code gives 2 as output for l=1 and r=2023, ans should be 0(i=1,j=2019) The problem is

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

      This testdata is better than others, my submission code can accept 2 2020, 6 2024. But cannot accept this data.
      MY CODE ON ATCODER

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

        This is because my code turned into 1 and 4, and this causes error to my code

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

    It is enough to have all 2019 modulos of 2019 number. After it you can calculate minimum product of all modulos. In loop from l to r you calculate current module, when you size of modulos reaches 2019 you break from loop. Then you can calculate minimum product.

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

    I have a simple solution. If the distance between l and r is greater than 2019 then it will certainly contain a factor of 2019 and hence the answer will be zero. Otherwise we can calculate by brute force.

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

How to solve Problem F??

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

Can some explain F in simple words i know about sqrt decomposition but was unable to come up with the solution

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

Can anyone explain the logic behind E.

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

    I will try to phase it so that everyone can understand. We will use multiplication principle of counting. Step number one consider any node(preferably 1) as root. We can color it in k ways. Now if I consider its children they cannot be colored in the same color as its parent(as a matter of fact no child can be colored with the same color as its parent). Now for any node, if it has sibling(same parent), it will have a distance of 2 from its sibling. So if a parent can be colored in $$$m$$$ ways, its first child can be colored in at least $$$m - 1$$$ ways(I will come to why more), second child at least $$$(m - 2)$$$, third $$$(m - 3)$$$ ways and so on. If you consider a set of nodes with same parent, the nodes among themselves cannot be colored with same color because they have a distance of 2 within them but their children, will have distance 3 from the other sibling hence, will be independent. To understand, this point consider following connections:

    1 2

    1 3

    2 4

    Here 2 and 3 children of 1 cannot have same color but 3 can have same color as 4 which is 2's child. So how to consider this in multiplication principle? Simple when you call dfs on unvisited children keep track of how many new nodes you found, if you found $$$c$$$ new nodes, then if you call the dfs on the $$$(c + 1)^{th}$$$ new node, you send that information onto it as a function parameter in a variable $$$siblingExplored$$$, so that when you call the child of the node it can get the correct multiplication factor. Let's run our example on an example. n = 5, k = 4 edges: 1 2

    1 3

    3 4

    4 5

    1 can be colored in 4 ways.

    2 can be colored in remaining 3 ways.

    3 can be colored in 2 ways(different from 1 and 2)

    now when we explored 3 we said can be colored in 2 ways, that doesn't mean 4 will be colored in only 1 way, but since we explored 1 child node of parent node 1, we went onto 3, we noted that 2 affects one but we sent that information(of having explored 1 sibling before) onto 3, so the multiplication factor of 4 remained 2 because 2 no longer affects it. And for nodes with height greater than 2 we will not reduce the multiplication factor by 1. Why because even though the number of possible colors are decreasing by 1 but at the same time, the dependecy on parent 3 levels up is also vanishing.

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

Hi! Can someone please share their approach and solution to problems C and D — I got stuck at a step in both of them. Also would be great if editorials can be provided for all problems, would help in upsolving. Thanks!

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

can anyone solve problem B without brute force or use any nice property. Thanks...

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

Official editorials are available here