awoo's blog

By awoo, history, 3 years ago, translation, In English

1295A - Display The Number

Idea: adedalic

Tutorial
Solution (BledDest)

1295B - Infinite Prefixes

Idea: Roms

Tutorial
Solution (adedalic)

1295C - Obtain The String

Idea: Roms

Tutorial
Solution (Roms)

1295D - Same GCDs

Idea: adedalic

Tutorial
Solution (adedalic)

1295E - Permutation Separation

Idea: adedalic

Tutorial
Solution (adedalic)

1295F - Good Contest

Idea: Roms

Tutorial
Solution (BledDest)
 
 
 
 
  • Vote: I like it
  • +106
  • Vote: I do not like it

| Write comment?
»
3 years ago, # |
  Vote: I like it +10 Vote: I do not like it

Finally, the editorial is out :D

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

in F how are we dividing into segments, little confused after reading tourist solution, also could someone help me what is this another way(link) of solving increasing sequences question.Thanks in advance.

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

    We use the values of $$$l_i$$$ and $$$r_i + 1$$$ as left borders of the segments: we sort all of them, get rid of the unique values, and each value now defines the beginning of a new segment. So, for each segment we get, all values from it can be used at the same positions of the non-descending sequence we are trying to construct.

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

Please,can anyone explain problem C,how you are calculating nxt table since I am not able to visualize it. thank you.

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

    If you are at index i in the string and want to calculate the smallest index of jth char between i and |s|, two possibilities occur. 1. s[i] is the jth character 2. s[i] is some other character than the jth character

    For the second case, s[i][j]=s[i+1][j] and for the first case s[i][j] is simply i

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

      Think about it this way

      For each position we need the next position for all characters from 'a' — 'z' if they exist.

      So starting from back to front for the current position we get results from index in front and for the current index we can set next position for the character at index i to be index i, since that's closer.

      Hope this helps.

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

    case -1: if any char in t is not present in s. other wise: store the positions of a,b,c...z occurring in S, in a vector of size 26.

    now build z from t by taking one char at a time (starting from 0). now suppose you have build some part of z and you had just chosen a character which was at position 'cur' in s.

    now we want to add next char of t at the end of z — lets say the next char is x. hence we need the position of 'x' in s. if we think greedily we will chose that position which is just after our cur. if there is no x after cur. we can start from beginning and therefore setting cur=0, and number of steps ++.

    https://codeforces.com/contest/1295/submission/69747737

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

    Just maintain an array recording the nearest char C whose index is strictly bigger than the current index enumerated from n to 1

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

What can be the possible divide and conquer solution for problem E? Thank you

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

Small correction: The combinatoric identity in F, the denominator of the last fraction is k and not k+1.

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

Is there any inclusion exclusion based solution to D?

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

IN PROBLEM D HOW IS 0 GETTING MANAGED BY USING THE EULER TOILENT FUNCTION.

EDITORIAL SAYS THAT GCD(0,m')=m'

but 0 can be added to a and gcd(a,m) will be same as gcd(a+0,m);

please help..

Thanks in advance ..

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

    $$$x' = x' ' g$$$ and author shows why 0 is not valid for $$$x' '$$$. The case $$$gcd(a, m) = gcd(a + 0, m)$$$ is covered when $$$x' ' = a'$$$

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

      i am not able to understand the tutorial for 1295D can you explain

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

        Let $$$gcd(a,m) = g$$$. Also Let $$$x' = x+a$$$.

        We want $$$gcd(x',m) = g$$$ which is equivalent to $$$gcd(\frac{x'}{g},\frac{m}{g}) = 1$$$

        So we need find number of $$$x'' = x'/g$$$ co-prime with $$$m' = m/g \in [1,m'-1]$$$ Since $$$x+a > 0$$$

        This is the Euler Phi Function.

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

Please explain problem E in simple words..

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

    Which words do you not understand?

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

      We are iteration on val or on POS and what we store on segment tree. And Finally this thing -- So what will happen if we increase val by 1? Let's define the position of pk=val as k. For each pos≥k we don't need to move pk to the second set anymore, so we should make t[pos]−=ak. On the other hand, for each pos<k we need to move pk from the second set to the first one now, so we should make t[pos]+=ak. Thanks

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

        Every node of segment tree stores the minimum cost if we select a segment [0,r] and we want all the elements between [0,r] lie between [0,val]. Initially assume val is -1. Therefore for every segment [0,r] we will have to shift its elements to the other segment (since no value lies between [0,val], i.e., [0,-1]). Now we will iterate over val. Now when value is 0, let the position of 0 in the original array be 'ind'. How to update the answer now??
        For every segment [0,r] which contain the index 'ind', we do not need to shift val to the other segment (we shifted it when val was -1,). Thus for every segment [0,r] which contain 'ind' we will add (-a[ind]). And for every segment [0,r] which does not contain 'ind' we need to get it, therefor we will add (a[ind]).
        Hope you got it. Feel free to ask if you didn't understand anything.

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

          We build segment tree on the given array and then check ind for every node Right? And why we iterate on Val not on pos.

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

            As we are sweeping according to val, which means we already know the answer for val, and we want to know the answer for val+1.

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

              Why are we sweeping from 1 to n+1 ? shouldn't it be from 1 to n ? n+1 doesn't even exist in the permutation.

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

                "All elements in the left set smaller than all elements in the right set" means that there is such value val that all elements from the first set less than val and all elements from the second set are more or equal to val. "
                Since all elements on the left set are less than (not less than equal to )val therefore sweeping till n+1, means left set will contain numbers from [1,n].

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

what is n in the editorial of Problem-B ? i mean what does n mean?

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

C can also be solved using binary search, pretty easy to understand.69761627

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

    Hey, I didn't get your code.How you are applying a binary search. Can you explain in detail please.

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

      Firstly we store the positions of all the appearance of each characters in string s. Then for each character in string t, we want the first appearance of that character after the position of the last character, which could be done with binary search.

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

        last character of what? string s or t ?

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

          I mean the last character that has been obtained in string t. Sorry for my bad English.

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

F says: we will count all the non-descending sequences

but must be non-increasing, since pair (1, 2) is inversion according to task

UPD: sorry, must be descending with possible equal, since descending with possible equal != non-increasing (1, 3, 2) is not-increasing, but not a descending with possible equal

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

    descending means a[i+1]<a[i] and non-descending means a[i+1]>=a[i]

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

      yes, but we need the array to look something like this: 3 3 2 1 a[i] >= a[i+1] it's not a strict decrease, but not a non-decrease or non-increase

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

        a[i+1] is less than a[i]----------a[i+1]<a[i]. -----------descending

        a[i+1] is not less than a[i]------a[i+1]>=a[i]. -------non-descending

        In this problem, we need to count the sequence a[] which satisfied:

        a[1]<=a[2]<=...<=a[n].

        And it means the sequcence a[] is a non-descending sequence.

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

    Yeah I got confused by that; once I realized the issue, I just reversed the array after reading the input to get the right answer

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

What programming language is the D solution written is? Is that just pseudocode?

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

Any other solution for problem D? or more detailed explanation for the one mentioned in the tutorial?

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

    1) gcd(a, m) is fixed -> let it be G

    2) Want to find gcd(a + x, m) = G, 0 <= x < m

    3) Same as gcd(X, m) = G, a <= X < a + m

    3.5) gcd(X, m) = gcd(X % m, m) (Think about euclidean algorithm)

    3.75) When you loop X from a to a + m — 1, X % m includes all possible outcome from 0 to m — 1.

    4) Same as gcd(Y, m) = G, 0 <= Y < m

    5) Same as gcd(Y / G, m / G) = 1, 0 <= Y / G < m / G (Floor division)

    6) So count number of Z such that gcd(Z, m / G) = 1 under m / G. That's the definition of euler's totient function.

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

      How could you be sure that (in point 5) after divide all numbers (0 to m-1) by G there can be found φ(n) numbers of value those are coprime to m/G ??

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

        For gcd(a, b) = G, a = k1*G and b = k2*G where gcd(k1, k2) = 1

        So gcd(a / G, b / G) = 1 is a necessary and sufficient condition

        Does that help?

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

        What do you mean by $$$\phi(n)$$$?

        If you want number of numbers coprime to $$$m/G$$$, then you use $$$\phi(m/G)$$$.

        $$$\phi(n)$$$ gives number of numbers coprime to $$$n$$$, and smaller than $$$n$$$.

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

      THANKS

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

    I hope my blog would help a bit.

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

Can someone explain editorial for B?

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

    To me it was easier to see it like this.

    Suppose the input was 011101000100, then you build the "balance" array of it, which is what the editorial mentioned:

    s:  0  1  1  1  0  1  0  0  0  1  0  0
    b:  1  0 -1 -2 -1 -2 -1  0  1  0  1  2
    

    The last number in the balance array, $$$bal(n)$$$, is going to be a "delta", and you can see that in the next iteration of the input the balance array will be the same, only delta units higher, like this:

    s:  0  1  1  1  0  1  0  0  0  1  0  0
    b1: 1  0 -1 -2 -1 -2 -1  0  1  0  1  2
    b2: 3  2  1  0  1  0  1  2  3  2  3  4
    b3: 5  4  3  2  3  2  3  4  5  4  5  6
    

    From here you can see that from each position $$$i$$$ the numbers you can reach are $$$bal(i) + \delta \times k$$$, where $$$\delta = bal(n)$$$, for some $$$k \geq 0$$$.

    You also need to be careful of two things. What if the delta is 0? then the balance array is going to repeat itself to infinity, so if the $$$x$$$ is in the initial balance, then the answer is -1, and if it's not it's 0. Lastly, how many times will $$$x$$$ appear? well for each position $$$i$$$, $$$x$$$ can be reached 0 or 1 times. If it can be reached more than once, that immediately means delta is 0, so the answer is -1. You can check if $$$x$$$ can be reached from an initial balance $$$bal(i)$$$ easily using modulo or the formula in the editorial.

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

"So let's make a sweep line on val from 1 to n+1 while trying to maintain all answers for each prefix pos". What does making a sweep line mean?

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

    It means we reconceptualize the $$$val$$$ dimension as physical time, and find a way to transition our program's state from $$$val = x$$$ to $$$val = x + 1$$$ (so like a line "sweeping" over that dimension over time). Working it out as in the editorial reveals that we want an array that we can do range additions and range minimums quickly, which can be implemented by a lazy-propagating segment tree.

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

Thanks guys for the editorial. By the way.

Happy Lunar New Year !
»
3 years ago, # |
  Vote: I like it +13 Vote: I do not like it

F says: ** Calculating the number of ways to take K elements from an interval [L,R) in sorted order can be reduced to calculating the number of ways to compose K as the sum of R-L non-negative summands (order matters). **, but must be the number of ways to compose R-L as the sum of k non-negative summands (order matters)

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

can anyone please optimize this code ? thanks in advance. this is my submission

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

REGARDING QUESTION D can this question be solved by taking prime factors of m and then by inclusion and exclusion of factors of these prime numbers?

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

Can anyone help me to find out the bug of my solution?

Problem: 1295E - Permutation Separation

Code
Approach
»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Can anybody help me with the following?

"Otherwise for each such j there is at most one k satisfying bal(j) + k.bal(n) = x ?

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

    We look at the string in iterations. One iteration is one time copy the string s to the end of the whole infinite string.

    If the balance for the string s bal(n)!=0, then it is so that the balance at a position j is at most once equal to x in all iterations.

    This is so because the balance at a position j changes from iteration to iteration by bal(n), and because it changes, it can be equal to x only once.

    That one time is the k-th iteration. To find the answer, we count the positions j where such a k exists.

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

Can anybody help me figure out why my DP solution for E is wrong ? dp[i] means the minimum cost if I choose there is i elements in the first set. Considering array a is a permutation, so there is exactly [1, i] in the first set if it is "good", am I right? I wrote my solution based on that. Here is my submission. 70131818 Thanks!

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

    Sorry, never mind this, I figured it out already.

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

      Can you explain where was the problem? My approach is similar with yours and I have the same wrong answer on 10th test case (69928822).

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

        We miss many situations, if we split the array into [1, k] and [k+1, n] at first, the "good" array after we move the elements may not as the same length as the original(k in the first and n-k in the second, this is nay not the minimum). That is the point. So naive but right solution must be two loops, one is the pos to split, another is the length(or val).

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

Problem A is almost same with 774C - Maximum Number.

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

Can anybody help me please: I cannot understand why in problem E we cannot compute L and R arrays as well as in prodipdatta7' solution in such way: Let's consider given permutation P and sequence 1,..., n — denote as N; current sum for L — denote as sum; set S. For i from 1 to n — 1:

  1. If k > i such that P(k) = i then: S.insert(i); sum += a(i) (- function a(x): cost of x in P)
  2. If P[i] belongs to S then: sum -= a(P[i]); S.erase(P[i]).

Such approach fails on 10th test.

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

    I think that the following example can help:
    Input:
    4
    2 4 1 3
    8 6 9 7
    Correct output: 6
    Your output: 7

    The correct solution (total cost = 6):
    Step 1: Split the initial permutation into [2, 4, 1] and [3];
    Step 2: Move 4 from the first set to the second set (total cost = 6). The final sets are [2, 1] and [4, 3].

    Your solution (total cost = 7):
    Step 1: Split the initial permutation into [2, 4, 1] and [3];
    Step 2: Move 3 from the second set to the first set (total cost = 7). The final sets are [2, 4, 1, 3] and [].

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

in Problem D why m`>1 ? I need explanation :( it's too hard

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

E can also be solved using Ternary search.

Solution link — here

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

Problem D is just Insane.

Since I found the editorial and all other comments quite tough to understand, here's my Detailed Beginner friendly approach. As Someone above said, this is what I tend to explain, a bit more clearly Perhaps.

Step By Step Approach For D:

  1. Let gcd(a,m) = g. So, we can say a=g*k and m=g*l. Now Putting back a and m , gcd(g*k,g*l) = g. Obviously , we can say gcd(k,l)=1.

  2. Now looking closely, we need to find all x such that gcd(a+x,m)=g. means that in our answers, x should also be a multiple of g. So let x=y*g. Now, gcd(g*k + g*y , g*l) = g, or gcd(k+y , l) = 1.

  3. Now What could be the range of y ? See, we have x=y*g and x<m and m=g*l, So we can say that 0<=y<l. So now just need to find for how many y in range [0,l), gcd(k+y,l) = g ?

  4. Now our Problem has been reduced to find how many numbers ranging from k to k+l are coprime with l.

  5. Now there's a Euler Totient Function i.e. Phi(n) which tells how many numbers between [1,n) are coprime with n, in O(sqrt(n)).

  6. We tend to use this, But how? Our current problem is to find how many numbers between [k,k+l) are coprime with l.

  7. Observe, that if 2 numbers are coprime, i.e. gcd(temp,l)=1 and temp>l, we can say temp = l*some_number + tmp%l . See, we can also say that gcd(temp%l,l)=1 . (See this step again).

  8. Now, we have established that we have a Range (of length l) [k....l.....k+l] to look for which of these have gcd()=1 with l, and for all those numbers z in this range greater l, we can simply replace them by z%l.

  9. So in the end, we have our Range as [1...l] only. Now to find all the numbers in [1,l] which are coprime with l, just use the Totient Function in O(sqrt(n)

Code.

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

I really liked Problem E