### Malinovsky239's blog

By Malinovsky239, 10 years ago, translation,

### A div2. Where do I Turn?

Let's consider cross product of vectors and , which is equal to . Sign of cross product defines sign of a sine of oriented angle between vectors (because cross product is also equal to ), and that sign leads us to the correct answer.

If cross product is equal to zero, then A, B and C lay on the same straight line. So the answer is <>.

If cross product is more than zero, then answer is <>.

And, at last, if it's less than zero, the answer is <>.

Also you should notice that the value of cross product doesn't fit 32-bit type, so you have to use 64-bit type in order to avoid integer overflow.

Implementation

### B div2. Effective Approach

Let's assume that number t is on the indtth position in the original permutation. Then, obviously, during iterating from left to right this number will be found in indt comparisons, and during iterating from right to left — in n - indt + 1 comparisons. Let's declare additional array, in ith element of each there will be such number j, that aj = i. This array allows to process each query in O(1) using formulas referred above. Additional array is built in O(n) during iterating array a. So, the final complexity is O(n + m).

Implementation

### C div2 — A div1. Flying Saucer Segments.

Let Fn be the answer for the task, where n is equal to the amount of aliens. Let's assume, that we've solved problem for n - 1 aliens, i.e. we know the value of Fn - 1. Let's try to find value of Fn. Notice, that the most junior alien in rank will be able to leave the 3rd section, if and only if all other aliens are in the 1st section. So, now we know first Fn - 1 actions. Then the most junior alien may go to the 2nd section. To make for him entrance to the 1st section possible, it's necessary for all other aliens to return to the first one. So, Fn - 1 more actions are necessary. At last, after the most junior alien will go to the 1st section, Fn - 1 more actions are required for n - 1 other aliens to return to the 1st section from the 3rd. So, Fn = Fn - 1 + 1 + Fn - 1 + 1 + Fn - 1. It allows to count Fn using matrix exponentiation in O(log n), but we'll improve current solution. Let's add 1 to both parts of the equality and after elementary operations we'll have Fn = 3·(Fn - 1 + 1) - 1. Now it's easy to solve this reccurence: Fn = 3n - 1.

To count Fn quickly you should use binary power method. Solution's complexity — O(log n).

Don't forget that if 3n mod m = 0, answer is equal to m - 1, but not  - 1.

And, in conclusion, notice that the task is equal to Hanoi Towers problem with a slight modification (it's impossible to move disks between one pair of rods).

Implementation

### D div2 — B div1. Naughty Stone Piles

Complexity — O(n · log n  +  q · log n).

Implementation

### E div2 — C div1. Anniversary

At first, let's prove the statement: GCD(Fn, Fm) = FGCD(n, m).

Let's express Fn + k using Fn and Fk. We'll get the formula: Fn + k = Fk·Fn + 1 + Fk - 1·Fn, which is easy to prove by induction.

Then use the derived formula and notice, that GCD(Fn + k, Fn) = GCD(Fk, Fn).

Now you are to notice an analogy with Euclidean algorithm and to understand, that we've got necessary equality for GCD of two Fibonacci numbers.

So, our current task is to find in the given set subset of k (or at least of k) elements with maximal possible GCD. To be exactly, to find this GCD.

Let the answer be equal to q. Then  -  ⌉ + 1 ≥ k (1) must be true.

Notice, that for each summand from left part of inequality O( ) segments exist, in which its value is constant. Moreover, we can find all these segments and values in . To be more precise, we are intersted in such q, that in the point q - 1 value of at least one summand changes (obviously, increases). There are also such values. Go over all of them and try to use each of them as the answer (i.e., check inequality (1) for each of them), and choose maximum from all satisfying numbers. The answer always exists, as q = 1 is true for any input.

So, we've found index of required Fibonacci number. The number itself can be calculated by matrix exponentiation.

Implementation

Complexity — .

### D div1. The table.

Let's get the required table. Act in the following way: find any row or column with negative sum and invert it. Notice, that sum of numbers in the entire table will always increase (at least, by 2). It can't increase permanently, because its maximal possible summary change is 200·n·m. So we'll get the required table anyway. It takes us not more than 100·n·m operations (applying of the spell), each of those is performed in O(n) or O(m). So, we've learned how to get required table in not more than ~ 1004 operations.

Now let's restore the answer. It's easy to understand that it will contain those rows and columns, which we've inverted odd times.

Implementation

### E div1. Noble Knight's Path

#### Solution 1

It's easy to guess that castles form a tree. Let's build heavy-light decomposition over it. Moreover, let's build persistent segment tree (with sum as the function) over each path. Tree's vertex will contain 0, if castle wasn't attacked by barbarians, and 1 otherwise.

Each knight's path should be divided into not more than two subpaths each of them lays on the path from one of the route's end to tree's root (just use lca in order to do it). Now let's solve the problem for each of the subpaths separately. We should sequentially process paths from heavy-light decomposition and single vertices, which lay on subpath. We are going to count the amount of vertices, which was not visited since year y + 1 up to the current year, i.e. (in the case of a path of the decomposition) such vertices, that the difference between values in the current version of persistent segment tree and in the version corresponding to year y (use binary search to find required version in the list of versions) is equal to zero (in case with single vertice it's enough to remember time when vertice was visited). As soon as the amount of appropriate vertices become not less than k, we should simultaneously walk down in two tree's versions in order to get the answer.

If the kth vertex isn't found on the first subpath, you should pay attention on the fact, that as we always go from down to up, we should accurately recalculate required vertex's number, in order to know it's position in the second subpath from down to up.

Complexity: O(m·log2 n) — in each query of the first type it can be necessary to update some segment tree, this action takes O(log n) operations; in each query of the second type there are O(log n) decomposition's paths, each of them is processed in O(log n) (firstly, binary search through versions' list, then query to the tree/walking down).

Implementation

#### Solution 2

Let's go round the tree using dfs. When we enter the vertex and when we leave it let's put down vertex's number in the additional array (you can find out that this list has something same with regular brackets sequence). Assign 0 as the second number to all elements of the array. Then build persistent segment tree on the described array.

Now, when the first event happens, we'll assign  + 1 to the second number in position of the first occurence of a castle's number, and  - 1 to a position of the last one.

In order to answer for the second query, just notice, that we should count sum of the second numbers assigned to positions between first occurences of the vertices a and b in the array described above for finding the amount of visited vertices in the path connecting them.

Now on each of the paths separately start binary search for an answer — position of required castle. For the answer's check use the idea from the previous paragraph.

Complexity: O(m·log2 n) — in each query of the first type it can be necessary to update some segment tree, this action takes O(log n) operations; in each query of the second type there are O(log n) iterations of binary search on answer, each of iterations takes O(log n) operations.

Implementation

#### Questions?

• +57

By Malinovsky239, 10 years ago, translation,

Hello everyone!

Today, at 19:30 MSK Codeforces Round #140 will take place. The competition will be held in both divisions.

I'am the author of today's competition. My name is Ilya Malinovsky, I'm student of Saint-Petersburg State University. This is my first Codeforces round.

I want to express my gratitude to Gerald Agapov (Gerald) for his help during the work on the round, Maria Belova (Delinur) for statements' translation from Russian into English, Mikhail Mirzayanov (MikeMirzayanov) for the possibility to prerare contests (and, of course, to compete in them) here, on Codeforces.

As usual, in most of the problems participants will help different characters to cope with their troubles and misfortunes. You will have only two hours to help epical hero, an alien, noble knight and other characters. Also, insidious stone heaps will resist you in one of the problems...

I hope, most of participants will enjoy the tasks.

Good luck!

P.S. Information about score distribution will appear not long before the round starts.

upd. Score distribution is stadart (500-1000-1500-2000-2500) in both divisions.

upd 2 Round is over!

Congratulations for winners!

#### div1:

Winners have from 3 to 4 solved problems. Noone managed to solve E.

#### div2:

1. Velicue (he is the only div2-participant, who got 5 "OK"s)

2. Frommi

3. Aerolight

4. Shahriar_sust

5. bzdbz

Thanks to everyone for participating! I hope, you've enjoeyd the round.

Full editorial.

• +235