### danilka.pro's blog

By danilka.pro, 6 years ago, translation,

## 483A - Counterexample

Problem author gridnevvvit

This problem has two possible solutions:

1. Let's handle all possible triples and check every of them for being a counterexample. This solution works with asymptotics O(n3logA)
2. 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 f1 be the number of numbers divisible by x, f2 — 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 f2 - both numbers, and to second friend f1 - 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

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, sizei — 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 pathv·sizev (decreasing or increasing).

Then we will calculate chv 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 Vv — if Vv changes by dVv, answer changes by chv·dVv.

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 Pi.

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 — Pv → v edges in compressed tree.

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

Now we should process request on changing parent of vertex v from pv to u on a compressed tree. The answer will change by pathv·sizev. Now for every vertex i, lying on a way from root to Pv vertex, two values will change: sizei will be decreased by sizev, but chi will be decreased by sizev·(sizei - Ct), (Pt = i), but pathi will stay unchanged. For every other vertex j only pathj 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 Cv 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 chv·dVv. For every vertex i lying in vertex v subtree only pathi will be changed (it could be easy done using Cto values), all other values stay unchanged.

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

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

• +24

 » 6 years ago, # |   0 Well, will English translations be available at sometime? google translate was blocked in China.:D
•  » » 6 years ago, # ^ |   0 translate.google.cn
 » 6 years ago, # |   +3 I think that problem 483B could be solved in a mush easier way, which works O(1): 8386617
•  » » 6 years ago, # ^ |   0 The gcd function would give the log factor. So it wouldn't be O(1) ;)
•  » » » 6 years ago, # ^ |   0 As soon as x and y are primes, there is no needed — their is equal to x·y.
•  » » » 6 years ago, # ^ |   0 Oh, I see. I forgot that.
 » 6 years ago, # | ← Rev. 2 →   +22 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 question pos in state mask, the chance that we will not guess correctly right away is equal to the fraction frac = bitCount(d[mask ^ (1<
•  » » 6 years ago, # ^ |   +3 Why should I divide binom(len, bitCount(mask)) ? I don't understand this.Can you explain it?
•  » » » » 2 weeks ago, # ^ | ← Rev. 2 →   0 Deleted
 » 6 years ago, # |   0 What does totalGuessed[i] denote in the link given in the Div1 C- Game of Strings? http://codeforces.com/contest/483/submission/8394918
 » 6 years ago, # | ← Rev. 3 →   0 Why we are doing sum[r[i]]-- in the solution of Div1-B ??
•  » » 6 years ago, # ^ |   0 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
 » 6 years ago, # | ← Rev. 5 →   0 Can someone explain Div2/b with more details? #include using namespace std; int main() { int cnt1,cnt2,x,y; cin >>cnt1 >>cnt2 >>x >>y; int l=1,r=INT_MAX,m; while(l=cnt1 && m-m/y>=cnt2 && m-m/(x*y)>=cnt1+cnt2){ r=m; } else{ l=m+1; } } cout <
•  » » 6 years ago, # ^ |   0 Define the value for INT_MAX
•  » » 6 years ago, # ^ | ← Rev. 2 →   0 use #include for using INT_MAX
 » 6 years ago, # |   0 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<
 » 6 years ago, # | ← Rev. 6 →   -14 Deleted
 » 4 years ago, # |   0 Link to my sol for C:- http://codeforces.com/contest/483/submission/20360073Here'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 3step 1: 1step 2: 1 4step 3: 1 4 2step 4: 1 4 2 3step 5 : print rem with diff 1;so answer is 1 4 2 3 5.
 » 3 years ago, # |   0 could some one explain the answer for problem b in binary search please
 » 3 years ago, # |   +4 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
•  » » 3 years ago, # ^ |   0
•  » » » 3 years ago, # ^ |   +3 Abusing notifications, huh?
•  » » » 3 years ago, # ^ |   0 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.
 » 2 years ago, # |   0 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)??
 » 6 months ago, # |   0 segment tree is not necessary in 483D
 » 5 months ago, # | ← Rev. 4 →   0 .
 » 2 months ago, # |   0 483A — Counterexample how did he come up with that algorithm ? can anyone please explain
•  » » 2 months ago, # ^ |   0 see ,between any two even number there is an odd number(ex:10,11,12) so if (r-l+1)>3 and l is even then the answer is l,l+1,l+2 else you have to print l+1,l+2,l+3.(please ensure that if d==3 or d<3 and l is odd then answer is -1)
 » 5 weeks ago, # | ← Rev. 2 →   0 In Problem A how my answer violates the range