nevidomy's blog

By nevidomy, 13 years ago, translation, In English

Problem А.

Since the restrictions were not so big you can offer many different solutions. For example, you can construct a graph with N * 4 vertices and use bfs. But there was a faster solution with complexity O(1): enumerate the integer points lying on the square, for example, as is shown below:


Then finding the number of a point by its coordinates isn’t difficult. For example if a point has coordinate y == n -> then its position is n + x (if the numeration, is as shown, i.e. starts with zero). It turns out that we have transformed the square into a line with a small complication: cells N * 4 - 1 and 0 are adjacent, so we have not a line but a circle, where for the base points from 0 to 4 * N-1 the distance between the adjacent two points is 1. It is easy to take the difference of indices of the points and find the distance when moving clockwise and counterclockwise: (a-b +4 * n)% (4 * n) and (b - a + 4 * n)% (4 * n), the shortest distance will be the answer.

Problem B

Under the restrictions on the number of requests, you cannot iteratively add staircase. But the problem is simplified by the fact that the number of cells in which it was necessary to know the number of stones was no more than 100. That’s why you can check for each "interesting" cell all the queries and calculate how much each of them adds stones, so the complexity is: O (K * M), which fits within the given 2 seconds well. A faster solution with complexity O(N + M + K) is as follows: we will keep in mind two variables: a – how many items should be added now to the cell, v - how much should change a next step. So problem is to update the value of a and v so that a equaled to the number of stones in the cell after all the operations, and v – to how much a increases during the transition to the next cell. Then the problem will be reduced to that you need to properly update a and v, which is not very difficult to do, for example if there is a request (x, y, z) – then in the cell x you need to add x to a and add 1 to v, since with each next cell the number of stones for some staircase will increase by 1. Similarly, after the cell y is passed you need to subtract 1 from v and pick up from “a” the current number of stones subquery. If you save all such pairs of queries in a single array and it's possible to calculate everything in one cycle.

Problem C

First, let's count how many there are arrays in which each successive element starting from the second one is no less than the previous one. To do this quickly, let’s take a piece of squared with height of one square and the length of N*2 - 1 square, then select N-1 squares and put crosses in them - let it encode some array, let the number of blank cells from the beginning of the array before the first cross be equal to the number of ones in the array, the number of blank cells from the first cross to the second – to the number of 2 in the array, and so on. It is easy to see that the number of empty cells will be equal to N * 2-1 - (N-1) = N. It turns out that each paper encodes an array which suits us, and moreover all the possible arrays can be encoded using such paper. There is exactly C (N * 2-1, N) different pieces of paper (the binomial coefficient of N * 2-1 to N). This number can be calculated by formula with factorials, the only difficulty is division. Since the module was prime - it was possible to calculate the inverse number in the field for the module and replace division by multiplication. So we get the number of non-decreasing sequences, respectively have the same number of non-increasing, we just need to take away those who were represented in both array sets, but they are just arrays with a similar number like {1,1,1,...,1} or {4,4,...,4,4} just subtract n.

Problem D

Let's see, if there is no occupied cells, then it is not difficult to calculate the answer - the answer is a sum of Manhattan distances, taking into account the fact that the Manhattan distance can be divided: first we can calculate the distance of one coordinate and then of another one and then just to sum up, so the answer is not difficult to find. What do we have when there are occupied cells? If the cell from which we seek the distance does not lie on the same horizontal or vertical with occupied cells – then it absolutely does not affect the difficulty of accessing the rest of the cells:


This happens due to the fact that in each cell from the previous distance front (the distance to the set of cells with maximum distance from the start cell, the fronts form a diamond as you can see in the picture or from the Manhattan distance formula) there are two ways how to get to the cell, we take into account the rule that no two cells can be adjacent (either diagonally or vertically or horizontally), it turns out that such a cell does not interfere with us. But a close look at the picture brings to mind the obvious: it is the cell which is located on the same vertical / horizontal that has only one neighbor from the previous front, let’s see what happens in such case:


All the cells after occupied one starts "to lag" at 2 in distance, but more importantly, that changed the front! Now (with the upper side of the front) are 2 cells for which the transition from the previous front is unique, and as a consequence there are two cells meeting of which will produce a new segment of a late cells, and the angle of the front will change again. Since we are only interested in one side of its expansion (on the other hand there can not be occupied cells by the condition) so we can assume that it was expand from the start cell:


It turns out that you can count how many cells will be "delayed", especially considering that the lag always equals to two. For each direction, you can choose each occupied cell and check in the left and right directions for adjacent cells series, and add delay for interval of free cell before occupied cell (in the current direction). It is possible to calculate such delays with complexity: square of the number of occupied cells (it is not greater than min(N, M)). The rest is details. You need to compute all Manhattan distances, taking into account the existence of occupied cells. To do this, first calculate the sum of distances for an empty field, then for each occupied cell calculate the distance to all the others - subtract it twice, as the extra distance is subtracted for the first time when we got out of this cell and for the second time when takes away from all other cells to the given one. But in this case we will take away twice the distance between two occupied cells - so we’ll have to add them (the complexity is also a square). A total complexity of solution is O (K ^ 2) where K is the number of occupied cells.

Problem E

The task turned out to be very difficult as it was planned. First remove all the deleted cells - let's see how changes the number of accessible cells with the increase of the new moves. First, the changes do not appear stable, but at some point we see a figure which won’t to change the form anymore and will only expand, it is obvious that the figure is two-dimensional, therefore the growth of its “area” is a quadratic function, in fact, a simple check shows that with each new turn the number of cells increases by the number of new cells opened with previous turn + 28, so after a certain point, you can simply take the sum of an arithmetic progression. The case with the deleted cells is similar. In general, there are several possible options - either occupied cells block further penetration of the horse or not, in the first case bfs is simply enough to find solutions, in the second one the story is the same: over time, when the figure "becomes balanced", the number of new opened cells satisfies the same rule "prev +28" . Moreover, because of restrictions of the coordinates of the deleted cells (-10 <= x, y <= 10) this happens fairly quickly. So, use usual bfs to balance figures, and after this use the sum of the arithmetic progression.

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

| Write comment?
13 years ago, # |
Rev. 5   Vote: I like it +4 Vote: I do not like it
the translation is a bit difficult to understand...= =
13 years ago, # |
  Vote: I like it +3 Vote: I do not like it
I can't clearly understand the solution to B here.
I submitted an O(M*K) in the contest which passed.

Now, I tried another approach using fenwick tree , complexity O(M log N + K log N) which passed too. Here's a link to see the code : http://pastebin.com/SAeFbdcG

Is this what is intended in the tutorial or a much simpler solution ?

13 years ago, # |
  Vote: I like it 0 Vote: I do not like it
I'm not that good at maths and although I knew little Fermat's theorem before, using it in that way was something new to me. 

But what if the module wasn't a primer number? In the contest I used dp to calculate the combination and my approach isn't depended by the module. I'm curious if there is a way (maybe involving math) different than this if the module isn't prime?
  • 13 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it
    Theoretically you can use Chinese remainder theorem on prime factorization of module and then restore answer.
  • 13 years ago, # ^ |
    Rev. 2   Vote: I like it +3 Vote: I do not like it
    Euler's totient theorem is a generalization of Fermat's one and works for any module. Also you can solve ax ≡ 1 (mod b) equation by solving its equivalent Diophantine equation ax + by = 1 with extended Euclidean algorithm.
  • 13 years ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it
    I didn't use any thing about prime module in my solution. For all prime numbers between 1 and 2n-1 I just computed their power in C(2n-1, n) and then multiplied the result with it. Computing the power was easy, like computing the number of first zero digits of n!. It ran in 130ms.
    Here is my code: http://pastebin.com/Xieb5PuP
13 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it
Thanks for the tutorial. But I cant understand the explanation of problem C. The explanation is very confusing :(
13 years ago, # |
  Vote: I like it 0 Vote: I do not like it
In problem D, i am facing a tough time in finding the sum of manhattan distances in O(max(n^2 ,m^2)). Can someone give me a hint? :)
  • 13 years ago, # ^ |
      Vote: I like it +4 Vote: I do not like it
    The sum of Manhattan distances over all cells: = = .

    Now we need to subtract the sum of Manhattan distances with occupied cells. Suppose (x, y) is one of occupied cells, then . Since there are at most max(n, m) occupied cells, we can just sum over all (x, y).

    Finally, the pairs of occupied cells were subtracted twice and we need to add them back. It's O(max(n2, m2)) as well.

    And don't forget that the order of cells matters, so for example the sums in the second paragraph above need to be multiplied by 2.