majk's blog

By majk, history, 5 years ago, In English
Tutorial is loading...
Code (by arsijo)
Tutorial is loading...
Code
Tutorial is loading...
Code
Tutorial is loading...
Code
Tutorial is loading...
Code
Tutorial is loading...
Code
Tutorial is loading...
Code (by winger)
Tutorial is loading...
Code
Tutorial of Good Bye 2018
  • Vote: I like it
  • +154
  • Vote: I do not like it

| Write comment?
»
5 years ago, # |
Rev. 2   Vote: I like it +9 Vote: I do not like it

1091A Tutorial is not available

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

It was fast :D

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

I haven't seen such fast editorial! Cool contest, I enjoyed it!

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

Happy New Year!

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

Problem D: How do you prove that, there are no pair of indices (i, j) such that sum of [i, j] is n(n + 1) / 2 and [i, j] is not a permutation of 1, 2, ..., N ?

  • »
    »
    5 years ago, # ^ |
      Vote: I like it -8 Vote: I do not like it

    Hi, when N = 5 there is a subsequence 1, 5, [4, 3, 2, 2, 1, 3], 4, 5 and there is subsequence of length 6 and sum 15, isn't it?

    • »
      »
      »
      5 years ago, # ^ |
        Vote: I like it +8 Vote: I do not like it

      The length of that subsequence should be equal to N

      • »
        »
        »
        »
        5 years ago, # ^ |
          Vote: I like it +17 Vote: I do not like it

        Thanks, I completely overlooked that.

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

    If (i,j) is just one of n! permutations it is obvious permutation. Lets call (i,j) substring. It has k characters as suffix of some permutation and n-k characters as prefix of NEXT permutation. This k first characters of our substring: a) If they are not in decreasing order we then the next permutation will only differ on last (or less)k characters which implies the fact that n-k first characters of next permutation are same as in this permutation before. So, indeed if this k characters are not in decreasing order we obtain substring (i,j) which is permutation of numbers 1,2,3..n. b) Second option is that first k characters of our substring are the suffix of before permutation and they are all in decreasing order. Then, we cannot actually obtain substring starting with this k characters. Why? This is about the thing that last k characters will differ in before and next permutation by exactly one letter, which implies that their prefixes of length n-k will differ exactly by one letter too. I know how to prove it when k is the longest decreasing suffix. Then it is pretty clear. In other case(k is decreasing suffix but not longest) i think that something similar is true. The proof when k is the longest decreasing suffix comes from algorithm for generating all permutations in lexicographical order. Im sorry for my poor english. Let me know if something is unclear or if im wrong somewhere.

  • »
    »
    5 years ago, # ^ |
    Rev. 2   Vote: I like it +5 Vote: I do not like it

    I think the following is pretty unreasonable to come up with in contest...

    We will show inductively that for any consecutive permutations σ, σ + 1,  if the sum of the first k elements are the same, the first k elements must be permutations of each other. Let σ = (a1, ..., an), σ + 1 = (b1, ..., bn).

    Let k be the length of the longest common prefix of σ, σ + 1,  and let m < n be maximal such that and for now assume k > 0. The key idea is that (ak + 1, ..., an), (bk + 1, ..., b2n) are consecutive permutations. Thus if k < m,  we have ak + 1 + ... + am = bk + 1 + ... + bm which by the inductive hypothesis implies that ak + 1, ..., am are a permutation of bk + 1, ..., bm,  contradicting the maximality of k.

    We now take care of k = 0. Clearly σ = (a1, a2 > a3 > ... > an) and σ + 1 = (a1 + 1, b2 < b3 < ... < bn). Then it is clear that the prefix sums of length  > 2 are greater for σ than σ + 1. For length 2,  equality only holds if the biggest number in (1, ..., n) is one more than the smallest, i.e. n = 2 which is checked by hand.

    With his lemma, we can show that if a contiguous interval of length n has sum n(n + 1) / 2,  it must be a permutation of 1, ..., n. Indeed, let this contiguous interval be lodged between two consecutive permutations (c1, ..., cn), (cn + 1, ..., c2n). Then which by the lemma implies that (cn + 1, cn + 2, ..., cn + k) is a permutation of (c1, ..., ck). Thus as permutations, as desired.

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

      There is few flaws in this proof.

      Thus if k < m,  we have a_(k + 1) + ... + a_m = b_(k + 1) + ... + b_m which by the inductive hypothesis implies that a_(k + 1), ..., a_m, are a permutation of b_(k + 1), ..., b_m.

      This induction implies that in inductive hypothesis, (a1 , ... ,  an) and (b1, ... ,  bn) don't need to form a permutation of consecutive numbers.

      We now take care of k = 0. Clearly σ = (a_1, a_2 > a_3 > ... > a_n) and σ + 1 = (a_1 + 1, b_2 < b_3 < ... < b_n).

      Then in this statement, σ + 1  might not begin with a1 + 1. It can be some other numbers in (a2,  a3 , ... , an) which is greater than a1.

      Besides, I don't think it is clear that the prefix sums of length  > 2 are greater for σ than σ + 1. Maybe it needs some further proof. Sorry for my poor intelligence.

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

        I'm not sure that I understand what you mean by the first point. What I'm using is that if two consecutive permutations have a common prefix of positive length, the remaining parts of the permutations are consecutive permutations of each other. For example 21354 -> 21435.

        I didn't explain the k = 0 case in much detail. The fact that σ + 1 must start with a1 + 1 comes from the fact that σ, σ + 1 are consecutive permutations and we need the first element in the permutation to swap out.

        For your last point, just see that the relative ordering of the ai and bi ensures the inequality... the details are a bit messy so I'll spare you, but I think it's clear (try 35421 vs 41235, for example to see what happens).

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

      Brilliant ideas!!

      One question here, why you say contradicting the maximality of k? By the hypothesis of the induction, they are permutations of each other, however, this does not imply that ak + 1 = bk + 1 and so doesn't contradict.

      Additionally, I think m doesn't have to be the maximal, it can be any value greater than longest common prefix.

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

Can someone explain how to check if the answer is on the left or the right?

  • »
    »
    5 years ago, # ^ |
    Rev. 4   Vote: I like it +11 Vote: I do not like it

    UPD: I'm explaining the solution of Problem E...

    In fact, the Theorem we used can be changed to:

    For each

    . So we only need to consider these two situations: point n+1 is in the set or not.

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

When you didn't realize the link in E was useful, so you had to come up with your own graph realization algorithm :(

  • »
    »
    5 years ago, # ^ |
      Vote: I like it +30 Vote: I do not like it

    Problem E is really not so friendly...

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

OMG! SO FAST!

Wish every coder : Happy New Year!

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

that was so fast , thank you for a great contest

»
5 years ago, # |
  Vote: I like it +14 Vote: I do not like it
»
5 years ago, # |
  Vote: I like it +1 Vote: I do not like it

That was fast insert Pikachu(o_o) meme

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

Here's a tutorial for similar optimizations as in problem H.

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

Alternate solution for B: Generate all points (xi + aj, yi + bj). Print the one that occurs n times.

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

    Can you please elaborate it ??? I find it difficult to understand

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

      since it proves that T always exists, so the equality (xi + aj, yi + bj) = T is always able to be satisfied?
      so we compute all possible (xi + aj, yi + bj), since (xi + aj, yi + bj) = T is always able to be satisfied, and 1 ≤ i ≤ n,so this equality can be satisfied by (at least) n pairs of (i, j),so this solution is correct?

      nah tbh i'm not sure as well :/

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

        Thank You ! :)

        • »
          »
          »
          »
          »
          5 years ago, # ^ |
            Vote: I like it +3 Vote: I do not like it

          But the time complexity is O(N^2log(N^2)), wouldn't that be a little too slow?

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

      T will be one of them (x_i + a_0 , y_i + b_0) (1<=i<=n)

      so , iterate over them and check for n points code

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

    Did the same but don't know why is it WA can you help please? 47738111

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

Wikipedia is useful for E but I didn't found it:(

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

I never saw this coming so fast :p

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

One can find complement of D (n·n! - an) in OEIS: A038156

  • »
    »
    5 years ago, # ^ |
      Vote: I like it +5 Vote: I do not like it

    god OEIS :/

  • »
    »
    5 years ago, # ^ |
      Vote: I like it +26 Vote: I do not like it

    I checked the sequence itself and also some intermediate results, but unfortunately not the complement.

    • »
      »
      »
      5 years ago, # ^ |
        Vote: I like it +3 Vote: I do not like it

      If you had found the complement sequence, would you have put a different problem D?

      • »
        »
        »
        »
        5 years ago, # ^ |
          Vote: I like it +27 Vote: I do not like it

        Probably yes. And this problem would still be useful for a contest without Internet access, e.g. local ACM contest.

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

    Damn i tried finding a$_n$ directly and couldn't find it. had to come up with a recurrence myself by just seeing patterns.

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

      Me too... looks like I need to work on OEIS skills :/

      • »
        »
        »
        »
        5 years ago, # ^ |
          Vote: I like it +23 Vote: I do not like it

        Maybe you should work on your problem solving skills?

        • »
          »
          »
          »
          »
          5 years ago, # ^ |
            Vote: I like it +12 Vote: I do not like it

          Haha, that goes without saying.

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

    Why do we subtract from n.n! ?

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

How didn't my Recursive Backtracking get TLE on problem B?
link here: http://codeforces.com/contest/1091/submission/47737516
I can't prove its time complexity but it seems to be like due to its elapsed time?

Well seems it works much slower than
but it took only 0.5s ,how?

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

    The Verdict is Accepted, isn't it?

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

      so i'm confused :/
      i thought it should get a TLE but it didn't
      this makes me puzzled

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

    Your solution's time complexity is about . In test 47 and 49, it equals 5 × 108.

    Thanks to Codeforces' amazing servers, it can pass the system test.

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

Wait, where did it say that sqrt in problem G returns a sqrt randomly?

  • »
    »
    5 years ago, # ^ |
      Vote: I like it +29 Vote: I do not like it

    You are choosing x randomly so in the n = pq case, the system doesn't know which of the four possible values of x yield a square of x2. The square root returned is random in the sense that it only depends on and not x.

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

I can't understand C. Can Anyone explain?

  • »
    »
    5 years ago, # ^ |
    Rev. 4   Vote: I like it +1 Vote: I do not like it

    it's easy to understand that, m = DivisorCountOf(n);

    Now, how to get f_1, f_2 , f_3, ......f_m ?

    What will be the series for some k , n (for all k , n%k == 0)

    1. k = 1, n = 6 , series a: 1 2 3 4 5 6
    2. k = 2, n = 6 , series a: 1 3 5
    3. k = 3, n = 6 , series a: 1 4
    4. k = 6 , n = 6 ,series a: 1
    5. series len = n/k

    6. a_1 = 1
    7. a_len = n-(k-1); /// cause , (a_len + k)%n = 1

    so , series sum : len*(a_1 + a_len)/2

    Here is the Code

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

Can anyone explain Problem C solution with example ?

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

Statement of problem B was very long and hard to understand hope to fix problems like that next time :)

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

Both My Solutions for B and C failed on main tests, but I can't seem to find my error.

Edit: I realized my Mistake with B

Problem B: https://codeforces.com/contest/1091/submission/47751047 Problem C: https://codeforces.com/contest/1091/submission/47762703

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

    I think it is because your condition in selecting minimum and maximum point

    if(a.x > max.x) { max = new Point(a.x, a.y); }

    maybe the a.x = max.x but a.y > max.y you don't handle this condition in your code

    the same thing in the min condition Java

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

      Thanks, I forgot to consider that. Do you know what could possibly be wrong with my solution for C?

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

Can anyone explain the recurrence in D?

  • »
    »
    5 years ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    as far as i understood :

    Well i thought about this while i was trying to solve as well it is true each part have same contribution , because when we switch the first digit ( 1 3 4 2 --> 2 1 3 4 ) the last digit of our last permutation is the same as the first digit of our next permutation during the switch , so concatenation of (1 3 4 2 + 2 1 3 4) , gives no sub string that has all 1 2 3 4. That's why they have equal contributions as the last digit will come twice always

    I am not sure how they counted for a single part i had a different approach which is giving some answer right and some wrong..

    but the recurrence is there as all the n! length string have same contribution , all starting with 1 , contribute the same as all starting with 2 , 3 and so on

  • »
    »
    5 years ago, # ^ |
    Rev. 6   Vote: I like it +1 Vote: I do not like it

    d(n) = (d(n - 1) + (n - 1)! - 1) × n

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

Can anyone please explain D?

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

walnutwaldo20 told me that on D, if you split the sequence evenly into n consecutive parts (each part has length n!) then:

  • each part contributes the same amount to the final answer
  • no valid subarray can overlap with 2 different parts

But we don't know why this is true. Does anyone know the proof?

edit: nvm ignore this

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

    Well i thought about this while i was trying to solve as well it is true each part have same contribution , because when we switch the first digit ( 1 3 4 2 --> 2 1 3 4 ) the last digit of our last permutation is the same as the first digit of our next permutation during the switch , so concatenation of (1 3 4 2 + 2 1 3 4) , gives no sub string that has all 1 2 3 4. That's why they have equal contributions as the last digit will come twice always

    Just explaining in case someone else also didn't understand this

»
5 years ago, # |
Rev. 2   Vote: I like it +37 Vote: I do not like it

The constraints on problem E seem incredibly tight.

Many O(N * log^2(N)) didn't pass (which is unsurprising, given 500K elements and 2 seconds). I thought the limit was so tight especially so that such solutions wouldn't pass (since there is a linearithmic solution), but the editorial does mention a O(N * log^2(N)) solution, so it seems like it was intended to pass after all?.

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

3 10 10 50 WGL

In problem F how is the ans 220 for this ? I think it should be 240? Am i missing something? pls help

  • »
    »
    5 years ago, # ^ |
      Vote: I like it +6 Vote: I do not like it

    Swimming for 120 minutes, gaining 40 stamina, then 50 minutes for grass getting another 10 stamina and 50 minutes for lava. 120 + 50 + 50 = 220min

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

Example of my pattern

I need help with why my solution is wrong for D So basicly my approach is dividing it into 2 parts , 1 is the obvious n! arrangements of 1 to n , then concatenations of 1 to 2nd and 2nd to 3rd and so on So what i found out was , when we check from 1st to 2nd we get exactly n-2 , from 2nd to 3rd we get n-3 this alternates all the way till the first digit is changed i.e ( 1 4 3 2 — > 2 1 3 4) so as each different digit startings ( 1 2 3 4 .... , 2 1 3 4 ... ) contribute the same amount so i found out the result for 1 2 3 4 and multiplied it by n for patterns starting with 1 , i did ((n-1)!)/2 * (n-2) + (((n-1)!/2) -1)* (n-3) , the -1 in 2nd case because last transition for digit change would have no substring that has all 1 to n Can anyone help me out

http://codeforces.com/contest/1091/submission/47759220

its giving correct answer for some cases and for some it is giving a wrong answer

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

Can I get a better explanation of D?

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

    Here's how I did it

    Two ways to have the required subarrays:

    (i) All the n! ones
    (ii) Making subarrays in between 2 adjacent permutation
    
    1. for first just add n! into the answer and for the second kind we can iterate from 2 to n-1
    2. for each i we will consider two adjacent permutations are different from ith place
    3. hence we can make n-i subarrays in between these two which will have sum = n * (n+1) / 2
    4. we select n-i numbers from n to place before ith place and permutate them
    //int front = fac[n-i] * ncr(n,n-i)
    

    Now,

    1. ith place needs to be different for all the permutations above
    2. we can select these number for ith place with iC1 which equals i
    3. as these permutations will be one after another we can get subarrays from i-1
    4. we multiply n-i into this to get total number of subarrays
    //int back = (n - i) * (i-1)
    

    front * back ∀ i + n! will be the answer 143534711

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

I have solved 'D' by finding patterns.

Divided into some points:

  1. Each legal sub array of length n must have numbers from 1 to n at most once.
  2. So, each permutation counts as one of the legal sub array.
  3. Now extra legal sub arrays come after concatenating permutations. Two adjacent permutations definitely have ELSA(Extra Legal Sub Array) from n-2 to 0. The number of ELSA varies according to the first place from the left side where two adjacent permutations don't have same digit.

(Ex-1: n=3, 1234 & 1243; 2nd place from right or 3rd from left. Ex-2 : n=3, 1243 & 1324; 3rd place from right or 2nd from left.)

  1. Here comes my pattern finding. For 2nd place(from right) ELSA is n-2, for 3rd place(from right) ELSA is n-3, for 4th place(from right) ELSA is n-4, goes on unless ELSA is 1 at the (n-1)-th place.

  2. Now I have to find how many adjacent pairs of permutation have X-th digit(from right) variant.

  3. Then, (Number of X-th digit variant) * (n-X);
  4. Here we found total ELSA after concatenation, now add the number of permutation.

Finding amount of each X-th digit variant: (By example, for n = 4): 1. 1-th variant or, number of permutation : 4 * 3 * 2 * 1 = 24 2. 2-th variant : 4 * 3 * (2-1) * 1 = 4 * 3 * 1 * 1 = 12 (ELSA = 2) 3. 3-th variant : 4 * (3-1) * 1 * 1 = 4 * 2 * 1 * 1 = 8 (ELSA = 1) 4. 4-th variant or n-variant never need because it's corresponding ELSA = 0. Now, 24 + (12*2) + (8*1) = 56.

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

Problem E can also be solved without using the property that the answer lies in a contiguous interval (yeah, guess who didn't realize this during the competition).

Following the Erdos-Gallai theorem, assume we want to check whether d could be the answer. Let p be the position d would be in the degree array. For every position i ≤ p, d would appear on the left hand side of the inequality, thus posing a lower bound on d; for every position i > p, d would appear on the right hand side, thus posing an upper bound.

The bounds at each position can be preprocessed using prefix sums (for right hand side we could do a binary search instead of value counts), and we can also perform another scan to obtain the "prefix lower bounds" and "suffix upper bounds" (i.e. intersection of prefix/suffix bounds). To check whether d could be the answer, just check the prefix and suffix bounds, and also whether the inequality satisfies at position p itself. Checking one value takes time (just have to do a binary search), so the whole algorithm is .

One caveat that can be ignored: we still need to take the min function on the right hand side into account: if i < d then we should ignore the bound. This means that when checking the answer, instead of using the "prefix" bound, we should use the bound of an interval, which is the maximum value in the interval. This is an static RMQ problem and can be solved in O(1) using sparse tables. However, it seems that the lower bounds are non-decreasing (bounds at higher indices are always tighter (larger)), so this is unnecessary.

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

    In the contest I solved it only realizing it had to be a contiguous interval between [ai, ai + 1] where the ai's are the integers in the input in sorted order.

  • »
    »
    5 years ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    Well, I just want to mention that this solution can be hard to implement. During the contest I also came up with a similar idea, but it took me more than 1 hour to code.

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

      Yes, you're right. I had a bunch of off-by-one errors before I could get it to work.

    • »
      »
      »
      5 years ago, # ^ |
        Vote: I like it +8 Vote: I do not like it

      Indeed, it's quite a bother to implement. The version I got in the contest is actually O(n) after sorting: 47753124. The prec0 and prec1 arrays correspond to the "sum of mins" part in the formula which can be precomputed by two pointers, and the lower/upper bounds are accumulated once and for all in opposite directions.

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

    How do you mean its O(logN) per value? For every value, you need to check all preffixes, not only one, isnt it O(n) per value? Looks like you are checking only prefix till new value but that does not make sense, becase that is not only prefix that can make inequality wrong.

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

      To check whether degree d could be an answer, we need to first find the position p of d in the sorted array of degrees. Then, we need to check whether all n inequalities hold. These can be categorized by their positions i into three types: i < p, i > p, and i = p. As I mentioned in the previous post, each type could be checked within time with a bit of preprocessing. It's a bit hard to explain so I won't go into details here, feel free to check out my submission: 47769662.

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

I am still a Beginner ^ _ ^ need help in A please

  • »
    »
    5 years ago, # ^ |
      Vote: I like it -13 Vote: I do not like it

    iterate from 100 to 1 for finding valid number of flower code:: scanf("%d %d %d",&a,&b,&c); for(int i=105;i>=0;i--) { if(i<=a+1&&i<=b&&i+1<=c){ cout<<3*i;break; } }

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

Let me share my approach on D!

We can observe (I have yet to figure out how!) that valid subarrays are the ones that contain all the n distinct numbers (from 1 to n). So, for example, for n = 5: [4, 2, 3, 5, 1] is a valid one, but [4, 2, 2, 3, 1] is not!

So, now let's write down the concatenation of the permutation for n = 4 (hoping to find a useful pattern):

1 2 3 4 (p1)

1 2 4 3 (p2)

1 3 2 4

1 3 4 2

1 4 2 3

1 4 3 2

2 1 3 4

.... (pn!)

Obviously, for each p, all the subarrays that start at the beginning are valid (n! so far). (S0 = n!)

What about the the ones that are beginning at the middle of p? (for example, on p1, the [2, 3, 4, 1] subarray?)

As you can see, we need the part of the subarray that belongs to the next p (its prefix) must be equal to the prefix of the current p. So, for instance, on p1 if we take (again) the [2, 3, 4, 1] subarray, we observe that the part which overlaps with p2 (ie. the 1) occurs on p_1, too! So, by applying this reasoning, we can conlude the following:

If the subarray begins from 2nd number of a p (it ends on the 1st number of the next p), it is invalid only when the 1st number is changed (see the last p on the example) (ie when 1st number on permutation become 1 (the first number), 2, 3, 4, ..., n). The valid subarrays for this case are S1 = n! (total permutations) — n

If the subarray ends on the 2nd number of the next p, it is invalid only when the 2nd number changes (ie. when the prefix becomes _ -> [1, 1], [1, 1] -> [1, 2], ..., [1, 4], [2, 1]) and this happens n * (n — 1) times, so S2 = n! — n * (n — 1)

...

By continuing like this, we get Sk = n! - n·(n - 1)·...·(n - k + 1)

Hence, we need to calculate the following expression:

ans = S0 + S1 + ... + Sn = n! + (n! - n) + (n! - n·(n - 1)) + ... + (n! - n!) = n·n! - (n + n·(n - 1) + ...)

How can we calculate the second part?

We can use DP, where:

DP[n] = n

DP[n - 1] = DP[n]·(n - 1)

...

DP[k] = DP[k + 1]·k (until k = 2 is enough)

And we sum them! :)

I hope that this helps!

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

    you don't need to calculate the second part using dp. just sum it is ok.

  • »
    »
    17 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    After reading so many solutions, only yours I understand, you deserve more likes, here I give you a belated like :)

  • »
    »
    8 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    CAN you explain what do you mean by this ---->

    If the subarray begins from 2nd number of a p (it ends on the 1st number of the next p), it is invalid only when the 1st number is changed (see the last p on the example) (ie when 1st number on permutation become 1 (the first number), 2, 3, 4, ..., n). The valid subarrays for this case are S1 = n! (total permutations) — n

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

For problem E: I found the Erdős–Gallai theorem after googling with something like "valid degree sequence". After some investigation into the theorem, I figured out that a solution could be devised using it. However, there are so many details (at least for me) involved in the adaption of the theorem to this problem that I couldn't make it within the last one and a half hour during the contest :(. For those who may be interested in this approach, see my submission 47772075 for detail. The complexity is .

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

How does one show that the given solution for F is optimal?

»
5 years ago, # |
  Vote: I like it +26 Vote: I do not like it
We use the Erdos-Gallai theorem linked in the statement to determine whether a sequence is graphic.

I just thought the link was to explain the statement during the contest...

Cannot imagine there exist some solutions...

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

In the problem E,how to make Havel–Hakimi algorithm faster?

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

    calculate the prefix sum of di at first so you can calculate in O(1)

    enumerate k from n to 1,define cnti as the amount of dj(k ≤ j ≤ n) which equals to i,then you can calculate in O(1)

    (every time k is decreased,,and for dk,add it to sum and cntdk if dk ≤ k)

    then you can do it in O(n)

    • »
      »
      »
      5 years ago, # ^ |
      Rev. 3   Vote: I like it +15 Vote: I do not like it

      Thank you.But what your said is Erdős–Gallai theorem.In the soluton above,majk mentioned that "Alternatively, we can perform a similar binary search using Havel-Hakimi algorithm, using a segment tree or a multiset and tracking whether the an + 1 was already processed or not to find out whether the answer is too small or too big. This yields O(nlog2n) algorithm." .But I don't know the detail about it.

      • »
        »
        »
        »
        5 years ago, # ^ |
          Vote: I like it +15 Vote: I do not like it

        emmm... I don't know how to use a segment tree or a mulitset to make it faster,but I know how to use splay to do it.

        In Havel–Hakimi algorithm,you need to find the maximum ,delete it and decrease the biggest k numbers.

        take di as the key value,the first and second operation will be simple.

        for the third operator,splay the (k+1)th number to root(if the kth and (k+1)th are equal and on the same node,split the node),mark on the root's left son,delete the root and insert it back(after decreasing the biggest k numbers,the root might be less than its left son(assume we put the bigger node on the left),so we reinsert it to make it in the right place).

        note that the third operation is very complex.

        • »
          »
          »
          »
          »
          5 years ago, # ^ |
            Vote: I like it +5 Vote: I do not like it

          That's right ! Thanks a lot ! Happy new year!

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

Problem E is terrible for someone who didn't care about the link.

It's really surprising that part of the solution is offered in the description.

»
5 years ago, # |
Rev. 2   Vote: I like it +15 Vote: I do not like it

I passed H without bitset.

Maintain a list of vectors, vector[i] contains all j's which grundy[j] = i. When calculating grundy[x], find y from vector[0], vector[1], ... orderly. While finding in vector[i], if x-y is available, break, and if no break happens in vector[j], then grundy[x] = i and finish.

I can't prove the time complexity, but it performs well.

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

Alternatively, we can perform a similar binary search using Havel-Hakimi algorithm, using a segment tree or a multiset and tracking whether the an+1 was already processed or not to find out whether the answer is too small or too big. This yields O(nlog^2 n) algorithm. So,can a solution Accepted this problem with O(n log^2 n)?n=500000 and n log ^2n=1.8e8.Also multiset is very slow.... P.S. 5e5 is enough to make python programmer TLE in input.

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

    It's said that the time limits are based on Java...

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

Shouldn't the sum in the explanation of D should be instead of

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

I cannot figure out how to make the Havel-Hakimi algorithm faster. How can I sort the degree sequences after each update operation if I use segment tree?

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

If we do not work out the factor list first, but use the following codeif n mod k = 0 then continue, can it pass the tests in problem C?

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

Can someone explain why maximum Grundy number on H is 100? I was hoping the editorial would explain it but it didn't.

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

I made some videos with more detailed explanations for some of the problems. I think my solution to F is a bit simpler than the editorial; the idea is to start with flying over everything and proceed greedily from there.

A: https://www.youtube.com/watch?v=Ue7gAIwhSoo

B: https://www.youtube.com/watch?v=sY1WOkz8K28

C: https://www.youtube.com/watch?v=CFP_lctIss4

F: https://www.youtube.com/watch?v=oj6bcK405lc

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

How to do binary search over a boolean array as said in editorial for E? Array is something like [ N, N, N, Y, Y, Y, N, N]. Plz suggest a general approach.

I know how to binary search range in sorted sequence but this sequence does not appear to be sorted.

  • »
    »
    5 years ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    Once you determine that the answer is no, you can figure out if you are too high or too low based on where you fail. For example, for the degree sequence [4 3 2 1 ?], if we try setting ? as 3, and then do erdos gallai, the first 2 inequalities work (4<=0+4) & (4+3<=2 +(2+2+1)), but the third does not (4+3+3>6+(2+1)). Now, looking at just this inequality, the edge with length 3 increases the left side, but not the right. So increasing 3 will not work, and we know the answer is hence less than 3.

    If we try to make the answer 1, then the second inequality won't be satisfied, as 7>2+(2+1+1). If we decrease the answer further, the inequality still won't be satisfied, since it will be decreasing the right side of the inequality only (instead we will have 7>2+(2+1+0)). So the answer is greater than 1. Hence we only have to check 2 and that works.

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

Auto comment: topic has been updated by majk (previous revision, new revision, compare).

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

For those don't know Lexicographically Next Permutation algorithm.

Algorithm link

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

Problem D: I read the tutorial and i interpret that we are trying to find the answer by subtracting all the false subarrays.

For each permutation seperately , n! should be added to answer

And for second part in the tutorial They are taking number of total subarrays as n.n! . I can't understand this Can anybody please further explain this? Thanks in advance:)

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

In question E , i think testcases are not so strong. Because in following testcase , solution of ecnerwala fails. That testcase is :

9

4 3 2 2 2 2 1 0 0

Actual answer should be [0,2,4,6] , but according to his solution , output is [0,2,4,6,8] . Link to his solution is https://codeforces.com/contest/1091/submission/47743506 .

  • »
    »
    5 years ago, # ^ |
      Vote: I like it +18 Vote: I do not like it

    Oops, looks like you're right, I had an off-by-one that wasn't caught. It's fixed here 47800829 if anyone cares.

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

How to prove the greedy solution for F? In editorial there is an algorithm and reasons for it, but I still dont understand how to prove such solutions, when one changes previous steps of the strategy.

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

    First, you need to fly to pass lava. Then we greedily choose to gain as much stamina as we can. So we walk on grass and swim in water. There are two situations. 1. If we still run out of stamina at some place. Then we need to do some extra work to gain stamina. So we need to go backward and forward at some place before. 2. If through this method we can spare some stamina in the end, then in fact we can spend them before. The solution discuss this situation detailedly.

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

In problem C : Can someone explain What is the intuition or proof behind using only divisors as K values which give unique sums?

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

    for each k , series : 1 , 1+k , 1+2k , 1+3k , ...... , 1 + x*k ;

    where , (1 + x*k)%n == 1

    so , (x*k)%n == 0

    convince yourself , that (x*k) == n , not 2n,3n,4n.....

    that means k is a divisor of n

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

      why did you rejected 2n,3n,4n... ??

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

        Think the circle as 0,1,2,...n-1. You wanna back to 0 not 1.

        Now series : k,2k,3k,....,x*k

        you stop tour after finding first x such that (x*k)%n == 0 ;

        Now, you tell me how a number forms with it divisors? say 12 ::
        3 , 2*3 , 3*3 , 4*3 (which is 12 itself)

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

    The fun value only depends on gcd(k,n), so you only need to iterate over possible gcds, and all gcds divide n (that's part of the definition of gcd!)

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

In D, Why do we subtract from n*n!?

  • »
    »
    5 years ago, # ^ |
    Rev. 8   Vote: I like it +1 Vote: I do not like it

    There are n! permutations , from each of them we can get at most n segments (1 from it , n-1 from its_suffix + next's_prefix) with sum of segment == n*(n+1)/2

    For our last permutation we don't have

    ( suffix_i + prefix_(i+1) == n*(n+1)/2 )

    so why we counting n from the last one?

    Look , last one has bad suffixes of length 1,2,3...n-1 ; All of which are includes in subtraction process ...-

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

Hey , I'm a newbie and just beginning CP. In Problem C , why the formula ,N*(i-1)/2 + i and N*(N/i-1)/2 + N/i , Can someone please explain this formula ? And it's significance in the problem ? What is the need for this formula , why not basic AP sum where d=i ? How do people crack such formula's within 2:30 hrs ?

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

Can't understand why i need to consider only k that divides n. Can anyone explain that moment in other words than in editorial pls?

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

    The fun value only depends on gcd(k,n), so you only need to iterate over possible gcds, and all gcds divide n (that's part of the definition of gcd!)

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

Problem E:How can you prove that "on the integers of same parity, the answer always forms a continuous interval"

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

    I do not have a simple mathematical proof, but you can definitely see this fact from the statement of Erdos-Gallai.

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

    We assume that x is the smallest possible answer for an + 1, assuming that there is an edge u - v in the graph, then we can change this edge to u - k, k - v without changing the degree of other points, obviously we have found that the degree of k has increased by 2, and this can be done for all other edges, so the possible answer for x is continuous and has the same parity.

    • »
      »
      »
      5 years ago, # ^ |
        Vote: I like it +9 Vote: I do not like it

      You also have assumed that neither of u-k and v-k was already formed

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

In problem G:

"The primes are of form 4x + 3 for a simple reason – we can find the square root of x modulo p by simply calculating ."

Shouldn't it be ?

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

毒瘤啊

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

Problem H:How can you proof that the Grundy number is less than 100?

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

"Coming back to our problem, if the suffix of length k is in decreasing order, than the prefix of length n−k of the next permutation has different sum from the prefix of the same length of the current permutation, hence the subarray has incorrect sum."

This statement requires a proof: a slick inequality statement which proves this. :D

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

In editorial of problem D, why we subtract from n*n! ? Is the given formula in simplified form? I can think of the number of subarray of length n is n*n!-n+1

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

    Because when we are doing the sum, the last permutation is in the formula $$$\sum_{k=1}^{n-1}$$$(n!/k!). In other words, you can solve it by the formula (n*n!-n+1 — $$$\sum_{k=1}^{n-1}$$$(n!/k!-1)). Subtracting 1 from n!/k! means that we are not going to take the last permutation into account.

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

      can you please explain from where this formula came n!/k! and i am not getting if we choose the first n-k elements freely as given in the tutorial we may be calculating the suffices repeating(means more than once) how are we so sure...please once more elaborate for me

      • »
        »
        »
        »
        5 months ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        As the editorial said we are permuting the first N - K elements freely. This means for the first N-K indices we have nP(n-k) which is nothing but N! / (N - (N - K)! -> N! / K!

»
22 months ago, # |
  Vote: I like it +10 Vote: I do not like it

I wonder if there is a convincible proof for the Grundy number of problem H is less than 100 or the problem setter just used programs to find it out