## 483A - Counterexample

Problem author gridnevvvit

This problem has two possible solutions:

- Let's handle all possible triples and check every of them for being a counterexample. This solution works with asymptotics
*O*(*n*^{3}*logA*) - Handle only a few cases. It could be done like this:

if (l % 2 != 0)
l++;
if (l + 2 > r)
out.println(-1);
else
out.println(l + " " + (l + 1) + " " + (l + 2));

Jury's solution: 8394832

## 483B - Friends and Presents

Problem author gridnevvvit

Jury's solution is using binary search. First, you can notice that if you can make presents with numbers 1, 2, ..., *v* then you can make presents with numbers 1, 2, ..., *v*, *v* + 1 too. Let *f*(*v*) be the function returning true or false: is it right, that you can make presents with numbers 1, 2, ..., *v*. Let *f* _{1} be the number of numbers divisible by *x*, *f* _{2} — the number of numbers divisible by *y*, and *both* — number of numbers divisible by *x* and by *y* (as soon as *x* and *y* are primes, it is equivalent to divisibility by *x*·*y*). Then to first friend at first we shold give *f* _{2} - *both* numbers, and to second friend *f* _{1} - *both* numbers. Then we must check, could we give all other numbers divisible neither by *x* nor by *y*.

This solution works with

Jury's solution: 8394846

## 483C - Diverse Permutation / 482A - Diverse Permutation

Problem author gridnevvvit

Let's see, what's the solution for some *k* = *n* - 1:

1 10 2 9 3 8 4 7 5 6

At the odd indexes we placed increasing sequence 1, 2, 3 .., at the even — decreasing sequence *n*, *n* - 1, *n* - 2, ... First, we must get the permutation the way described above, then get first *k* numbers from it, and then we should make all other distances be equal to 1.

This solution works with *O*(*n*).

Jury's solution: 8394876

## 483D - Interesting Array / 482B - Interesting Array

Problem author gridnevvvit

We will solve the task for every distinct bit. Now we must handle new constraint: *l*[*i*], *r*[*i*], *q*[*i*]. If number *q*[*i*] has 1 in bit with number *pos*, then all numbers in segment [*l*[*i*], *r*[*i*]] will have 1 in that bit too. To do that, we can use a standard idea of adding on a segment.

Let's do two adding operation in *s*[*pos*] array — in position *l*[*i*] we will add 1, and in posiotion *r*[*i*] + 1 — -1. Then we will calculate partial sums of array *s*[*pos*], and if *s*[*pos*][*i*] > 0 (the sum on prefix length *i* + 1), then bit at position *pos* will be 1, otherwise — 0.

After that, you can use segment tree to check satisfying constraints.

Jury's solution: 8394894

## 483E - Game with Strings / 482C - Game with Strings

Problem author gridnevvvit

Let's handle all string pairs and calculate the *mask* mask, which will have 1-bits only in positions in which that strings have the same characters. In other words, we could not distinguish these strings using positions with submask of mask *mask*, then we must add in *d*[*mask*] 1-bits in positions *i* и *j*. This way in *d*[*mask*] we store mask of strings, which we could not distinguish using only positions given in mask *mask*. Using information described above, we can easily calculate this dynamics.

Now, when we have array *d* calculated, it is not hard to calculate the answer. Let's handle some mask *mask*. Now we should try to make one more question in position *pos*, which is equal to adding one more 1-bit in *mask* in position *pos*. After that we may guess some strings, they are 1-bits in mask `s = d[mask] ^ d[mask | (1 << pos)]`

. Then you have to calculate number of bits in *s* quickly and update the answer.

Jury's solution: 8394918

## 482D - Random Function and Tree

Problem author RoKi

Let's calculate *d*[*v*][*p*] dynamics — the answer for vertex *v* with size of parity *p*.

At first step to calculate this dynamic for vertex *v* we should count all different paintings of a subtree visiting all children in increasing order of their numbers. By multiplying this number by 2 we will get paintings visiting children in decreasing order. Now some paintings may count twice. To fix that, let's have a look on a some subtree of a vertex *v*.

Consider all the parities of children subtrees visited by our function (0 or 1). First thing to note is that among these parities exist two different values, the subtree will have different paintings with different ordering (you can prove it yourself). Otherwise, all our children sizes have the same parity.

If all sizes are even, this subtree will be counted twice. Otherwise, if sizes are odd, we are interested only in odd count of visited subtrees. This way, we must subtract from our dynamic the number of ways to paint any number of children with even subtree sizes and odd number of children with odd subtree sizes.

Jury's solution: 8394936

## 482E - ELCA

Problem author dans

Let's split all *M* requests in blocks containing requests each. Every block will be processed following way:

First using dfs we need to calculate for every vertex *v*, where *u* is every ancestor of *v*, *size* _{ i} — size of subtree of vertex *i*, including itself. This value shows how will the answer change after removing or adding vertex *v* as child to any other vertex, furthermore, answer will change exactly by *path* _{ v}·*size* _{ v} (decreasing or increasing).

Then we will calculate *ch* _{ v} the same way — the number of all possible vertex pairs, which have LCA in vertex *v*. This value shows how the answer changes after changing *V* _{ v} — if *V* _{ v} changes by *dV* _{ v}, answer changes by *ch* _{ v}·*dV* _{ v}.

Then mark all vertexes, which occur in our block at least once (in worst case their number is ). Next, mark every vertex being LCA of some pair of already marked vertexes, using DFS. We can prove that final number of these vertexes is at most . After all this we got 'compressed' tree, containing only needed vertexes. Parent of vertex *i* in compressed tree we will call vertex numbered *P* _{ i}.

On the image above example of this 'compression' way is given. Vertexes colored red are vertexes in request block, blue — vertexes marked after LCA, dotted line — *P* _{ v} → *v* edges in compressed tree.

On such compressed tree we need to calculate one new value *C* _{ v} for every vertex *v* — the size of a vertex, lying on a way from *P* _{ v} to *v* after *P* _{ v} on main (non-compressed) tree (son of a *P* _{ v} vertex in main tree).

Now we should process request on changing parent of vertex *v* from *p* _{ v} to *u* on a compressed tree. The answer will change by *path* _{ v}·*size* _{ v}. Now for every vertex *i*, lying on a way from root to *P* _{ v} vertex, two values will change: *size* _{ i} will be decreased by *size* _{ v}, but *ch* _{ i} will be decreased by *size* _{ v}·(*size* _{ i} - *C* _{ t}), ( *P* _{ t} = *i*), but *path* _{ i} will stay unchanged. For every other vertex *j* only *path* _{ j} will be changed: it will be decreased by . After that, we got compressed subtree where subtree of a vertex *v* is missing. Next, doing the same way as above, all values are changed considering that *v* (and all it's subtree) is a children of a vertex *u*. Do not forget to change *C* _{ v} too.

Let's see, how the value-changing request of a vertex *v* is to be processed. As described above, the answer will be changed by *ch* _{ v}·*dV* _{ v}. For every vertex *i* lying in vertex *v* subtree only *path* _{ i} will be changed (it could be easy done using *C* _{ to} values), all other values stay unchanged.

This solution has complexity, but in *N* = *M* case it has to be .

Авторское решение: 8394944

Well, will English translations be available at sometime? google translate was blocked in China.:D

translate.google.cn

I think that problem 483B could be solved in a mush easier way, which works O(1): 8386617

The gcd function would give the log factor. So it wouldn't be O(1) ;)

As soon as

xandyare primes, there is no needed — their is equal tox·y.Oh, I see. I forgot that.

There are lots of very similar ways to solve Game with Strings. Here are two more of them: first, calculate

d[mask] as in the editorial.1) The number of different masks you visit during a game is equal to number of questions you ask. So from linearity of expectation the expected number of questions is the sum of the probability that you will visit

mask, for all masks.That probability is easy to calculate: it's .

See 8396215 for the code.

2) Let

dp[mask] be the expected number of questions from state mask. When we try to ask questionposin statemask, the chance that we will not guess correctly right away is equal to the fraction`frac = bitCount(d[mask ^ (1<<pos)]) / bitCount(d[mask])`

.So

dp[mask] is equal to the average of`1 + dp[mask ^ (1<<pos)] * frac`

, for allposnot in mask.See 8412617 for the code.

Why should I divide binom(len, bitCount(mask)) ? I don't understand this.Can you explain it?

You're on the start state and you want to know the probability that you will reach the set of questions asked represented by

mask. You will reachmaskunless one of the following things happen first:maskyou already know what the answer is.mask.For the first item not to happen, you need the final answer to be one of the values which is not immediately distinguishable from the replies to queries to positions in

mask. By construction of the d[] array, there arebitCount(d[mask]) such values, so the probability that the answer is a "good" one for you to reach mask is .This leaves the second case. If the answer is a good one, it's still possible that you will ask a question not in

mask, and therefore not reach it. For this not to happen, you need your firstbitCount(mask) questions to be exactly the ones inmask. As you have len choose bitCount(mask) ways (orbinom(len,bitCount(mask)) ways) to choose your first bitCount(mask) questions and only one of those ways is good, this is responsible for the factor.What does totalGuessed[i] denote in the link given in the Div1 C- Game of Strings? http://codeforces.com/contest/483/submission/8394918

Why we are doing sum[r[i]]-- in the solution of Div1-B ??

Instead of iterating all the intervals to set the bits to 1, he stores the beginning and end of those intervals and only iterates the whole array one time for each bit position

Can someone explain Div2/b with more details?

always CE

Define the value for INT_MAX

use #include<limits.h> for using INT_MAX

Sorry, but can anyone explain to me the Div1-C method? I can't understand it well. 1. the solution says:

`Let's handle some mask mask. Now we should try to make one more question in position pos, which is equal to adding one more 1-bit in mask in position pos.`

What does it mean by saying "make one more question"? 2. what does this`s = d[mask]^d[mask|(1<<pos)]`

mean exactly? I took it as "if I guess position pos, the remaining 1-bit(s) in s denote the string that I am not sure if the selected string is one of them". But I am not sure about this.Please forgive my broken English...

Deleted

Link to my sol for C:- http://codeforces.com/contest/483/submission/20360073

Here's what i did:

We need K distinct numbers.right.

So,i tried to get difference K,then K-1,then K-2 till 1.

Example:- 5 3

step 1: 1

step 2: 1 4

step 3: 1 4 2

step 4: 1 4 2 3

step 5 : print rem with diff 1;

so answer is 1 4 2 3 5.

could some one explain the answer for problem b in binary search please

HI ,Every one.

Can you help me with pro Div1B/Div2D ?

My submission got ACCEPTED.

BUT, I have a test which it's answer should be NO.

AND my AC code print YES.

the test is:

5 2

1 5 1023

1 5 511

AND THIS IS MY SUBMISSION:

http://codeforces.com/contest/482/submission/29177333

extraVirgin20

Abusing notifications, huh?

Passing system test doesn't mean your program is correct. It only means it passed all tests, the author thought of. It should happen barely, but you can't get rid of it.

For example think of a code, which does if()s for all given test cases, and prints the correct answer. Obviously it won't work generally, but it will get AC.

It's okay if you notice this, and ask to add this to tests, but you shouldn't abuse notifications, as extraVirgin said. Also, you should fix your code at your own, to pass both system tests, and this test.

For 483A: How to code the first method? Let's handle all possible triples and check every of them for being a counterexample. This solution works with asymptotics O(n3logA)??

segment tree is not necessary in 483D

.