AkiLotus's blog

By AkiLotus, 5 years ago, In English

Model solutions are now available.
GreenGrape and I will write more about problem F, including an alternative data structures (still quite tight on time limit) ;)

1114A - Got Any Grapes?

Author: AkiLotus, GreenGrape
Development: AkiLotus, GreenGrape, neko_nyaaaaaaaaaaaaaaaaa
Editorialist: AkiLotus

Tutorial
Solution (Akikaze)

1114B - Yet Another Array Partitioning Task

Author: xuanquang1999
Development: AkiLotus, xuanquang1999
Editorialist: xuanquang1999, neko_nyaaaaaaaaaaaaaaaaa

Tutorial
Solution (xuanquang1999)

1114C - Trailing Loves (or L'oeufs?)

Author: AkiLotus
Development: AkiLotus, majk, cdkrot
Editorialist: AkiLotus

Tutorial
Solution (Akikaze)

1114D - Flood Fill

Author: neko_nyaaaaaaaaaaaaaaaaa
Development: AkiLotus, neko_nyaaaaaaaaaaaaaaaaa, cdkrot
Editorialist: neko_nyaaaaaaaaaaaaaaaaa, cdkrot

Tutorial
Solution 1 (_kun_)
Solution 2 (neko_nyaa)

1114E - Arithmetic Progression

Author: AkiLotus
Development: AkiLotus, GreenGrape
Editorialist: AkiLotus, xuanquang1999

Tutorial
Solution (Akikaze)

1114F - Please, another Queries on Array?

Author: AkiLotus, cdkrot
Development: AkiLotus, GreenGrape, cdkrot
Editorialist: AkiLotus, GreenGrape, cdkrot

Tutorial
Solution 1a (_kun_)
Solution 1b (Akikaze) [literally kun's solution, yet shorter, and a bit uglier :P]
Solution 2 (GreenGrape)
  • Vote: I like it
  • +148
  • Vote: I do not like it

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

Thanks for FAST editorial :)

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

    I believe the solution for the problem D is wrong. Take the following test case for example.

    5
    1 2 1 3 1
    

    Its obvious the answer is supposed to be 2. But both the solutions give 3 as output.

    Edit :- I misunderstood the question. There is nothing wrong.

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

      yeah, test is wrong

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

      Could you explain that for me? I don't know how to get 3 instead of 2.

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

      Can you please explain how the answer is 3 and not 2

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

        Starting index is 1 — 4 moves: [1 2 1 3 1] -> [2 2 1 3 1] -> [1 1 1 3 1] -> [3 3 3 3 1] -> [1 1 1 1 1] Starting index is 2 — 3 moves: [1 2 1 3 1] -> [1 1 1 3 1] -> [3 3 3 3 1] -> [1 1 1 1 1] Starting index is 3 — 3 moves: [1 2 1 3 1] -> [1 2 2 3 1] -> [1 3 3 3 1] -> [1 1 1 1 1] Starting index is 4 — 3 moves: [1 2 1 3 1] -> [1 2 1 1 1] -> [1 2 2 2 2] -> [1 1 1 1 1] Starting index is 5 — 4 moves: [1 2 1 3 1] -> [1 2 1 3 3] -> [1 2 1 1 1] -> [1 2 2 2 2] -> [1 1 1 1 1]

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

      Can anyone elaborate how would we drop the third parameter if we compressed the array in the first solution of D?

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

Problem E. Yeah, probability to fail finding 30 random elements is small, yet random_shuffle() (to randomize a permutation) can't manage to be random enough. Is there an explanation for that? It's so strange

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

Ah... It's such a pity that I fail to submit my code of problem-F in the end. Just few seconds! I didn't think I should get a segment-tree template before, but now I realize it's really important especially for those people like me whose finger are frozen and type slowly.

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

    Hmm...My fingers were also frozen,and so was my brain.I must have been crazy that I submitted the code to wrong problems twice

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

I can't read the codes for problems C and D!

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

what would be the ans in test case in problem C:

5 100?

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

    The number is (1, 20)100 so the answer is 0.

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

      Since 20 has a trailing zero , shouldn't ans be 1?

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

        Don't mix up 10-ary numbers and 100-ary numbers.

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

    The answer would be 0. 5! is 120. 120 in base 100, would be (1)(20), where the 2 numbers in the brackets are the 2 digits in base 10. This is because 120 can be expressed as 1 * 1001 + 20 * 1000. Therefore there are no trailing zeroes.

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

How can we talk about changing the color of some squares in segment [l, r] if we don't know where our starting square is(Since the starting square is the only one whose component we may change)?

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

    you need to make a for, leaving each Ci as initial, not to receive TLE, we need an array to save the previous results dp[i][j][1].

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

About B:
I had similar idea during contest, but didn't code it because i couldn't prove it. Still don't know its proof.
How to prove solution of B?

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

    Imagine the array where we remove all "irrelevant" elements that are not in the largest m * k elements. It is trivial to see that we can simply place the dividers for every m elements.

    Then just re-insert all the "irrelevant" elements back into their proper locations and we now have our complete answer.

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

      That is a very good way to imagine it. Thanks for sharing !

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

Could someone explain why this code is getting WA verdict at TC 10, 49727068 . It is the same approach as editorial and top users submissions.. I am unable to visualise the error.

  • »
    »
    5 years ago, # ^ |
      Vote: I like it +3 Vote: I do not like it
    while(tmp2/ii > 0) {
    	c2 += tmp2/ii;
    	ii*=i;//when i was about 1e10, overflow here!
    }
    

    You should divide tmp2 in such a case.

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

Isn't the answer for D n-1-ceil(LPS/2) ?? ( of course in the compressed one (for example 112553 -compress-> 1253 ) )

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

I'm sorry but, no.

Quote from editorial of E

How exactly was I supposed to know that checker isn't adaptive? This isn't stated in statements, meaning that we can not just assume that we can query random elements. I've coded a solution, asking only ? i where i ≤ 30 just because non-adaptiveness wasn't stated in the problem statements. I'm almost sure at the moment that with adaptive checker this problem is basically unsolvable.

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

    What prevented you from asking?

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

      Because this is common sense for interactive problems to explicitly specify whether checker isn't adaptive in case it being true. (if it is relevant to the solution)

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

        I guess it's our mistake to not write it in statements, however we indeed answered this question in clarifications to all interested.

        Also, I have a feeling that, at least on codeforces, we have an opposite situation: most of the interactive problems are not adaptive and when it is adaptive, it's written in statement (basically, since there is a need to specify hack format, it's mentioned, that jury can play differently).

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

          I think in most cases it's better not to write anything about interactor. Participants should be prepared for the worst case.

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

            Worst case is that jury's own randomised solution also fails in adaptive interactions!

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

smh problem A was obviously max flow, how did this contest even get accepted!? 49718487 :D

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

    I try to say something more funny about this but i cant

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

    If you are a RED CODER and contest is unrated for you that is how you approach easy implementation questions.

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

    Next time I should invite millions of friends for our grape party.

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

Can someone help with problem C. I am getting a WA on pretest 10. 49731384

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

How to prove B ?

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

    You have to choose m * k max. Let's write them somewhere, and if you have a segment from l to r and there are any m maxima in it that you wrote out, then the segment can be finished, so you will have m maxima in the first (k — 1) segment from those that you wrote out. And you will have a segment of length greater than or equal to m, since you have not chosen the remaining m peaks.

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

    Take k=2 and think about it, I guess everything will be clear then

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

    You just take M*K largest numbers. Obviously if we can use them for constructing subarrays they will provide the best result. Let's sort them by positions in the list. take the first M numbers and call their union "the 1st subarray", then the next M numbers, call them "the 2nd subarray" and so on. We constructed K subarrays which give the best result for this problem. There is nothing contradictory in this construction, each subarray has at least M elements and there are no upper bounds on the sizes of subarrays. So they're valid and this solution works.

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

Prob E:

I've got WA on first 2 submission 49735490, 49734969

Then after some minor changes I've got AC 49736359. I guess that even with little probability, it could still f- you up after all :<

Still, congratz on successfully held such an amazing contest like this on codeforces ^^ Hope to see more from us vietnamese and maybe more from HNA alumni if possible <3

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

    Thanks. ;)
    Just realized, I haven't worked with HNA's Informatics Team ever before.
    Maybe in the near future. ;)

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

      Well that day could be nearer than you think ;)

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

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

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

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

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

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

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

i love fast editorials <3

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

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

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

For D, I use the same dp state, dp[l][r][0/1]. But I get wa on 8 :( This is my code: https://codeforces.com/contest/1114/submission/49737924

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

For F problem we should precompute inverses of primes <300 ( mod 10^9+7) and then use it to multiply our answer by (p-1)/p, am i right?

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

I am not getting the cause of difference between the output of the same code when submitted on codeforces and on ideone..?

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

    @Akikaze @GreenGrape Can anyone tell why this solution is getting WA verdict? Offline it gives correct output for same test case and on other online compilers(ideone) also it works fine. Why it does not work on Codeforces?

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

Can't understand the second solution for problem D.

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

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

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

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

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

Thanks. First D solution was helpful to me. How intuitive and pretty it is!

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

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

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

1114B I can't understand (division[i] = ind[(i+1)*m — 1];) of M.xuanquang1999 solution....any explanation ?

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

    ind store the indices of m * k largest elements in the array a, sorted in increasing order. This array is 0-indexed.

    division[i] store the ending indices of the i-th array. This array is also 0-indexed.

    Since we need m elements for each subarray, division[i] is the value of the (i + 1) * m elements in ind. So, division[i] = ind[(i+1)*m - 1] (-1 because ind is 0-indexed).

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

Please cdkrot, may you explain to me the following cases in the first solution of D:

For dp[l][r][0], dose your code take in consideration dp[l][r - 1][0] and dp[l][r - 1][1]?

I see that your code, for calculating d[l][r][0] takes in consideration just dp[l + 1][r][0] and dp[l + 1][r][1]!

And also this is hold for dp[l][r][1] where I see that your code doesn't take in cosideration dp[l + 1][r][0] and dp[l + 1][r][1]!

And when I took those states in consideration, my submission failed on test 8, where my answer is smaller than judg's answer. So, please where is the wrong?

UPD: now I know why my submission was wrong, and corrected it. But still don't know the proof behind those states being negligible!

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

    hi~ would you like to explain why it is wrong? I have the same problem with you. I keep on getting WA on pretest 8.

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

      Hi,

      while calculating dp[l][r][0] and taking dp[l][r - 1][0] in consediration, we shouldn't add just 1 if a[l]! = a[r], we should add 2, because we need one turn to flood all the segment [l, r] with color a[r], then another turn to flood this segment with color a[l] (because that is what we are calculating).

      Why we should flood the segment with color a[r] firstly? why not just flood it with a[l]?

      The answer: the start point of all process should be still in segment [l, r - 1] to not clash with the fact that we cannot change the starting point.

      And this case is the same while calculating dp[l][r][1] taking in consediration dp[l + 1][r][1].

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

    The states are simple: note that in every moment the component containing a starting cell forms a segment and has its color equal to one of the ends of this segment.

    And then there are few transitions between

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

      Can you explain why it is unnecessary to consider dp[l][r - 1][0] and dp[l][r - 1][1] for calculating dp[l][r][0], please? Just because they need more transition?

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

        Note, that I've written in "forward dp"-style, that is, I update the next states using the current one.

        And if the current state is [l;r] and some color c, then extending to [l - 1;r] will make the segment having color[l - 1].

        And we can add the transition to [l - 1;r], color[r], by saying "go to state [l - 1, r], color[l - 1] first and then recolor again.

        But this is not necessary: there is no need to recolor to stay in the same segment, it's easy to see that it's not optimal (formally, not better than not doing this) ;)

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

          Thank you!!! I can understand it now.

          I also tried to prove dp[l][r][0] + 2 ≥ dp[l][r][1] + 1, which comes from dp[l - 1][r][0] = dp[l][r][0] + [s[l - 1] ≠ s[l]] + [s[l - 1] ≠ s[r]] and dp[l - 1][r][1] = dp[l][r][0] + [s[l - 1] ≠ s[l]].

          I guess |dp[l][r][0] - dp[l][r][1]| ≤ 1 is always true, and this submission(49850235) shows it is always true. But I don't know how to prove this.

          With dp[l][r][0] + 2 ≥ dp[l][r][1] + 1, it means two transitions are always worse than those with one transition, so dp[l][r - 1][0 / 1] needs two transitions to reach dp[l][r][0] and therefore is worse than dp[l + 1][r][0 / 1].

          This is not intuitive :( I think your explanation is better!! :D

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

            |dp[l][r][0] - dp[l][r][1]| <= 1 is always true.

            Suppose we can color [l,r] with color c_l in x steps. Then we can color [l,r] with c_r in x + 1 steps by first coloring to c_l in x steps then 1 more step to change color to c_r. Therefore the larger one is at most 1 more step than the smaller one.

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

My solution to problem F has complexity N*log(N) + Q*(log(N)+P) where P = 62 is the number of primes. Here's what I did : We need basically the product of values in the range l to r and for each prime, we need to check whether that prime exist in the range l to r. Product of values can be done in log(N) using segtree or bit. Now I created another segtree where each node has a bitset, which tells whether ith prime occured or not in the range. So we just have to do OR of all the bitsets. So operations on bitset is now almost O(1) (more precisely O(P/32)). But still my soln got tle due to heavy modulo operations. To reduce the number of modulo operations, I took the discrete log of all the values. Now the problem changes to range addition and range sum. The solution is very fast after taking discrete logarithm. Here's my submission : 49740935

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

    You can use long long instead of bitset<62>.

    How did you choose the base of the discrete log? Just some brute force?

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

      You find a primitive root in O(sqrt(MOD) * log(MOD)) or faster (or bruteforce and copy+paste it). Any primitive root is good enough.

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

        The method that I understood from Wikipedia page is

        cpp code

        But I can't see why this is O(sqrt(MOD) * log(MOD)). It looks like O(sqrt(MOD) + log^2(MOD)*R) where R is the root.

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

          That's kind of the method that I was talking about. The specific details of implementation change the complexity of the second check (in some code, people actually do O(sqrt) loop to look through every divisor of phi(n)).

          As for why I ignored R, R will be very low. The are phi(phi(N)) primitive roots of N. This number is really large and if you calculate the probability that a random number is a primitive root it's large enough that with a few tries it'll find one.

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

    Thanks for sharing — nice trick!

    But still my soln got tle due to heavy modulo operations.

    For your initial solution, do you have an implementation which will be N*log(N)? As far as I can tell, it has additional log(N) factor because you need to do exponentiation to apply "multiply on a segment" update — for example, in

    tr[nd] = (tr[nd] * pwr(lz[nd], e - s + 1, mod)) % mod;
    

    line in your code (49718093) — so it is not just "heavy modulo operations". For a few minutes I tried to find some workaround to shave off that log(N), but I didn't succeed.

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

      Hmmm. You are right. The soln was getting tle because of the additional log(N) factor because of exponentiation and the previous soln was not N*log(N), rather N*log^2(N). But since the editorial soln has a complexity of log^2(N), I believe the solution got tle because of heavy modulo operation. I also couldn't remove the log(N) factor by any means. Thanks for noticing it!

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

For D how do we prove that when we are flooding [L, R], minimum would be when we either color it with cL or cR. Why not with anything in between?

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

    In a turn, you always change the color of the connected component containing the starting square. So you have to change it to the color of the left or right component to be able to "merge" them.

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

      Makes sense, thanks!

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

      how to solve if starting square is not fixed?

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

        can anybody confirm that 49761228 will be the solution for above case.

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

          If it's possible to change the color of any component in a turn then it becomes problem K from 2008 Moscow Subregional. I don't know any solution better than O(n^3) for this problem.

          Your code gets WA in case

          13 10
          2 4 5 6 7 1 2 3 1 8 9 10 3
          

          The answer is 10.

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

    Because of the start point being unchangable, so, wherever you start the process, the first time the interval will be flooded with the same color, this color will be one of the two ends' color.

    So, if the minimum number of turns for reaching the same color the first time is x, any other turns to any other color will be added to x making the answer bigger.

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

      I meant Say we have 1 2 2 3 Then we can also change it optimally to 2 2 2 2 ( 2 moves ) But it will never be better than changing to L or R. Can be equal though.

      Edit: this state can't be reached

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

        It's not possible to achieve [2, 2, 2, 2] from [1, 2, 2, 3].

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

          Ok, I reread the question and realized it now :(

          Thanks both of you for your help!

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

          With 2 moves, I think, but it's possible in 3 moves:

          By selecting index 1 as starting index (1-indexed):

          [1, 2, 2, 3] -> [2, 2, 2, 3] -> [3, 3, 3, 3] -> [2, 2, 2, 2].

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

            I am still finding it difficult to understand the problem statement.

            [1, 2, 2, 3] -> [2, 2, 2, 3] -> [2,2,2,2]

            In above case, the connected component of 3 is just 3 itself and the starting connected component is at position 4(1-based indexing) and then we change this starting component to 2.

            Kindly help in my wrong undertanding.

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

              In the first step, you decided that the starting point of the process is position 1 (1-based indexed), and according to the problem's statement, one cannot change this position along the whole process, so you cannot change it to position 4 in the next step.

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

                So, that means if I select index 2 as the starting position then

                [1,2,2,3] -> [1,3,3,3]

                So, after this, I won't be able to change the configuration further because this is the last configuration. Right?

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

                  [1, 2, 2, 3]  - »» [1, 3, 3, 3]  - »» [1, 1, 1, 1]  - »» [2, 2, 2, 2]  - »» and so on...

                  So, you can always change the segment that the starting point lies in.

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

                  Aah Sorry my mistake

                  Thanx brother

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

                  Not bad. Welcome brother!

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

KEK

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

Can somebody explain the idea and logic behind Problem B using Nth_element concept as mentioned in editorial

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

problem C is just WoW! learned so many things! Thank you for such a beautiful problem <3

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

pE : doing binary search on rank of an element requires only log( n ) = 20 queries

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

xuanquang1999, What does od{x} represent in the proof of problem E?

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

    It is a formating error. What I supposed to write is .

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

    Yeah, I didn't see that formatting error at first. It's now fixed. ;)

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

Who can tell me about problem A that why there are '>=' in the tutorial but '>' in the solution?

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

    Well never mind, it's my mistake = =

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

in problem D, i found a lot of accepted solution (tutorial's solution included) that give the answer of 5 for the test :8 5 2 6 2 4 3 4 5. However I think the correst answer is 4. Can anyone explain this for me?

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

    hi~ in the problem statement, you need to change the component containing the start one to any other color every turn. I think you want to change 6 first and then 3. But this operation is forbidden.

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

      May I ask you a question? if each turn we can choose a connected block arbitrarily,then how to solve the problem?

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

    oh my mistake. Thank you

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

In problem F we have several ways to maintain the segment products with per query/modification. Here is my approach: Suppose we have 2 structure T1 and T2 which can maintain the prefix product and allows us to modify one place of the array with (Obviously both segment tree and fenwick tree can do it.) We will just discuss about how to handle the modification with r = n and the query with l = 1 (Other modification could be splitted into twice this kind of modification). Then the contribution of a modification on position l for a query on position r (l ≤ r) will be xr - l + 1 = xr / xl - 1. So we can maintain with T1 and with T2. Then Arr / Br will be the result of query(r). Here is my implementation with fenwick tree.

Another way is to maintain the discrete logarithm of the product, then the multiplication will be converted to addition modulo P - 1. But we need to calculate the discrete logarithm of 1, 2, ..., 300.

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

Can anyone explain the approach to calculate xi in C? Thanks a lot!

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

Can someone please help me in my C problem

49735519

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

    number b can be prime. And b * b can be > long long MAX. now=pow(k,i) in function calci is wrong

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

For D it says "To find the longest palindrome subsequence, we can make a reversed copy of the array and find LCS of it with the original array."

Sometimes strings will have multiple LCS's: some are palindromes and some are not. (For example the string "CABCA" and its reverse "ACBAC" have "ACA" as a palindromic LCS and "ABC" as a non-palindromic LCS).

How does one modify the LCS algorithm to always return a palindrome?

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

    For every non-palindrome LCS, its reverse is also a LCS. In your case, CBA is also a valid LCS. Then you can just find half of any LCS then mirror it.

    I'll put a link because it's faster than explaining. It even uses your example (lul).

    In fact you can use an average LCS tracing without modification. Just make sure you go the topmost or bottommost route in the DP table. You'll end up with some arbitrary LCS if you choose some random valid trace path.

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

      Why does tracing the topmost or bottommost path in the DP table yield the palindrome LCS?

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

xuanquang1999 I can't understand why .

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

    Let D(S) the GCD of all difference between consecutive elements in S = {s1, s2, ..., sk}. In other word, .

    Then:

    You should check out the article about Mobius inversion in the editorial (especially the first example in the article) to understand this better.

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

      Oh, I didn't notice that I should turn the formula into . Thanks a lot!

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

If the rest of problem D remains unchanged, and each operation can choose a connected block arbitrarily to change its color,then how to solve the problem?

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

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

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

For D: "Alternatively, we can also use the first solution on the compressed array, without needing the third parameter."

Can someone explain how to do it without the third parameter?

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

    After compressing, let dp(l, r) be the minimum number of moves needed to make interval [l, r] into a single component. The answer is just dp(1, m), where m is the size of the compressed array. The base case is dp(x, x) = 0 for each x,

    The key here is that after turning interval [l, r] into a single component, all the interval will always have the same color as either element l or r had initially. This gives the transition dp(l, r) = min(1 + dp(l + 1, r), 1 + dp(l, r - 1)), the former if our final step is including the l-th element into our component and the latter if it's including r. (By the observation one of those is always the last step). The exception is the case where the elements l and r are equal, in which case the answer is 1 + dp(l + 1, r - 1), because we only need one move to include both l and r into our component.

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

      hi can you please explain how monochromising the segment to its ending colors always take minimum operation,and how you got this DP recurrence idea ,my second question is bit stupid but i would be very thankful if you could answer that also.

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

49765798 Can someone tell me why is the memory limit exceeded? (Problem D)

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

    Overflow issues.The value of j might exceed maxn.

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

    Because #define int long long .

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

      Thanks!! This was indeed the problem. But why does it lead to MLE?

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

        Long long uses twice the amount of memory int does. Your solution with ints used ~190MB and memory limit is 256MB.

        You can use sizeof(int) and sizeof(long long) on your pc or in custom invocation to check this.

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

My submisstion using rand() passed just fine... I've yet to see one where I have to use mt19937 generator instead of normal one... 49774343

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

    I've expected this solution and didn't intend to kill it.

    Imagine a sequence from 0 to 106 - 1, its mean and median will be both 500000, and the standard deviation would be 288675.

    Now, imagine a list of 107 random integers generated by your algorithm. A source code might be as of following:

    Source code

    And the output:

    Result

    Pretty close to the perfect distribution.

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

I loved problem B and D

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

    B is a tricky problem.

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

    Can you explain this line Suppose squares [L, R] are flooded, then they are either of color cL or cR I m not getting it

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

      say , [L,R] flooded , no matter with which color....ok?

      if we take L-1 then new segment [L-1,R] will be flooded with the color[L-1]

      else we take R+1 then new segment [L,R+1] will be flooded with the color[R+1]

      so , no matter which one is our new segment ([L-1,R] or [L,R+1]) we always can say that our new segment [l,r] is flooded with the color[l] or color[r]

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

Can you please explain me, why this works on Python, but not on PyPy.

49779398 49779361

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

I must say that as a new programmer, the way you're written problem A and B is so clean and nice and I love it.

Most other programmers use complicated code that I feel like sometimes is there for no other reason than to show off.. I have no problem with complicated code as long as it gets explained, the weird thing is that even tutorials use complicated code that doesn't get explained..

I haven't tried other problems but I will try to do them by myself.

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

I still think that this E is wrong .-.

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

In the 1st editorial of problem D why are we always making L-R as either CL or CR can't we change the color to some other color in that Range such that the cost of operations will become minimum.

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

Hello, I have a question on problem F.

This submission is getting MLE. I think I can reduce the memory with state reduction(node.cumulfactors is removable if I change the logic a bit) but it still requires at least 200MB. My original submission is using almost 500MB while some other participants are passing this problem with less than 50MB of memory usage.

Let me say the main question. The bitmasking(to reduce memory) is mentioned on tutorial and I studied but I still cannot understand how bitmasked information can contain number of prime divisors applied so far. I think bitmasking can store only the information about the used prime divisors, not total number of each used divisors.

Can anyone explain? Thank you.

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

    You don't need the quantity of each prime factor.

    You can read the formula again, the Euler totient of the range product is in fact the range product itself, multiplied by for each distinct prime factor p appearing on the range product.

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

      How can I multiply into result since the result is represented in mod 109 + 7?

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

        Multiply the sum product by p - 1 first. then multiply the result by the modular inverse modulo 109 + 7 of p.

        Since the constraints guaranteed , the modular inverse of p is simply pφ(mod) - 1, or p109 + 5, modulo 109 + 7.

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

Imho, two problems from this contest for me seems very similar to these one: Similar to problem C, oh not similar, they are actually equal, just different constraints, but solution is the same. Similar to problem F, not so similar, but if you know solution to this one, you will easily solve F.

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

    The problem similar to F looks familiar — I found it on one Vietnamese National ICPC and take the core idea from this. However, later on thanks to cdkrot's suggestions, I increased the constraints and get rid of the multiple segment trees approach. Without that straightforward solution, it'll be quite tricky and require sufficient math backgrounds to surpass.

    About C, I have seen many others pointing the same thing. Honestly, I haven't heard of any of those references, and none of my partners told me as well. ;)

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

    Could you please elaborate on how to solve the second task?

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

      Calculate the exponential of each prime in a seperate segment tree.

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

        And how to do queries 0?

        Why does it say in the statement that P ≤ 109 and in example it's P = 109 + 7?

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

          As X <= 100, there are 25 primes up to 100. Store 25 seg trees for each prime, i-th denotes exponential of i-th prime in segment l_r. So when comes query multiply x, we just factorize x and add in segment l_r to every prime that took place in factorization. Division is the same, now we just do not add, we subtract. Calculating answer is to bruteforce all 25 primes and calculate product of primes powered to the sum of its segment tree in l_r.

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

            Nice (:

            I thought that involving fast power would lead to TLE due to that log factor, but i tried and it passed in 800ms. Thanks anyway.

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

Why my code for F has different results on codeforces and my PC?....please

https://codeforces.com/contest/1114/submission/49812712 on my PC,the results is right , but when I submit my code the output is weird... thx

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

better code for understanding problem D https://codeforces.com/contest/1114/submission/49815297

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

Problem A:My submissions were not passed until I had submited six times,I am so sad!

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

can someone explain what do we have to do in second part of second question??pleaseee

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

    Have a look to this comment — https://codeforces.com/blog/entry/65136?#comment-491791 After you have found m*k elements sum, all you need to do is traverse original array again(unsorted/input array) and print out index+1(or index if 1-indexed base arr.) if you found m elements — This step has to be done k-1 times.

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

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

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

Hey neko_nyaaaaaaaaaaaaaaaaa.

Can you please elaborate your solution of question D and explain the reason behind finding the longest palindromic subsequence and also the reason behind the following line of code:

cout << n — (dp[n][n] + 1)/2 << '\n';

Thanks in advance!

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

    Try solving this problem: given a starting square, find the fewest number of turns possible.

    Example:

    7
    4 5 2 1 3 5 4
    Choose square 4 (with color 1) as starting square
    

    The answer is 4 turns.

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

      neko_nyaaaaaaaaaaaaaaaaa I do not have a problem finding the long palindromic subsequence but can you please tell that why are we actually finding it and also why after finding it

      |L| + |R| - LCS(L, R)

      is providing us the answer.

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

Problem C:

r=min(floor(xi/yi)) where i=1 to m

Why does it work?

any proof?

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

    Basically you are checking if given set of prime frequencies of 'b' exists in the same prime set frequencies of n. If "yes" increase result by 1, and decrease all the frequencies of prime factors of n by the corresponding freq. of b prime factors. Repeat the process, until you dont found such, then return result. This is similar to finding min(floor(xi/yi)) where i=1 to m.

    Idea is basically to check if (n!%b == 0) repeatedly (by doing n!/=b), so we just check prime factors of b and their frequencies in prime factorization of n!.

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

my solution on F is exactly same with the Tutorial but i get TLE,what's the problem of my code ?

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

Problem F , Is it many time-complexity difference recursive power function and bottom-up power function? First, I used recursive power function. but got TLE. so, i saw another people's codes, they used bottom-up power function. i fixed my power function, and i got accepted.

TLE : https://codeforces.com/contest/1114/submission/49850747

ACCEPTED : https://codeforces.com/contest/1114/submission/49852069

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

    In fact recursive function is somewhat (significant enough) slower than normal function. You can compare between 49740091 {non-recursive} and 49863959 {recursive}.

    Still, the calculations can be quite faster if all integers are stored in int and casted into long only before multiplication.

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

Can someone tell the explanation of first proposition in Problem C?

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

    Let say that x=n! is some number with zeroes in the end.

    Trim all those zeroes and call new number y.

    Then, x = y*(b^r), — here b and r is from editorial and r is number of zeroes in the end, because b^r is 1 and r zeroes in the end similarly to decimal numbers.

    Fundamental theorem of arithmetic says that there is unique factorization of any number.

    Thus, x has b^r in factorization.

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

in F problem you have missing powers for primes in place where you proving that answer is n*product.

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

    Can you please specify the error location?

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

      sorry, I'm wrong. I thought ai are primes, thus, they should have powers. but in text it's values from array.

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

In the tutorial of Problem C: Initially, denote xi = 0. **** Repeatedly do the following: add to xi, then divide n by pi. The loop ends when n is zero. When i want to find each xi,why do this?

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

    In n! exactly numbers are divisible by pk but some of them are also divisible by pk + 1 and so on. If you consider how many times you will count numbers that is divisible exactly by pk you'll see that they are included in all with j up to k. So, you'll count p exactly k times for this numbers. And using property that floor(n/p^k) = floor(floor(n/p)/p) you get repeated division by p.

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

in problem B, how to find partions??

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

    consider largest m*k numbers. mark them with 1 in some additional array. pick first m numbers and assign them to first segment. pick second m numbers and assign them to second segment. and so on. you'll get exactly k segments — obviously. none of segments can be smaller than m, because they all of them contain at least m numbers. all you need to do now is to cover gaps. you can do that by joining gaps to any segment nearby. other easier way to make partitions: just make cut each time when you reach m numbers in segment, and then reset counter of numbers you picking (marked ones) to zero.

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

Getting a strange compilation error("Compiled file is too large [53650389 bytes], but maximal allowed size is 33554432 bytes") in problem F. How to overcome it ?? My code https://ideone.com/5ijuXg

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

This was a really great contest by talented authors ! I learnt a lot solving problems of this contest.

I actually found the B more challenging than the C.

D was a very nice DP. I noticed the editorial used a 'forward-dp'. I tried understanding the transition and writing a 'backward-dp' so that I can understand it wel E was a good interactive problem.

F was a really good question. First, noticing that you do not need to know the exponent of each prime in the product, just their list. And then maintaining a product segment tree and a bitmask OR tree. Amazing !

Here are my solutions :)

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

Hello. I have a question about problem D

I made a testcase below

5

5 6 5 7 5

I think the answer is 2 ( [5 6 5 7 5] -> [5 5 5 7 5] -> [5 5 5 5 5] )

but, the output of both solution is 3.

Please anybody correct me

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

    All transformation must include one cell being chosen as the starting cell.

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

      Oh. I miss that condition.

      Thank you!

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

      Can I have one more question??

      I just read problem D one more time.

      But, I don't understand well

      My question is here

      In testcase [5 6 5 6 5], output is 2

      In testcase [5 6 5 7 5], output is 3

      What difference??

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

        Keep in mind that, each time you change the color of the initial cell, you change the color of all reachable cells having the same color as it as well.

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

          Thank you!

          I appreciate your kind explanation

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

AkiLotus you wrote in problem E's editorial: d will be calculated as greatest common divisors of all difference between all consecutive elements, provided all elements found (yes, including the max one) is kept into a list sorted by increasing order. Won't the probability of success be higher if we obtain the numbers and rather than taking the gcd of only the difference of consecutive elements take the gcd of absolute difference between all pairs of them ?

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

    Both approaches literally yield the same value. This is due to the nature of Euclidean algorithm in finding GCD.

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

      Can you provide any proof or references maybe ? I was thinking for some case like when the elements are a+d, a+2d, a+5d, taking adjacent elements' difference will yield d and 3d only whereas taking all pairs will yield d, 3d and 4d. I know the elements in the second list will be linear combinations of elements in the first list. So, is that somehow the reason that they don't add much value or what ?

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

        You can refer to the Euclidean algorithm here.

        The Euclidean algorithm is based on the principle that the greatest common divisor of two numbers does not change if the larger number is replaced by its difference with the smaller number.

        Thus, the linear combinations of a set of number yield the same GCD as that set alone.

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

the problem C gives correct answer in geeks-ide (link is given below) for the 7th test case but wrong answer in codeforces not sure why::

https://ide.geeksforgeeks.org/NY6e3NArcO

https://codeforces.com/contest/1114/submission/50059493

7th test case input : 1000000000000000000 97

expected output: 10416666666666661 (i am getting this in geeks ide)

wrong output in codeforces: 10416666666666639 (not sure why )

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

    This is one of the tests specifically designed to counter solutions with poor overflow handle.

    Take a look at this part of your code:

    while( mul <= n ) {  
        ncnt += (n/mul) ;
        mul *= i ;
        if( mul < 0 ) break ;
    }
    

    mul might even breach the limit of int64.

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

In problem C, i have found some incorrectly index from editorial:

The process of finding p1, p2, ..., pn, y1, y2, ..., ym can be done by normal prime factorization of the value b.

the pi is the prime factor of b. So, the last boundary index. it's should be pm not pn.

however, it's not importance more. if you have understood in idea.

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

Please help me in understanding question D. Testcase: 8 4 5 2 2 1 3 5 5

Why is the answer for this 4? I think it should be 5. Where am I wrong? My procedure if I take starting square as 5: 4 5 2 2 1 3 5 5 4 2 2 2 1 3 5 5 //Step 1, 4 4 4 4 1 3 5 5 //Step 2, 1 1 1 1 1 3 5 5 //Step 3, 3 3 3 3 3 3 5 5 //Step 4, 5 5 5 5 5 5 5 5 //Step 5,

I didn't understand the explanation they gave in the question. Explanation: In the second example, a possible way to achieve an optimal answer is to pick square with index 5 as the starting square and then perform recoloring into colors 2,3,5,4 in that order.

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

    You're taking the 2nd element (with color 5), not the 5th element.

    The most optimal way is as following:

    4 5 2 2 1 3 5 5
    4 5 2 2 2 3 5 5 // step 1
    4 5 3 3 3 3 5 5 // step 2
    4 5 5 5 5 5 5 5 // step 3
    4 4 4 4 4 4 4 4 // step 4
    
    • »
      »
      »
      5 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Oh thanks I got it. I thought we have to stick to a connected component but seems like we can switch between them.

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

        No, we still have to stick into one connected component. As you can see all segments I performed color change included the 5th element (yet with different color).

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

For problem 'D', the claim, "Suppose squares [L, R] are flooded, then they are either of color cL or cR", I've been trying for some time to prove this claim using Mathematical Induction, but I'm unable to do it, and although it seems somewhat intuitive, it'd be great if someone can provide a proof for it. Thanks! cdkrot neko_nyaaaaaaaaaaaaaaaaa

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

    The claim is pretty much self-explanatory.

    If [L, R] is already connected at the initial state, then the claim is obviously true.

    Let's denote the starting cell's index as i (L ≤ i ≤ R).
    If [L, R] are flooded, then before the last flooding there exists two components [L, x] and [x + 1, R] (L ≤ x < R). Consider the two cases:

    • [L, x] is not flooded~--- which means x < i. To make the entire range [L, R] flooded, since we can only access the component containing the i-th cell, we must change the color of that segment into the color of [L, x]. Such segment was completely untouched at first, thus the target color will obviously be cL.
    • [x + 1, R] is not flooded~--- which means i ≤ x. Using similar explanation, we can see that the color of the segment containing the initial cell must be changed into cR.
    • »
      »
      »
      5 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Sorry, I couldn't understand the proof properly. "Let's denote the starting cell's index as i", What is starting cell here?

      Also, suppose the segment contained within [L, R] is: 1 2 2 3 3 3 4. Here we can do: 1 2 2 3 3 3 4 ---> 1 3 3 3 3 3 4 ---> 3 3 3 3 3 3 4 ---> 3 3 3 3 3 3 3, which is also optimal, and the resulting color is not 1 or 4.

      If this segment contained within [L, R] was surrounded with lots of 3's on either side, then shouldn't we solve [L, R] this way, where we do not flood [L, R] with neither CL or CR? Or am I completely missing out on some point here?

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

        Yes, you are missing out on some point. Please read the problem statement carefully.

        The statement clearly says pick any square at the start of the game. Then you can only change the component containing that square. Thus, your 3rd move is not valid.

        I see a lot of people have trouble with this. Maybe I should've highlighted something. I apologize for this.

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

Soln got accepted when i did 2 changes in tutorial's soln 1. I think we don't need to include Max in List, after sorting bcoz rest of the element would decide the value of d soln without including -- got accepeted 2. We only need series from 0 to +1000000000 bcoz numbers in AP can be possible from 0 to +100... not from -100... to 100... it got accepted using series from 0 to 100... correct me, if i'm wrong -- it would help me & other understanding the concept more better

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

    Yeah, you can exclude Max from that part. The probability to get incorrect answer would be increased a little bit, but still insignificant to cause an issue for normal upsolving. ;)

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

For those not getting mistake in Div2 D, try this testcase- 7 4 3 4 2 5 3 5 answer should be 3 upvote if you r getting 5

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

can someone explain recurrence relation for first solution for prob D ?

»
9 months ago, # |
  Vote: I like it 0 Vote: I do not like it

ok