Seyaua's blog

By Seyaua, 14 years ago, translation, In English

Here you can find the editorial for the past round. You can ask questions in the comments below. Special thank you goes to sdya 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.

  • Vote: I like it
  • +57
  • Vote: I do not like it

| Write comment?
14 years ago, # |
  Vote: I like it 0 Vote: I do not like it
Elegant solutions!
In particular, problem E!!
14 years ago, # |
  Vote: I like it 0 Vote: I do not like it
problem E!
14 years ago, # |
  Vote: I like it 0 Vote: I do not like it
Will problem C be published?
»
8 years ago, # |
  Vote: I like it 0 Vote: I do not like it

What is test case 14 for problem C? I tried to simulate the file system yet it ended up in a RE.

http://codeforces.net/contest/66/submission/19224146

»
8 years ago, # |
  Vote: I like it +3 Vote: I do not like it

well...I don't need it myself but, problem C's Editorial has been delayed for 6 years...

»
7 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

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)

»
7 years ago, # |
  Vote: I like it 0 Vote: I do not like it

In case, anybody requires more explanation for Problem D, I have written a post about it here.

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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.