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

 » 10 years ago, # |   +3 Tower of Hanoi with a slight modification. Sweet.
•  » » 10 years ago, # ^ |   0 Problem C Div II looks like(Of course, statement is totally different) the 2nd Exercise problem(Warm Up Section) from the first chapter of Concrete Mathematics (By Knuth, Graham and Patashnik)In short that problem is: Find the shortest sequence of moves that transfers a tower of n disks from the left peg A to the right peg B, if direct moves between A and B are disallowed. This highlighted condition makes the difference with this problem to the classic Tower of Hanoi problem :-)
 » 10 years ago, # |   0 Why the number of operations is at most 200*n*m in problem D?
•  » » 10 years ago, # ^ |   +8 Least possible sum of all elements is (-100) * n * m, greatest possible sum is 100 * n * m. Each turn sum increases (at least by 2), so the number of operations is not greater than 100 * n * m.
 » 10 years ago, # |   0 I used treap as the segment tree's vertices in problem E.It can solve the problem in O(n(logn)^3) and pass the all tests.How stupid I am!
 » 10 years ago, # | ← Rev. 2 →   0 how to deduce the inequality in problem E div 2
•  » » 10 years ago, # ^ | ← Rev. 2 →   0 Obviously, is the maximal number which is divided by q among all numbers between l and r, inclusive. Also, is the maximal number which is divisible by q among all numbers between l and r. Moreover, each of the numbers between l and r inclusive are divisible by q. There are exactly such numbers.q may be the correct answer if and only if the amount of such numbers is not less than k.
 » 3 years ago, # |   0 For the problem Div2 E / Div1 C here's a nice proof for the GCD property. And the expression $(1)$ can also be written as $\left \lfloor \frac{r}{q} \right \rfloor- \left \lfloor \frac{l-1}{q} \right \rfloor \ge k$, which says that the multiples of $q$ within the range $[l, r]$ must be $\ge k$.
 » 14 months ago, # |   0 Effective approach solution gives wrong answer at test case in my code can any one debug my codeimport java.util.Scanner;public class EffectiveApproach { `public static void main(String[] args) { int sum1=0,sum2=0; Scanner sc = new Scanner(System.in); int n=sc.nextInt(); int a[] = new int[n+1]; for(int i=0;i
•  » » 10 months ago, # ^ |   0 Bro, have you got the bug in your code? I am also getting the wrong answer on test case 7. My solution import java.io.*; import java.lang.*; import java.util.*;public class Solution {public static void main (String[] args){ Scanner sc=new Scanner(System.in); HashMap hm=new HashMap<>(); int n=sc.nextInt(); int arr[]=new int[n]; for(int i=0;i