### Seyaua's blog

By Seyaua, 11 years ago, translation,

Here you can find the editorial for the past round. You can ask questions in the comments below. Special thank you goes to who prepared editorials for problems D and E, which I used here.

### Problem A

In this problem you should answer to 4 questions:

1)       Can we use type byte to store N?

2)       Can we use type short to store N?

3)       Can we use type int to store N?

4)       Can we use type long to store N?

We should check these conditions in the given order. If all these conditions are wrong, the answer is BigInteger.

The simplest way to check these conditions is to store numbers as strings and write a function to compare such strings. In Java you can use type BigInteger.

### Problem B

Try to check all possibilities for creation artificial rain and calculate how many sections contain water. The maximal answer from all these possibilities is the answer for the problem. To calculate the answer for the given position we should check how many sections are to the left and to the right of the given section receive water. The complexity of this algorithm is O(N^2).

### Problem C

Will be published later.

### Problem D

Consider N distinct prime numbers: p1, p2, …, pn.

Let A=p1*p2*…*pn. Then, easy to see, that the numbers A/p1, A/p2, …, A/pn can be considered as the answer.

The special case is when N=2. In this case there is no answer. We can see that this solution needs long arithmetic. If we choose first n prime numbers as p1, p2, …, pn then the maximal number in the answer for all N<=50 contains less than 100 digits.

Of course, there are other solutions. For example, if N=3 numbers 15, 10, 6 are the answer, and for N>3 numbers 15, 10, 6, 6*2, 6*3, …, 6*(N-2) are the answer.

### Problem E

First of all we divide our problem into 2 parts: consider stations from which we can start if we are moving in the clockwise direction and stations from which we can start if we are moving in the counterclockwise direction.

Obviously, if we know the solution of one of these problems, we know the solution of another problem.

So, we may assume that stations are located in the counterclockwise order and we are moving in the counterclockwise direction.

Consider the following differences:

D1=a1-b1,

D2=(a1+a2)-(b1+b2),

D3=(a1+a2+a3)-(b1+b2+b3),

Dn=(a1+a2+…+an)-(b1+b2+…+bn);

Obviously if one of Di’s is less than a zero, then we cannot drive one round along the road. Let D = min(Di) – we will use it later.

Obviously, if D<0 then the first station cannot be the start station.

Now, we can check with complexity O(n) whether the first station can be used as the starting point. Next, we want to show how we can check this for the second station with complexity O(1).
To show this, consider:

E1=D1-(a1-b1),

E2=D2-(a1-b1),

En=Dn-(a1-b1).

Next, substitute Di in these equalities. We get the following:

E1=(a1-b1)-(a1-b1)=0=(a2+a3+…+an+a1)-(b2+b3+…+bn+b1) – (a1+…+an=b1+…+bn=X)

E2=(a1+a2)-(b1+b2)-(a1-b1)=a2-b2

E3=(a1+a2+a3)-(b1+b2+b3)-(a1-b1)=(a2+a3)-(b2+b3)

En=(a1+a2+…+an)-(b1+b2+…+bn)-(a1-b1)=(a2+…+an)-(b2+…+bn)

But it’s easy to see that number E1 has the same meaning for the second station as number D1 for the first one. So, we just have to check min(Ei)>=0. But Ei=Di-(a1-b1), so we have to check min(Di-(a1-b1))>=0. Now, we can see that if min(Di)=Dk, then min(Di-(a1-b1))=Dk-(a1-b1). So, if we know Dk, that we can check whether the second station can be the starting point with complexity O(1). Similarly, we can check this for the third, the fourth, …, the nth stations.

Now we should check the same things but assuming that the car is moving in the clockwise direction.

• +57

 11 years ago, # |   0 Elegant solutions!In particular, problem E!!
 11 years ago, # |   0 problem E!
 10 years ago, # |   0 Will problem C be published?
 10 years ago, # |   0 E is an artwork.
 » 5 years ago, # |   0 What is test case 14 for problem C? I tried to simulate the file system yet it ended up in a RE.http://codeforces.com/contest/66/submission/19224146
 » 5 years ago, # |   +3 well...I don't need it myself but, problem C's Editorial has been delayed for 6 years...
•  » » 5 months ago, # ^ |   -8 Update : Delayed for 10 years ...
 » 4 years ago, # | ← Rev. 2 →   0 For whoever is interested, here is an outstanding and creative approach taught to me by IOI silver medalist, ICPC world Finalist and my teacher razimantv to problem D. It does not require big integers and teaches an important fact about binary numbers. First, let us consider all the numbers (from 1 to N) in binary.For each bit position, associate two primes — P[i], Q[i]. Multiply all numbers which have the i-th bit set by P[i]. Multiply all numbers which have the i-th bit unset by Q[i]. Now, all pairs of numbers which have a common bit set have a common factor. We only have to deal with those numbers which don't have any common bit (i.e. ... Those numbers who's XOR gives a string of all 1s).We give all pairs of numbers (i, X) a common prime factor if i^X gives a string of all 1s.Now, we have ensured that any two numbers have a common prime factor and no prime divides all numbers, We are done !Here is code for this approach and here is some explanation.(Here's the same code on GitHub)
 » 3 years ago, # |   0 In case, anybody requires more explanation for Problem D, I have written a post about it here.
 » 2 years ago, # |   0 My solution for D is like this: First element will always be $2\times3$ and fill $n/2$ elements with $2$ while fill remaining elements with $3$. It will ensure that $n/2$ and $n-n/2$ groups have pairwise $gcd>1$ inside their groups. This also means that $gcd$ of all elements is $1$. Now for every element from $2$ to $n$ multiply it by 5. This ensures that each pair has $gcd>1$. Now for elements to be distinct, multiply elements with unique prime numbers. Note: It will not work for $n=3$.Solution link
 » 2 years ago, # |   0 problem B can be solved in O(n)
 » 17 months ago, # |   0 A O(n) time O(n) space solution for problem B. https://codeforces.com/contest/66/submission/76025642
•  » » 12 months ago, # ^ |   0 please tell me your approach
 » 9 months ago, # | ← Rev. 2 →   0 Problem B in O(n) complexity: 100618390Logic: Count if a[i]<=a[i+1] and save this count for every position. Do the same from backward. Then find the max of all.
 » 6 weeks ago, # | ← Rev. 2 →   0 For anyone who is wondering , The problem B can be solved in a more optimal way in O(N) by using Double Prefixes , rather than brute forcing in O(N^2) that the Editorial suggests and most beginners would have thought at first , though the constraints of the problem allows it , here is my better approach (O(N)) - https://codeforces.com/contest/66/submission/124894358
 » 5 weeks ago, # |   0 Can anyone suggest how to deal with the overflow in C++? Long long is not sufficient for storing the answer and trying to perform multiplication with strings is very complex.
{"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 1, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0}