witua's blog

By witua, 11 years ago, translation, In English

Hi!

289A - Polo the Penguin and Segments

Solution. First of all, we need to count, how many integers are inside given segments at the beginning. Since they don't intersect and even touch, no integer point can belong to more than one segment at the same time. This mans that starting value of segments is .

If k divides p, then answer is 0 — we don't need to do anything, it's already done. But if it's not true, we need to know the minimal number of turns to make k divisor of p. Since we can in single turn increase p by 1 (by decreasing the left point of the leftmost segment), this number is equal to .

Note. Note that just output is not enough: you need to pay attention for case when , or just output .

289B - Polo the Penguin and Matrix

Solution. First of all, we need to know when the answer is -1. For that you should notice that after any operation on number z, value doesn't change. Indeed, . This means that there is not answer if there are two different points for which is diffrent.

Now we can transform our problem a bit. We can just write down all integers from matrix n × m to one array b of size k = n × m and sort them all in non-decreasing order. It is not hard to notice that in some of the optimal solutions, all number are at the end equal to one of the number for starting array. But also, it is optimal to make all number equal to (median element). Why to median? Suppose that we make all numbers equal to non-median element with index x. Then if |x - (k - x)| > 1 (i. e. from one side there are more elements than from another + 1). So, by moving out element more to median, we can make result better.

After we know, to which number we should bring all, the answer is just , divided by d.

Note. There is also full solution with complexity O(n2m2).

288A - Polo the Penguin and Strings

Solution. To solve this problem we need to find out some contruction of resulting string. But first of all, we need find out when there is no result. Obviously, if k > n, there is not result — you cannot build string of length n with more than n characters. Another one case is when k = 1 and n > 1 — there is no answer in that case also.

Consider that k = 2. It's really easy to see that answer for such case is a string of form abababab.... To construct string for k > 2 you need to add some extra characters — c, d, e.... To make string lexicographically smallest, you need to add that characters as close to the end as we can. And the best bet here is abbabab...abacdefgh.... So, we need just to add characters c, d, e, f... (i. e. k - 2 characters from c) to the end of the string.

288B - Polo the Penguin and Houses

Solution. Since k ≤ 8 you can solve this problem using brute force. This means that you can recursively construct all possible kk possibilities of first k assignments. (For k = 8 this is equal to 16 777 216.) For each of that assignments you need to check whether it is correct or not (by problem statement). Ths can be simply done using loops.

When you know the number of assignment for the first k tables (let it be f(k)), all you need to do is to count the number of assignment for the rest n - k plaques. Since there should bo no path to 1, there should be no path to any of first k houses, so at each plaque for houses from k + 1 to n there can be any number from k + 1 to n, inclusive. There are (n - k)n - k such possibilities. And hence the total answer is f(k)(n - k)n - k.

Note. There also exists solution with dynamic programming, and also there exists formula for f(x). You can read about it more here, here и here.

288C - Polo the Penguin and XOR operation

Solution. Since we need to maximize the result, we need to find such permutation, for which the least number of bit disappear. (We consider bit disappeared if it was 1 both in i and pi, so in it is 0). It turns out that for each n there is such permutation that no bit disappear. How to build it? We will be solving problem by iterations while n > 0. On each iteration, we need to find the biggest (the leftmost in binary representation) bit which is not 0 in binary representation of n and denote it position (bits are numbered from 0) by b. Now we need to find integer m — minimal integer from 0 to n, inclusive, such that b-th bit is also 1 in it. After that you can see (look image below), that at no bit disappear, at no bit disappear, ..., at no bit disappear. So, it is good to assign exactly that integers to our permutation, i. e. pm = m - 1 and pm - 1 = m, pm + 1 = m - 2 and pm - 2 = m + 1 and so on. After that assign value m - (n - m + 1) - 1 to n and go to next iteration.

Now when we know how to build permutation and that no bit disappear, the value of the answer is equal to .

288D - Polo the Penguin and Trees

Solution. As always in such problems, root our tree at some vertex, for example vertex with number 1. We need to find out, what will happen when we have already chosen one path. Obviously, after deleting all vertices and their edges from that path, tree will disintegrate in some set of trees. Denote their sizes by c1, c2, ..., ck, where k is the number of trees. Then the number of ways to choose the second path is equal to . This gives us O(n2) solution — just to brute force all pathes and count the number of second paths by this formula. We need to do it in O(n). To do so, dfs our graph and fix some vertex during dfs, we will consider this vertex as the last vertex in the first path. Now we need to find the sum of above formula for the rest of the vertex. Here you can separately solve this problem for all vertex inside subtree of current vertex and for the rest of the vertices. For subtree vertices, you can, after finding the answers for all vertices of subtree, find the answer for root of subtree. To do so, you need to iterate all edges from current vertex and sum up results for that vetices. Also you need to add the sum of values multiplied by the number of vertices in subtree, where di are all sizes of subtrees of vertices from current vertex, not including from current edge). You can use some partial sums of something like that to make it linear. For the rest of the vertices (not in subtree) it is actually similar, but a bit harder. Here you need to keep current result as a parameter of dfs and when you entering some vertex you should add some additional counts to the current sum (similarly as in first case).

Note. Also, you can find the number of bad pairs of pathes and subtract it from the total number. Also some divide and coquer solution exists, you can think about it.

288E - Polo the Penguin and Lucky Numbers

Solution. In this problem there are a lot of different formulas, most of them are for optimizing solution and making it lenear. Editorial shows just a general idea because it's pretty hard to explain all of them and good for you to derive it by yourself. If you have any questions — write them all in comments.

Denote by a1, a2, ..., an all lucky number from segment. First of all, we need to do reduce the problem a bit. Let we have some fixed digit (pos, d), i. e. position of this digit is pos (from 0 from right to left) and value is d (4 or 7). Then, for all ai (1 ≤ i < n) such that pos-th digit of ai is equal to d, we need to add ai + 1 × d × 10pos to the answer. Now we can see that problem can be reduced to the following. For each fixed digit (pos, d) find the sum of all ai such that ai + 1 on the pos-th position has digit d. Obviously, we can solve the problem for 1..l and 1..r separately and then subtract the first from the second — that will be the answer.

How to find such sum among all lucky numbers of some length but less than some lucky number x? We will describe the general idea. Any lucky number, less than x has some common prefix with x, then one digit is less than the corresponing in x (i. e. it is 7 in x and 4 in another integer) and the rest of the digits are arbitrary. So, by iterating all such positions where is the first digit less than in x, we can, using the fact that the rest of the digits are arbitrary and some formulas and precomputations, compute the results for each position and digit.

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

| Write comment?
»
11 years ago, # |
  Vote: I like it 0 Vote: I do not like it

I think 288B - Polo the Penguin and Houses could also solved in O(nm) by what Xellos said here.

Am I wrong ?

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

Who can translate it into English? Orz

  • »
    »
    11 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I think google translate does it well , at least it helps me :)

    But I admire that the original translation would be more helpful :)

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

In the problem "Polo The Penguin and Matrix", the following test case is missing: 1 7 4 1 5 9 13 16 17 18 Some of the accepted codes are giving wrong answer on the above test case.

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

Problem B. Polo the Penguin and Matrix can be easily solved using ternary search. Just make a vector.All elements of vector are multiple of d in increasing order.Then just run a ternary search on vector and check it. my submission: http://codeforces.com/contest/289/submission/14401995

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

In 288B - Polo the Penguin and Houses , my O(log(n)) solution using the formula ans = kk - 1 * (n - k)n - k gave correct answers to all system tests.

Since I was thinking of that formula intuitively, can anyone give me an idea of how to mathematically prove it?

UPD: Nevermind, I've just looked deeper and found Swistakk's comment on the announcement.

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

Can someone explain Div1 E?

»
3 years ago, # |
Rev. 3   Vote: I like it +14 Vote: I do not like it

Another solution of Div.1 E(?)

Let $$$calc(x)$$$ be the answer of $$$[44444...44,x]$$$. Then $$$ans=calc(r)-calc(l)$$$

It's not hard to find that if we want to change $$$a_i$$$ into $$$a_{i+1}$$$, we need to change the first "4" from the back to the front into "7" (let's suppose it's the $$$i$$$-th digit), and $$$i+1$$$-th, $$$i+2$$$-th, ..., $$$n$$$-th digit into "4". For example, $$$47447747777 \to 47447774444$$$.

Enumerate the number of trailing "7"s of $$$a_i$$$, say, $$$cnt$$$. $$$a_i$$$ can be expressed as "$$$xxx...xx4777...77$$$", $$$a_{i+1}$$$ can be expressed as "$$$xxx...xx7444...44$$$". If we replace "xxx..xx" with a $$$n-cnt-1$$$-digit number $$$X$$$, and we call the number $$$777...77$$$ (there're $$$k$$$ 7s in it) $$$seven_k$$$, $$$444...44$$$ $$$four_k$$$, then $$$a_i=X\times 10^{cnt+1}+4\times 10^{cnt}+seven_k$$$, $$$a_{i+1}=X\times 10^{cnt+1}+7\times 10^{cnt}+four_k$$$.

$$$a_i a_{i+1}=(X \times 10^{cnt+1}+4\times 10^{cnt}+seven_k)(X \times 10^{cnt+1}+7 \times 10^{cnt}+seven_k)=X^2 \times 10^{2cnt+2}+7X \times 10^{2cnt+1}+X \times 10^{cnt+1} \times four_k+4X \times 10^{2cnt+1}+28 \times 10^{2cnt}+4 \times 10^{cnt} \times four_k+X \times 10^{cnt+1} \times seven_k+7 \times 10^{cnt} \times seven_k+seven_k \times four_k$$$

Let $$$S_i$$$ be all the $$$i$$$-digit lucky numbers less or equal than the first $$$i$$$ digits of $$$x$$$. $$$f_{i,0}=|S|,f_{i,1}=\sum_{i \in S}i,f_{i,2}=\sum{i \in S}i^2$$$. Replace $$$X,X^2$$$ with $$$f_{i,0},f_{i,1},f_{i,2}$$$ and calculate the fomula above. $$$f_{i,0},f_{i,1},f_{i,2}$$$ can be obtained by digit dp.

AC code: 95351065

»
8 months ago, # |
Rev. 2   Vote: I like it -10 Vote: I do not like it

[delete]