### 678A - Johny Likes Numbers

The problem was suggested by Abdrakhman Ismail Ismail_A.

We should find minimal *x*, so *x*·*k* > *n*. Easy to see that . To learn more about floor/ceil functions I reccomend the book of authors Graham, Knuth, Patashnik "Concrete Mathematics". There is a chapter there about that functions and their properties.

**С++ solution**

Complexity: *O*(1).

### 678B - The Same Calendar

The problem was suggested by Arthur Jaworski KingArthur.

Two calendars are same if and only if they have the same number of days and starts with the same day of a week. So we should simply iterate over years and maintain the day of a week of January, 1st (for example). Easy to see that the day of a week increases by one each year except of the leap years, when it increases by two.

**C++ solution**

Complexity: *O*(1) — easy to see that we will not iterate more than some small fixed constant times.

### 678C - Joty and Chocolate

The problem was suggested by Sheikh Monir skmonir.

Easy to see that we can paint with both colours only tiles with the numbers multiple of *lcm*(*a*, *b*). Obviously that tiles should be painted with more expensive colour. So the answer equals to .

**C++ solution**

Complexity: *O*(*log*(*max*(*a*, *b*))).

### 678D - Iterated Linear Function

The problem was suggested by Zi Song Yeoh zscoder.

The problem can be solved using closed formula: it's need to calculate the sum of geometric progression. The formula can be calculated using binary exponentiation.

I'll describe more complicated solution, but it's more general. If we have a set of variables and at each step all variables are recalculating from each other using linear function, we can use binary matrix exponentiation. There is only one variable *x* in our problem. The new variable *x*' is calculating using formula *A*·*x* + *B*. Consider the matrix *z* = [[*A*, *B*], [0, 1]] and the vector *v* = [0, 1]. Let's multiply *z* and *v*. Easy to see that we will get the vector *v*' = [*x*', 1]. So to make *n* iterations we should multiply *z* and *v* *n* times. We can do that using binary matrix exponentiation, because matrix multiplication is associative.

As an exercise try to write down the matrix for the Fibonacci numbers and calculate the *n*-th Fibonacci number in *O*(*logn*) time. The matrix and the vector is under the spoiler.

**The matrix and the vector for the Fibonacci numbers**

**C++ solution**

Complexity: *O*(*logn*).

### 678E - Another Sith Tournament

The problem was suggested and prepared by Alexey Dergunov dalex.

Let's solve the problem using dynamic programming. *z*_{mask, i} — the maximal probability of Ivans victory if the siths from the *mask* already fought and the *i*-th sith left alive. To calculate that DP we should iterate over the next sith (he will fight against the *i*-th sith): .

**C++ solution**

Time complexity: *O*(2^{n}*n*^{2}).

Memory complexity: *O*(2^{n}*n*).

### 678F - Lena and Queries

The problem was suggested by AmirMohammad Dehghan amd.

Let's interpret the problem geometrically: the pairs from the set are the lines and the problem to find to topmost intersection of the vertical line with the lines from the set.

Let's split the queries to blocks. Consider the lines added before the current block and that will not deleted in the current block. Let's build the lower envelope by that lines. Now to calculate the answer to the query we should get maximum over the lines from the envelope and the lines from the block before the current query that is not deleted yet. There are no more than lines from the block, so we can iterate over them. Let's find the answers from the envelope for all queries of the third type from the block at once: we should sort them and iterate over envelope using *two pointers technique*.

**C++ solution**

Complexity: .

Please write the editorial for E

About problem E:

After some interesting discussion, I was wondering what exactly can we say about the optimal solution?

Let's assume that optimal sequence can be represented by a sequence

a_{1},a_{2}, ...,a_{n}of fighters in whicha_{1}fightsa_{2}, then the winner fightsa_{3}, etc. Intuitively, the probability someone wins is higher the more to the right of the sequence it is: this impliesa_{n}should be Ivan in an optimal sequence.Now who do we want at the second to last position? That person will be given a greater chance to fight Ivan, so we want it to be the one that has the smallest chance to win against him.

Generalizing to all

nplayers, we have the followingO(n^{2}) greedy solution: construct the sequence of fighters from the last to the first. For each pick, pick the available fighter that makes probability of Ivan winning in current sequence highest possible.Code: 18489412

Of course, there's no such thing as "proof by AC", so I'm still curious if this solution is actually correct. If it is, I think it is quite interesting :)

Some history: the setting was taken from Timus 1218. In 1218, we discovered a NlogN solution and included that problem into our contest at Petrozavodsk camp, where 7 teams out of 50 solved it.

As my teammates just said, it became a tradition: all "Jedi Tournament" problems can be solved better than author initially thought. You could have done the same with your solution.

Actually I've discussed with dalex that. And I thought about the same thing. But both of us agreed that greedy solution is incorrect here.

If you discussed this before the contest, shouldn't you have included a test that fails the greedy solution?

I'm also curious to know how you reached this conclusion.

I thought about different greedy solution. This one passes random tests and must be correct.

Oh you misunderstood. We have not a countertest, we just thought that greedy solution is incorrect.

Another solution for problem F in

O(N*log.^{2}N)Could u please go for more detail about it?? I have difficulty in reading and understanding ur code :P

Explained.

Do you use the technique with storing the blocks of the sizes 2

^{k}?Problem to find the topmost intersection of the vertical line with the lines from the set can be solved with Convex Hull Trick.

I builded a segment tree of CHT over time interval.

For i

^{th}insertion query update the segment tree with this line in range [ i, time till i^{th}line exists ].For every query of 3rd type it is equivalent to point query at that point of time.

Nice. But I think it's more complicated.

During contest I tried with the same approach as mentioned in editorial (Submission). But it timed out because my complexity was O(N

^{1.5}* logN). So I had to go this way but it seems that logN factor can be removed.I solved the problem using your idea but with complexity O(N*logN). Submission here: 18842789

Insertion in logN nodes is log

^{2}N, right ?P.S: Your N*logN is slower than mine N*log

^{2}N.Actually if you insert the lines in sorted order the overall complexity for insertion is because you insert in nodes in total linear time. This was marX and I did (using your idea, btw) 18845711. You probably didn't look deeper into marX code.

Can anyone explain a little more about the recurrence relation in E?

http://codeforces.com/blog/entry/45405?#comment-310782

Can someone explain the editorials for Problem E in a somewhat detail manner, it would be a great help.

My approach. First observe that our jedi should fight last. Then we exclude him from the set of siths. Let

maskbe some subset of set of siths (without jedi) andiis a sith of this subset. Thendp[mask, i]is maximum (among all sequences of siths) conditional probability that Ivan wins tournament given that siths frommaskalready fought and i-th is a winner.We init our dp with full mask:

where

allis full mask. That means, if all siths already fought and i-th is a winner then probability that Ivan wins tournament is equal to probability of win this sith. Then we loop over smaller and smallermask's and calculating their values from previous calculations. To finddp[mask, i]use formula from editorial. Should be more clear now. If not I can explain in more detail.And the end answer will be maximum of

dp[mask_i, i]. It is conditional probability that Ivan wins tournament given that i-th sith is the first participant in tournament.Could you explain more about the formula. I want to ask some specific questions:

z(mask,i) is when the i-th wins so why do they + z(mask U j,j)*p[j][i] as well.

z(mask,i) is the "maximal probability of Ivans victory if the siths from the mask already fought and the i-th sith left alive". So is Ivans necessarily be i or some bit 1 in mask or some other siths?

This contest has been so long but if you could answer these, I would be really thankful. I have comtemplating about this problems for a week but still didn't figure it out. Thanks <3

Please ,explain me the solution of Another Sith tournament.Please explain me in words how that formula comes??

http://codeforces.com/blog/entry/45405?#comment-310782

In above code for problem F, I want to know why blen is 2500, not sqrt(n). Please tell me.. my submission also TLE when i set blen sqrt(n), but accept when i set blen 2500. sorry for my fool english

we need sorting all query in each block. so complexity is O(N * N / size + N * log2(size) + size * size). for best we choose size = 2500.

Can someone explain how is the complexity of solution of Problem E , in the editorial O(N^2 * 2^N) ? I think it is O(N^3 * 2^N) as for every O(n^2) starting pairs we have an O(n) loop to set the next winner.

Problem F 线段树分治+斜率优化可以做到O(NlogN)吧

Can anyone please explain the base cases in problem E

Could you explain more about the formula in problem E. I want to ask some specific questions:

z(mask,i) is when the i-th wins so why do they + z(mask U j,j)*p[j][i] as well.

z(mask,i) is the "maximal probability of Ivans victory if the siths from the mask already fought and the i-th sith left alive". So is Ivans necessarily be i or some bit 1 in mask or some other siths?

This contest has been so long but if you could answer these, I would be really thankful. I have comtemplating about this problems for a week but still didn't figure it out. Thanks <3

$$$z(mask, i)$$$ is not the probability that the $$$i$$$-th sith wins. Remember that we are calculating the probability of the $$$0$$$-th sith being the ultimate winner. It simply says that all siths in the mask have fought some match and the only sith alive out of those currently is the $$$i$$$-th sith.

Ivan corresponds to the $$$0$$$th bit. There are $$$2$$$ cases:

$$$1$$$. $$$0$$$-th bit is $$$0$$$, which means Ivan hasn't fought yet.

$$$2$$$. $$$0$$$-th bit is $$$1$$$. In this case, if $$$i \ne 0$$$, this means that Ivans fought but was killed. So, the answer for this state is $$$0$$$. Otherwise, Ivan is the survivor from all matches conducted so far and we proceed with the tournament.

Adding to roll_no_1's comment, I think it is easier to understand the recursion a little better when putting it side-by-side to the base case.

In this case the base case is $$$z(2^n-1,i)$$$ is:

notthe last person standing)Let's say for some $$$mask$$$ and $$$j$$$, $$$mask \cup j = 2^n-1$$$. Thus, in the last recursive call of the function $$$z(mask, i)$$$, only one of $$$z(mask \cup j,i)$$$ or $$$z(mask \cup j,j)$$$ is set to $$$1$$$ (the one which corresponds to Ivan defeats the previous winner). Therefore, the probability of

only Ivan winningis computed and propagated back to the initial recursive call.As mentioned in the tutorial, $$$z(mask,i)$$$ measures how likely it is that Ivan will win given contestants in $$$mask$$$ have fought and $$$i$$$ is the current winner. If $$$i$$$ wins in the next round, then $$$z(mask \cup j,i)*p[i][j]$$$ represents the probabilty and if $$$j$$$ wins in the next round $$$z(mask|j,j)*p[j][i]$$$. Either of the two can eventually lead to Ivan winning (it is only known when the recursive call reaches the base case), thus we need to sum up the probabilities.

678D - Iterated Linear Function How do we come from knowing that we have to find a matrix to the actual matrix? And what about the vector v, where that comes from?

Here is a simple mathematical approach:

After simplifying:

gn(x) = (a^n)*x + b*( 1 + a + a^2 + a^3 ....... a^(n-1) )

[ 1 + a + a^2 + a^3 ....... a^(n-1) ] forms a Geometric Progression, whose sum can be calculated as:

S = b* (a^n-1)/(a-1)

for the solution, see my submission 78952460