Блог пользователя ajit

Автор ajit, история, 4 года назад, По-английски

We hope that you have enjoyed this round! Here is the sketch of solutions for the problems.

A. Array and Peaks

  • Problem Setter: ajit
  • Editorialist: ajit
Tutorial
Implementation in C++

B. AND Sequences

  • Problem Setter: ajit
  • Editorialist: ajit
Tutorial
Implementation in C++

C. Add One

Tutorial
Implementation in C++

D. GCD and MST

  • Problem Setter: ajit
  • Editorialist: ajit
Tutorial
Implementation in C++

E. Cost Equilibrium

Tutorial
Implementation in C++

F. Swapping Problem

Tutorial
Implementation1 in C++
Implementation2 in C++
  • Проголосовать: нравится
  • +153
  • Проголосовать: не нравится

»
4 года назад, # |
  Проголосовать: нравится +1 Проголосовать: не нравится

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

»
4 года назад, # |
  Проголосовать: нравится +41 Проголосовать: не нравится

Thank you for the contest, I really enjoyed it.

»
4 года назад, # |
  Проголосовать: нравится +22 Проголосовать: не нравится

In problem C, I faced lot of issues when I did not use fast input "ios_base::sync_with_stdio(false); cin.tie(NULL);"

112714905 — here I took input number as a string (to iterate over it's digits) and even when I used integers only (112715192) even then also, It got TLE verdict.

Has it become mandatory to use FAST I/O to pass the verdict now?

»
4 года назад, # |
Rev. 4   Проголосовать: нравится +10 Проголосовать: не нравится

Solved C by direct simulation with pre-processing

Basically just brute-force the numbers gotten when you add 1 to every digit starting from 0. The numbers can't be stored because they get large very quickly. To fix this, store the count of each digit in the current number in an array instead and update the array to the next number after applying the operation accordingly. The sum of digits of the current number(we only care about the sum) are stored in a vector for pre-processing. Then for each digit of n, apply the operation m times, which is done in O(1) because of pre-processing.

Total time Complexity: O(t*number of digits in n)

https://codeforces.me/contest/1513/submission/112728116

Lol, the updating can be done with only one array by updating backwards. Was too braindead to realize...

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    Hi, I tried a similar solution, and as far I understand, I shouldn't be getting a TLE, but I still get one on test case 3, could you kindly tell me what is missing in this? https://codeforces.me/contest/1513/submission/112745133

    • »
      »
      »
      4 года назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      As I can see, you are are simulating the whole procedure in each test case. This would take at max (no. of test case)*(m) i.e., 4e10 (worst case) operations which would obviously give TLE. Instead store answer for each digit from 0....9 and for each value of m using simple DP, before the test cases and then access them in O(1) in each test case.

      • »
        »
        »
        »
        4 года назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        Yeah, usually I consider each test case as an isolated environment. Thanks for the reply, I shall try it out.

        • »
          »
          »
          »
          »
          4 года назад, # ^ |
            Проголосовать: нравится 0 Проголосовать: не нравится

          Think, If you have digit D and you want to increment it let's say X times then how much length it will increase. You can store it in table[D][X](Try to Precompute this table). If you don't get the solution, then you can see mine.

»
4 года назад, # |
  Проголосовать: нравится +28 Проголосовать: не нравится

F can be solved using an $$$O(n^2)$$$: 112699481.

»
4 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

In the explanation of Problem B, Why do bi needs to be super mask of b1?

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    I'll explain my thought process maybe it will help you.

    When $$$i=1$$$ only $$$B_1$$$ will be on the left side. And when $$$i=n-1$$$ only $$$B_n$$$ will be on the right side. Therefore it's clear that $$$B_1=B_n$$$ condition must hold.

    For $$$2 \leqslant i \leqslant n-2$$$ there will be some mid elements on the left and some will be on the right. But in every case their bit-wise AND should be equal to $$$B_1$$$ (or $$$B_n$$$ as they are equal). TO keep the bit-wise AND constant,

    • $$$B_i$$$ needs to have all the set bits of $$$B_1$$$. If not the new AND will have some bits set to zero where it wasn't before.

    • What they have on other bits doesn't matter because in $$$B_i$$$ and $$$B_n$$$ those bits are set to zero and $$$B_1$$$ is always on left and $$$B_n$$$ is always on right. Thus their AND will be zero anyway

    This means that $$$B_i$$$ has to be a super mask of $$$B_1$$$

    • »
      »
      »
      4 года назад, # ^ |
        Проголосовать: нравится +1 Проголосовать: не нравится

      Sorry I still don't get it.

      I understand why $$$B_i$$$ has to be a super mask of $$$B_1$$$.

      But why choosing two of the minimum of the array $$$a$$$, and the rest of the array will automatically meet the "super mask" requirements?

      For example, a = [2,4,2]. min(a) = 2(and count(2) >=2), but 4 & 2 != 2

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится +3 Проголосовать: не нравится

    So that then only prefix and operation and suffix and operation for all i from 1 to n-1 will hold good , as long as those minimums are present at 1 and nth position

»
4 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

for question F:

i guess this is correct: first : let vc[i] = {b[i] — a[i], i}; second: sort vc; ans = sum = segma(llabs(vc[i].first)); third: enumerate vc[0].second whith i from 1 to n, let ans = min(ans, sum — diff); last : enumerate vc[n — 1].second whith i from 1 to n, let ans = min(ans, sum — diff);

it can pass all test, but i cannot prove it, who can give a prove?

submit code: https://codeforces.me/contest/1513/submission/112733065

»
4 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Can someone tell me what is wrong in this submission for question B 112733641

»
4 года назад, # |
  Проголосовать: нравится +7 Проголосовать: не нравится

Great contest.

»
4 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

In Editorial of D — why is checking if the new_element has edges enough to determine if a cycle will be formed?

  • »
    »
    4 года назад, # ^ |
    Rev. 2   Проголосовать: нравится +11 Проголосовать: не нравится

    Hi, I have updated the solution slightly. Please have a look at it. We are spanning to left and right. Suppose we are currently spanning right and currently encountered a new_element which has an edge. First observation is that this edge is between new_element and an element with another index greater than it. Now what I meant is that, we will connect the edge between g and new_element and stop spanning in this direction (here right). This is because we just connected our current span group (current connected component) with some previous span group (another connected component) right? Any further entry we consider in this direction will form a cycle. You can have a look at the code for further understanding.

    • »
      »
      »
      4 года назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      Ya ,now I get it. Thank you

    • »
      »
      »
      3 года назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      Hi. I implemented this solution to end up getting WA. My approach is similar. Just going left and right to each index 'i' as long as I have multiples of a[i] (which would mean a[i] is the gcd) And for edge I am just taking min(a[i], p). Not able to find a case where it would fail. Could you check if you can figure out what's wrong. Also comp is the number of connected components. So at the end I am adding (comp — 1) * p to the ans. https://codeforces.me/problemset/submission/1513/137968697 I know this is an old problem but see if you can recall.

»
4 года назад, # |
  Проголосовать: нравится +3 Проголосовать: не нравится

For problem C

I use A[i] to represent the occurrences times of number i. So we can construct anonther matrix B to represent the operation and we can multiply B m times to get the answer.

My problem is that can I improve the complexity which is O(t * 10^3)[10 is the size of B] because B is a sparse matrix.

The test code is https://codeforces.me/contest/1513/submission/112741598

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    Probably a little late response — I did the same approach.

    Submission 115852637 is my Time Limit Exceeded on test 2.

    The simple fix was to first sort all the queries by $$$m$$$ increasingly, and update only by the power of difference (ie. keep the previous matrix power used in a variable and multiply by difference in $$$m$$$ between queries; in other words, if previous matrix was $$$P = M^{m_1}$$$ you just multiply it by $$$Q = P \cdot M^{m_2 - m_1}$$$). Lastly, just sort queries back to their original order and output the answer.

    Submission 115853823 is where I applied this optimization and it worked :D

»
4 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Great contest with awesome problemsets.

Problem C just refreshed my memory of dynamic programming.

I solved problem C using two dimensional dp table using memoization approach.

My submission 112676785

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    can you explain the recurrence relation?

    Isn't dp[i][5]=dp[i-1][4] true? Why is it dp[i-1][6]? pls explain

    • »
      »
      »
      4 года назад, # ^ |
      Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

      dp[i][j] is defined as if we has number i and we apply exactly j moves than what is the total number of digits in the lastly generated digit.

      if i-1 is not 9 :

      than you can see that number of digits in(i-1)=number of digits in (i)

      so we can say that if i-1 is not 9 than applying one operation does not increase number of digits

      this implies dp[i-1][j]=dp[i][j-1]

      applying above thing dp[i-1][6]=dp[i][5] if i-1 is not 9

      and dp[i][5]=dp[i+1][6] if i is not 9

      so this clearly answers your query that dp[i][5] isn't dp[i-1][4] and dp[i][5]=dp[i-1][6] with constraints on values of i.

»
4 года назад, # |
  Проголосовать: нравится +51 Проголосовать: не нравится

My understanding for F:
1. Plot all A_i and B_i on a number line.
2. If A_i < B_i, write a right arrow A_i -> B_i.
3. If A_i > B_i, write a left arrow B_i <- A_i.
4. The answer is original_answer — 2*(the maximum length of overlapping two opposite direction arrows).
(Continue to the editorial)

By the way, thanks for the great contest!

»
4 года назад, # |
Rev. 2   Проголосовать: нравится -13 Проголосовать: не нравится

...

»
4 года назад, # |
  Проголосовать: нравится +18 Проголосовать: не нравится

In problem E, how to prove that array will be balanced when this condition holds true:

All the source vertices are before the sink vertices in the permutation

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится +3 Проголосовать: не нравится

    I would also like to see prove that there aren't any other conditions.

  • »
    »
    4 года назад, # ^ |
    Rev. 3   Проголосовать: нравится +10 Проголосовать: не нравится

    I accidently found the proof when I was trying to prove it wrong. So here it is:
    Lets denote $$$U_i$$$ as some value needed at some sink and $$$V_j$$$ as some extra value we can take from some source. Lets say positions of sinks are $$$x_1, x_2, ..., x_m$$$ and positions of sources are $$$y_1, y_2, ..., y_n$$$. Lets say $$$y_j > x_i$$$ for $$$1<=i<=m$$$ and $$$1<=j<=n$$$. Lets denote $$$w_{ij}$$$ as some value we take from some source $$$i$$$ and give it to some sink $$$j$$$. So,
    $$$cost = y_1*(w_{11}+w_{12}+...+w_{1m}) - x_1*w_{11} - x_2*w_{12} - ... - x_m*w_{1m} + $$$
    $$$ y_2*(w_{21}+w_{22}+...+w_{2m}) - x_1*w_{21} - x_2*w_{22} - ... - x_m*w_{2m} + $$$
    $$$.$$$
    $$$.$$$
    $$$.$$$
    $$$y_n*(w_{n1}+w_{n2}+...+w_{nm}) - x_1*w_{n1} - x_2*w_{n2} - ... - x_m*w_{nm}$$$

    Now $$$V_a = w_{a1}+w_{a2}+...+w_{am}$$$,

    So
    $$$cost = y_1*V_1 - x_1*w_{11} - x_2*w_{12} - ... - x_m*w_{1m} + $$$
    $$$ y_2*V_2 - x_1*w_{21} - x_2*w_{22} - ... - x_m*w_{2m} + $$$
    $$$.$$$
    $$$.$$$
    $$$.$$$
    $$$y_n*V_n - x_1*w_{n1} - x_2*w_{n2} - ... - x_m*w_{nm}$$$

    And $$$U_b = w_{1b}+w_{2b}+...+w_{nb}$$$,

    So finally,
    $$$cost = y_1*V_1 + y_2*V_2 + ... + y_n*V_n - x_1*U_1 - x_2*U_2 - ... - x_m*U_m$$$

    Now, we can see that cost doesn't depend on $$$w_{ij}$$$ for any $$$i$$$ and $$$j$$$. So cost is constant no matter how we transfer values from sources to sinks. Hence proved.

    • »
      »
      »
      4 года назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      Thanks for the detailed proof! However, I think this covers only one part of the problem, i.e. proving that sinks-after-sources or sources-after-sinks will always have a unique cost. Can you formally prove that permutations other than sinks-after-sources/sources-after-sinks will never have a unique cost? I could only come up with a very informal, rather intuitive proof.

      I feel this should have been covered in the editorial itself, instead of plainly writing down the formulae. On another note, shout-out to liouzhou_101's amazing, detailed af editorial of Round #700's Problem D/B1.

      • »
        »
        »
        »
        4 года назад, # ^ |
        Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

        First we come up with a conjecture, then we try different examples so the conjecture is not wrong and then we move to the proof. So if you can think of an example to prove it wrong then you don't need to prove.

    • »
      »
      »
      3 года назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      Very Nice Mathematical proof!!

»
4 года назад, # |
  Проголосовать: нравится +15 Проголосовать: не нравится

PermutationForces

»
4 года назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

Would someone mind to explain to me why my submission for C got TLE? Its time complexity is O(10 * m), so I thought it would pass under 1 second. My submission 112747375

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    Indeed complexity is $$$O(10 * m)$$$. But there are $$$2 * 10^5$$$ test cases as well. So, final complexity is

    $$$O(10 * m) * 2 * 10 ^ 5$$$

    which will surely TLE

    • »
      »
      »
      4 года назад, # ^ |
        Проголосовать: нравится +5 Проголосовать: не нравится

      My bad, I thought the sum of m over all test cases does not exceed 2e5. This time there is no this line, thank you for pointing it out xD

»
4 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

length would be the sum of i−9 operations and i−10 operations, why? Problem C

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится +6 Проголосовать: не нравится

    If you look carefully every single digit from "1 to 8" produce one new digit after 10 moves, expect "9" which produce two new digits. Thus, number of digits doubles up after 10 moves, if they don't have "9" in it... we need to add the digits contributed by "9", this is easy, number of 9 in number $$$x$$$ $$$=$$$ total no of digits in $$$x + 1$$$ — total no of digits in $$$x$$$

    $$$dp[i] = 2 * dp[i - 10] + dp[i - 9] - dp[i - 10]$$$

    which is equals to

    $$$dp[i] = dp[i - 10] + dp[i - 9]$$$

    • »
      »
      »
      4 года назад, # ^ |
      Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

      also can you please explain why dp[i] is taken as the length to the operation applied to number '10'?why are we considering only 10?

      I got it.Thanks :)

»
4 года назад, # |
  Проголосовать: нравится +8 Проголосовать: не нравится

Problem C can be solved Using a Simple 2D DP

where DP[digit][times] indicate the length of digit after times number of operations and digit E (0, 9)

and the answer for a query like 1057 16 would be dp[1][16] + dp[0][16] + dp[5][16] + dp[7][16] and take MODULO'S AS NEEDED

heres the code https://codeforces.me/contest/1513/submission/112697582

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится +1 Проголосовать: не нравится

    That is one of the cleanest dp approach one can think of. Definitely beats the editorial.

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится +2 Проголосовать: не нравится

    Way better than the editorial which has no explanation.

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится +1 Проголосовать: не нравится

    your dp approach is easy to visualize as compared to the editorial's thanks for sharing :)

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    can you explain the recurrence relation?

    Isn't dp[i][5]=dp[i-1][4] true? Why is it dp[i-1][6]? pls explain

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится +6 Проголосовать: не нравится

    Can be converted to 1D dp as each digit can be brought up to 10. So, just calculate for dp[10][times]

    • »
      »
      »
      4 года назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      Can you explain how the dp relation i-9 and i-10 for dp[i] as in editorial.

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    Hey, your solution is great. Can you elaborate more on the last else statement when we are dividing $$$N$$$ in $$$0$$$ and $$$1$$$ .
    Is it due to the fact that we are calculating dp from small digits to the large, As at that time we would have already calculated dp for smaller no. ?
    Like if we say we take $$$N = 11$$$ or more.. then its not gonna divide in 1 and 0. I think, got the intiution. But can you confirm the above logic ?

    • »
      »
      »
      4 года назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      thats because of this if n==8 then in next step it only becomes 9 hence dp[n][times] = dp[n+1][times-1]

      but for n==9 it becomes 10 (1 and 0) hence dp[1][times-1] + dp[0][times-1]

      observe how after 9 dfs now solves for dfs(1, times-1) and dfs(0, times-1) so dfs(n, times) n would only be from 0 to 9 and would never exceed 9 and hence no need to solve for n==10 and n==11 as these are non existent.

      cheers

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    absolute beauty , same approach just missed % mod to add , thanks dude!!!

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится +1 Проголосовать: не нравится

    broooo that was neat!!!

»
4 года назад, # |
Rev. 2   Проголосовать: нравится +1 Проголосовать: не нравится

My idea for problem B:
So we have to find some elements that we can put at start & end. We will check something for all elements bit by bit. Let's say we have a set which contains potential candidates for start & end and we have to remove some candidates. And we are checking for $$$kth$$$ bit. There are three cases possible:
1. All elements have $$$kth$$$ bit set.
2. All elements have $$$kth$$$ bit unset.
3. Some elements have $$$kth$$$ bit set and some have unset.
Now for case 1 & 2, we have to do nothing. And for case 3, we can see that we can't let the first element of permutation have $$$kth$$$ bit set and similarly for last element. So we will remove all the candidates which have $$$kth$$$ bit set. We will do this for all the bits. Note that we have all the elements from $$$1$$$ to $$$n$$$ in our set initially. After doing this for every bit, lets say size of our set is $$$x$$$.
So, for choosing and permuting first & last element we have C(x,2)*2 ways. And permuting the remaining elements will give us (n-2)! ways. So total ways: C(x,2)*2*(n-2)!
My submission

»
4 года назад, # |
  Проголосовать: нравится +1 Проголосовать: не нравится

it lookes like codechef helped our problem maker. lol

»
4 года назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

Ignore.

»
4 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

In problem E "ways filling (n−src−snk) identical values in (src+snk+1) places)" should be equal to Binomail(src+snk+1,n-src-snk). But it is getting me wrong answer but when i use Binomial(n,src+snk) my got accepted. Can anybody explain?

»
4 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

The complexity of E should be O(nlogn) because of when we count the frequencies of every number cost us nlogn times(using the STL map or just sort).

»
4 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Guys, What does super mask means??

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится +4 Проголосовать: не нравится

    Assume that there are two numbers A and B such that B is a submask of A. Then in such case A is a supermask of B.

    Generally speaking, a supermask of a number is a number of which current one is a submask.

»
4 года назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

simple solution for problem D

this is what i have done
Maintain a list of edges with there weight

first push n-1 edges between neighboring nodes
(For Ex) a1 a2 s3 a4 (given array)
then push (1,2,p) (2,3,p) (3,4,p) in our edges, where first 2 numbers are indices and last one is weight

the observation is GCD(in a range) == min (in a range)
if an only if min divides every other value in the range

For example say one of the subarray is this
9 6 3 3 9 6 array
1 2 3 4 5 6 ind
then min is 3 and it divides every element
so push(3,1,3) ## here 3 is the index of element 3 and 1 is index of 9 and last 3 is (gcd weight)
and push(3,2,3) (3,4,3) (3,5,3) (3,6,3)
what my code would do is push them in 2 iterations in forward iteration push (3,4,3) (3,5,3) and (3,6,3) and in backward iteration push the rest

Originally u had n-1 edges this forward and backward iteration may push 2*(n-1) edges more so total 3*(n-1) edges<br?

now since u have O(n) edges apply any Min Spanning Tree algorithm

cheers

https://codeforces.me/contest/1513/submission/112722912

»
4 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

I can't understand why the number of edges considered in problem D would be O(N) ? We are also rejecting the edges forming a cycle which takes O(1) time with DSU. Can somebody please help me understand.

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    So are you referring to my code.

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится +3 Проголосовать: не нравится

    since total edges already there is n-1 then in 2 (forward and backward iterations i at most push (n-1)edges in each iteration)

    total 3*(n-1) which is O(n) edges for cases like
    10 10 10 10 10 (array) (total edges will be 3*(5-1) i guess)
    u can simply print edg to verify

    now Coming to DSU Part Just Search Minimum Spanning Tree algorithm based on UnionFind on youtube.

    I hope this clears it.

»
4 года назад, # |
  Проголосовать: нравится -19 Проголосовать: не нравится

Simple Solution Problem B

What is asked in the problem is this
Count all permutation such that binary and of any prefix would match the binary and of remaining suffix

For Example
Say array elements are A1 A2 A3 A4 A5 A6 A7
so lets say one of the prefix is A1 A2 A3
then A1 & A2 & A3 must equal A4 & A5 & A6 & A7
this must be true for all prefix and resulting suffix with length greater than 1

lets just call them prefAnd and SuffAnd
since prefAnd == SuffAnd
Therefore prefAnd & SuffAnd == prefAnd == SuffAnd since and of 2 same numbers is the same number
but prefAnd & SuffAnd == FUllArrayAnd

so just find the all array and; say this comes out to be A

now every permutation must begin and end with A

since there can be prefix of size 1 and this must equal prefAnd == A
similary there can be a prefix of size (n-1) leaving suffix of size 1 and this has and values as SuffAnd == A

now you can show that any arrangement in the middle cannot affect any prefAnd and SuffAnd

For Example given array 1, 3, 5, 1
whole array and is 1 and hence it should always begin and end in 1
and middle part can be juggled as you wish giving (n-2)! for the mid

Proof of why the prefAnd wouldn't change observe any prefAnd >= wholeArrayAnd (since a&b <= min(a,b) and just generalise this by taking more numbers)
NOTE: WHOLE ARRAY AND IS THE SMALLEST NUMBER IN THE ARRAY (IF NOT PRINT(-1)) WE WILL USE THIS FACT LATER.

observe only 1&1 yield rest (0&1 0&0 1&0) yield zero
lets look at our example
001 (1)
011 (3)
101 (5)
001 (1)
001 (whole array and) (observe if ith bit is on here then it is on in every number)
so all the numbers have atleat all the bits on as in (A or wholeArrayAnd)

now suppose 1 3 1 5 is given
we arrange 1 number as 1
1 _ _ _
now suppose we put 5
then 1&5 will always be (A == 1 == wholeArrayAnd)

since both 1 and 5 will have all the bits open as in wholeArrayAnd the And will atleast be WholeArrayAnd or A
so FirstElement & SecondElement >= WholeArrayAnd (Eq 1. )

again since a&b <= min(a, b)
so (FirstElement & SecondElement) <= min(FirstElement, SecondElement) = FirstElement = WholeArrayAnd
(Eq. 2)
(since FirstElement == WholeArrayAnd and WholeArrayAnd is the smallest)

from equation 1 and 2 we have FirstElement & SecondElement = FirstElement
so that does it for any 2 elements at the beginning

for 3 elements just replace the first 2 elements by their binary and which again is FirstElement as shown and case of 3 elements at the beginning will be the same as case of 2 elements.

Here you go prefAnd would never change irrespective of arrangement in the middle.

if Prefand doesnt change then suffAnd wouldn't change either as wholeArrayAnd is constant.

so arrange at begging and end is C(A, 2)*2 (multiply by 2 since we are arranging indices and not element values) and middle permuation will be (n-2)!

Answer is 2*(A,2)*(n-2)! % MOD;

cheers

  • »
    »
    4 года назад, # ^ |
    Rev. 4   Проголосовать: нравится 0 Проголосовать: не нравится

    Here is my code

    Spoiler
  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    Don't know why you got so heavily downvoted. Your explanation was very clear!

»
4 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Please help regarding my submission of problem c.. my submission is https://codeforces.me/contest/1513/submission/112704547 looking at the time complexity it looks fine to me... please explain if any changes are required

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится
    ios_base::sync_with_stdio();
    

    Maybe you mean:

    ios_base::sync_with_stdio(0);
    

    Actually the first one is as same as none. It got TLE because of slow IO.

»
4 года назад, # |
  Проголосовать: нравится +19 Проголосовать: не нравится

I did really bad during the contest.

I got wrong attempts in every problem of the first three ones, and I did't solve D in the contest.

IMG_2268.png

»
4 года назад, # |
  Проголосовать: нравится +7 Проголосовать: не нравится

Problem D is just amazing. Tried something with 3 segment trees, binary search and lazy propagation but did not work out. The solution idea in the editorial is really cool. I wonder how you thought about such a problem idea xD.

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    Hey can you elaborate on the editorial? I am still trying to upsolve this.

  • »
    »
    4 года назад, # ^ |
    Rev. 2   Проголосовать: нравится +3 Проголосовать: не нравится

    I had the same idea and managed to implement it during the contest (Instead of 3 segment trees, I used 2 sparse tables and 1 segment tree) Code

    I felt really stupid after reading the editorial xD and felt I did a really big overkill

»
4 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

For $$$D$$$, why does the following approach give wrong answer
1. Join $$$i$$$ and $$$i - 1$$$ with edge weight of $$$p$$$ for all $$$2 \leq i \leq n$$$
2. Pick up the minimum unvisited element, call it $$$minVal$$$ at $$$minIndex$$$. Start from this value and do dfs to the neighbours if and only if they are multiples of $$$minVal$$$. Connect $$$minIndex$$$ to this vertex, with edge weight $$$minVal$$$. Repeat for the next unvisited minimum
Now, run "kruskal-algorithm" on this graph to find the minimum spanning tree. Here is my submission

»
4 года назад, # |
  Проголосовать: нравится +1 Проголосовать: не нравится

Such a pathetic way to write problem description for Problem B.

»
4 года назад, # |
Rev. 6   Проголосовать: нравится +14 Проголосовать: не нравится

I find a different way of problem C, link it to Pascal triangle, but unable to implement it, my submission failed testcase n = 98, m = 100, see the picture of my approach and the wa submission : https://codeforces.me/contest/1513/submission/112781701 . I would appreciate it if anyone could help! 714C.jpg

»
4 года назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

Problem E was really fun to solve! Thank you.

»
4 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

In problem E, can somebody prove the 3 and 4 or explain why these conditions are sufficient ?

»
4 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

For problem E, shouldn't it be $$$Binomial(n, src + snk)$$$ in the third to last line of the editorial? (Since we are using stars and bars basically?)

»
4 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Can anyone please explain (cnt⋅(cnt−1)⋅(n−2)!)%(109+7). Why do we have to do this?

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    It's the number of ways to choose 2 elements among those who have minimal value which need to be at the start and the end of the array multiplied by the permutation of the other $$$n - 2$$$ elements. In other terms, we're doing: $$$Binomial(cnt, 2) * 2 * (n - 2)!$$$, where $$$cnt$$$ is the frequency of the smallest element.

»
4 года назад, # |
Rev. 2   Проголосовать: нравится +8 Проголосовать: не нравится

nice contest.

»
4 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

in editorial of problem E how did we get C=(# ways filling (n−src−snk) identical values in (src+snk+1) places) = Binomial(n,src+src) could someone explain or elaborate?

  • »
    »
    3 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    See bars and stars:

    To place N identical values into K places (with potentially multiple items in each place), you think of it as partitioning the N items into K places using K-1 partitions. So think of placing N items along with (K-1) partitions down in a row, which is N+K-1 total things. You'll choose the spots of the K-1 partitions, and there is: Binomial(N+K-1, K-1) ways to do it.

    Then plug in N = n-src-snk, and K=src+snk+1

»
4 года назад, # |
  Проголосовать: нравится +13 Проголосовать: не нравится

Alternative solution for 1513F - Swapping Problem: let $$$i$$$, $$$j$$$ be the indices of the swapped elements. Fix the signs of $$$a_i - b_j$$$ and $$$a_j - b_i$$$. With a merge sort tree (or a BIT of vectors), you can find, for each choice of the signs and for each $$$i$$$, the valid $$$j$$$ and, among them, the optimal one (the one that maximizes $$$|a_i - b_i| + |a_j - b_j| - |a_i - b_j| - |a_j - b_i|$$$). Complexity: $$$O(n \log^2(n))$$$.
113102167

»
4 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Problem C can probably be in O(logm) time. It is matrix powers.

»
4 года назад, # |
  Проголосовать: нравится +3 Проголосовать: не нравится

While trying to hack solutions in 1513F - Swapping Problem, I got a number of unexpected verdicts. From what I understand, it happens when the problem authors' solutions are inconsistent.

Can problem setters please resolve that? Thanks in advance.

»
4 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

In problem E, in the first example with array $$$[1,2,3]$$$ the notes say the total cost must be 2, but consider the following transformations:

$$$ [1,2,3] \stackrel{3\rightarrow2,x=2}{\rightarrow} [1,4,1] \stackrel{2\rightarrow1,x=2}{\rightarrow} [3,2,1] \stackrel{1\rightarrow3,x=1}{\rightarrow} [2,2,2] $$$

The total cost is $$$6$$$ ($$$2$$$ for each step) and as far as I can tell I followed the rules. Am I missing something?

»
4 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

My idea for D was exactly the same as Editorial's. Somehow I failed 3rd test case and couldn't figure out why.

Please help. Code

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится +3 Проголосовать: не нравится

    Hey, did you find the bug ? I had the same idea for this and my implementation even resembles yours. My Submission

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится +3 Проголосовать: не нравится

    One of my friends was having the same difficulty and their output was same as yours.

    We found a case —

    1

    4 75

    6 30 5 2

    Correct output — 86

    The mistake was not visiting 30 again as it was already visited before by 5.

    However, I checked your latest code and it gives the right output for this case, but a mistake I found in it was that you were not marking done for the current element, what I mean is that you should do done[index] = true as well, since it can give you wrong output in case of elements having same value as an edge can be counted multiple times.

    I hope I explained well what I meant to say. Hope this helps!

»
3 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

i was just reading the editorial for problem C-Add one it was hard to understand but it is such a beautiful solution after u understand it i wanted to write it out

so for all the numbers for 0 to 9 in n we can take it to 10 if (10-s[i])<m otherwise this digit would not contribute to increasing the number of digits which gives the final formula

now our problem remains to count number of digits whenm-(10-s[i]) operations are applied to 10 for this . lets see for any number lets say 123419 we apply 10 operations the numeber of digits will be double plus as 9 will become 109 so dp[i]=2*dp[i-10]+number of 9's in the number (i-10) operations

now to calculate number of 9's in i-10 using dp[i-9]-dp[i-10] because in one operation from i-10 to i-9 all the digits that increase would just be due to the 9s in number thats is there after i-10 operations

hence dp[i]=2*dp[i-10]+dp[i-9]-dp[i-10]=dp[i-10]+dp[i-9]

which i think can be a general expression for any number not just i th operation on 10 if we set base case right

this is my understanding uptil now please tell if there is something worng about it

»
3 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

simple solution to C using 2D array ..try to use fast io and gcc GNU C++20 (64) to get AC https://codeforces.me/contest/1513/submission/156032017

»
16 месяцев назад, # |
Rev. 3   Проголосовать: нравится 0 Проголосовать: не нравится

For F, there's a simple approach that's only around ~60 lines of code. I cannot verify if it actually works for all test data, but it gets accepted.

Basically, only look at "extreme" indices:

  • Look at the indices that are amongst the 2500 smallest/largest elements in $$$a$$$.
  • Look at the indices that are amongst the 2500 smallest/largest elements in $$$b$$$.
  • Look at the indices that are amongst the 2500 smallest/largest elements in the vector $$$abs(a[i] - b[i])$$$.

Try swapping all pairs of "extreme" indices and see which one yields the best result.

215878392

»
9 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Can anyone give any counter test for my soln for D https://codeforces.me/contest/1513/submission/246063865

»
5 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится
In the Editorial of C....as mentioned Similarly, for all i from 1 to n, we get and of prefix equals to b1, and of suffix equals to b1. It would be "and of suf(i+1) equals to b1", I think as and of prefix and of suf(i+1) are qual by defination