### 710A - King Moves

Easy to see that there are only three cases in this problem. If the king is in the corner of the board the answer is 3. If the king is on the border of the board but not in a corner then the answer is 5. Otherwise the answer is 8.

**С++ solution**

Complexity: *O*(1).

### 710B - Optimal Point on a Line

The function of the total distance is monotonic between any pair of adjacent points from the input, so the answer is always in some of the given points. We can use that observation to solve the problem by calculating the total distance for each point from the input and finding the optimal point.

The other solution uses the observation that the answer is always is the middle point (by index) in the sorted list of the given points. The last fact is also can be easily proven.

**C++ solution**

Complexity: *O*(*nlogn*).

### 710C - Magic Odd Square

The problem was suggested by Resul Hangeldiyev PieceOfCake.

The solution can be got from the second sample testcase. Easy to see that if we place all odd numbers in the center in form of rhombus we will get a magic square.

**C++ solution**

Complexity: *O*(*n* ^{2}).

### 710D - Two Arithmetic Progressions

I wanted to give this problem a lot of time ago. I thought it is very standard problem, but I underestimated its difficulty.

Let's write down the equation describing the problem: *a* _{1} *k* + *b* _{1} = *a* _{2} *l* + *b* _{2} → *a* _{1} *k* - *a* _{2} *l* = *b* _{2} - *b* _{1}. So we have linear Diofant equation with two variables: *Ax* + *By* = *C*, *A* = *a* _{1}, *B* = - *a* _{2}, *C* = *b* _{2} - *b* _{1}. The solution has the form: , where the last equation can be solved by extended Euclid algorithm and *p* is any integral number. The variable *p* should satisfy two conditions: and . The values *A* and *B* are fixed, so we can get the segment of possible values for the values *p*. The length of the segment is the answer for the problem.

**C++ solution**

Complexity: *O*(*log*(*max*(*a* _{1}, *a* _{2}))).

### 710E - Generate a String

The problem was suggested by Zi Song Yeoh zscoder.

This problem has a simple solution described by participants in the comments.

My solution is a little harder. Let's solve it using dynamic programming. Let *z* _{ n} be the smallest amount of time needed to get *n* letters 'a'. Let's consider transitions: the transition for adding one letter 'a' can be simply done. Let's process transitions for multiplying by two and subtraction by one simultaneously: let's decrease the number 2·*i* *i* times by one right after getting it. Easy to see that such updates never include each other, so we can store them in queue by adding the new update at the tail of the queue and taking the best update from the head.

The solution is hard to describe, but it is very simple in the code, so please check it to understand the idea :-)

**C++ solution**

Complexity: *O*(*n*).

### 710F - String Set Queries

The problem was suggested by Alexandr Kulkov adamant.

Let's get rid of the queries for deleting a string. There are no strings that will be added two times, so we can calculate the answer for the added (but not deleted strings) and for the deleted separately and subtract the second from the first to get the answer. So we can consider that there are no queries of deletion.

Now let's use Aho-Corasik algorithm. The only difficulty is that the strings are adding in online mode, but Aho-Corasik algorithm works only after adding all the strings. Note that the answer for the given set of strings equal to the answer for any part of the set plus the answer for the remaining part. Let's use the trick with converting the static data structure (Aho-Corasik in this case) to the dynamic one.

For the set of *n* strings let's maintain a set of no more than *logn* sets of the strings with sizes of different powers of two. After adding new string we should move the sets from the lowest powers of two to the largest until we got an invariant set of sets.

Easy to see that each string will be moved no more than *logm* times, so we can process each query in *O*(*logn*) time.

**C++ solution**

Complexity: *O*((*slen* + *m*)*logm*), where *slen* is the total length of the string from the input.

In problem B , How do we prove that "the answer is always in one of the given points" .

Sort the array, and just find the median.

Why we can forget about them ?

Because, they're part in the answer is length of the interval, and we can just add this interval in final sum, and then forget

I just want to detail TaTaPiH's answer a bit more, but he has 100% of the credits (thanks btw).

Let the two points be x1 and x2. If you take a point y between x1 and x2 (x1 <= y <= x2) the sum of distances to get to y is (y — x1) + (x2 — y) = x2 — x1, which means that independent of the point y you take, the total distance will be the same (the length of the segment).

Let's sort array x which has the n points, 1-indexed. For example, suppose n = 5. We want to minimize the total sum of distance to some selected point y.

Consider points x[1] and x[5]. These 2 points alone will always sum (x[5] — x[1]) to the final sum of distances if you choose some (any) point y between them. (See case n = 2 for explanations). So let's sum that to the final sum and move on.

Now we're left with points x[2], x[3], x[4]. For points x[2] and x[4] you should also take a point y between them, because otherwise a point y outside points x[2] and x[4] would sum something greater then the length of that interval to the final answer. Note that a point between x[2] and x[4] is a point between x[1] and x[5] so you can continue this logic without changing the answer for the previous points. Now only one point (x[3]) is left, see case n = 1.

So the pattern is: you fix 2 extremes points (one to the left and one to the right), now you'll now that an optimal answer must choose a y between them, and move on to the next points, until you're left with 1 or no points.

Assume that we are moving a point from left to right to get the optimal answer, consider the following scenario. (X denotes a point, # denotes the current chosen point)

(XXX m points XXX) # (XXX n points XXX)

Whenever you move to the right, the resulted total distance changes by n-m since you are moving closer to n points and moving away from m points. At one point, when n becomes smaller equal than m, you need not to continue go right for the answer since we only need the leftmost point as the answer. Hence, the answer is always one of the given points.

Where is english version?!

Google translate

Does'nt seem to be working well

English version please? :(

This round is important and must have an English Editorial.

Codeforces is a good international site.

Please don't limit us by only releasing in Russian. I kindly request you to release an English version.

A lot of people are making a great effort to learn english to understand and take place in the community. Are U serious with this EDITORIAL in russian? I hope an english version release of this editorial.

In particular I am very interested in the solution for problem F.

Sorry for the delay with English editorial. I'll write it as soon as I can.

TNX ;))

Now,Thanks!

Hi, Can somebody perhaps explain the motivation behind the solution for problem C? After reading the code in this tutorial I can verify that it works, but still I cannot imagine how one comes up with this solution.

Thank you!

^resolved now with the English tutorial

Longing For Solution in English...

Can someone please explain to me the logic behind problem E. I have used a 1-D array to store the least time to generate i no. of a's.

Let dp[i] be the optimal cost to produce string of length i. We have two cases:

If i is even, then the optimal cost for dp[i] is the minimum of dp[i-1] + x (add new char) or dp[i/2] + y (copy from half the length)

If i is odd, then the optimal cost for dp[i] is the minimum of dp[i-1] + x (add new char) or dp[(i+1)/2] + x + y (copy then remove). For example, you can get string of length 15 either by adding from length 14, or copy from length 8 then removing 1 character

(Note: I don't know if this is the logic used in the editorial code)

I have previously implemented the same.But it is showing incorrect. This approach fails in the following case.

Suppose we want to produce the string of length 30 then according to above approach dp[15]=min(dp[8]+x+y,dp[14]+x)

dp[30]=min(dp[15]+y,dp[29]+x)

you can see that we are having an extra x seconds(which is the same time spent in removing an extra 'a' generated from doubling 8 a's) while calculating dp[30]. Now if we want to produce a string of higher length then there would be more and more extra x seconds while calculating the cost.Hence this approach won't work.

First of all, how do you know that dp[15] must be dp[8] + x + y? It could also be dp[14] + x, depending on the x y values.

Secondly, I don't quite get what you mean by "more and more extra x seconds", but looking at your submissions, I guess your mistake is implementing the dp when i is odd as min(x + v[i/2] + y, v[i-1] + x). It should be v[(i+1)/2]

I got my mistake.Thank you so much.

I don't get something

Lets say i is even, then you that dp[i] is the minimum of dp[i-1] + x or dp[i/2] + y, but why can't it be dp[i+1]+x, like if we can get to 31 faster then to 29, and we need to get to 30. Same question if i is odd.

EDIT: Nvm figured it out

Can you please explain why we aren't considering dp[i+1]+x, I am having the same doubt?

can u please explain dp recurrence a little?

I solved problem 710E - Generate a String using Dijkstra, but it got AC with 1918ms :D almost TLE

could anyone explain the other solution using Dynamic programming?! i did not understand what is written in the editorials!!!

Hi, my interpretation of the solution was that instead of using Dijkstra, they managed to use a slightly-different graph that was a DAG, so finding shortest paths is O(N).

You can note that between doubling operations (after the first one, of course), there is at most one + or — operation, because otherwise you could be more efficient by moving them before the previous doubling operation. (for example, the sequence +*++ -> 4 could be done as ++*). If you take a graph with only these edges, it becomes a DAG, and dijkstra is not necessary.

EDIT: Here is incredibly simple solution with this idea http://codeforces.com/contest/710/submission/20469970

If you still interested)) DP solution is far easier than Dijkstra.(I'm specialist but I found out it) Here is my solution http://codeforces.com/contest/710/submission/25821459. If you don't understand how to get the formula, try to think from what values we can get an answer. And don't forget that we can delete chars and it costs x./////Sorry if my English is poor)

Can I see your code please? my code works with Dijkstra but I got TLE I wanna see if I can do any thing about it!

Did anyone solve problem D?

I know how to derive

x_{0},y_{0}, whereAx_{0}+By_{0}=Cby using Extended Euclidean Algorithm, but I don't know proceed from here to . Also, it seems to me that . Am I misunderstanding something? If not, can somebody perhaps elaborate more on this step? Thank you!.

Also obviously , so the first equation is also true.

The only difficulty thing is to prove that all solutions can be described in the given form. It is a standard fact and I think you can easily find the prove in the internet.

Thank you for the reply!

I know how to prove that all solutions can be described in this given form now, but still I cannot understand three lines in your program, which relates to the lower bound

Land upper boundRafter staring at it for a long time.li L = _ceil(r * g — x0 * C, B);

li R = _floor(l * g — x0 * C, B);

R = min(R, _floor(y0 * C, A));

Can you give me some more explanations regarding these three lines, or direct me to a link? Thank you!

I found that no one can solve Problem F by Java 8 because of TLE in test case #19 (demands flush*300000) though problem statement refers to Java's flush.

Did writers&testers check whether it can be solved by Java 8?

+1. I ran into the same trouble here.

I wonder why F a O(N*sqrt(N)) gets a TLE. Here is my code 20350890 Anyone can help? UPD: Trouble solved.

Hello,

Here is my submission for problem B.

I am sure that my idea is correct but maybe there is a tiny bug in the implementation. Any help?

What is the "floor (y0*C, A)" in the end of problem D's code for? Why is it here ? How did we get to "y0*C/A" ?

Hi, Can someone explain the motivation behind the solution for problem C? After reading the code in this tutorial I can verify that it works, but still, I cannot imagine how one comes up with this solution.

Thank you!

Proof of Problem E , DP — Solution