awoo's blog

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

1107A - Digits Sequence Dividing

Tutorial
Solution (Vovuh)

1107B - Digital root

Tutorial
Solution (Ne0n25)

1107C - Brutality

Tutorial
Solution (Vovuh)

1107D - Compression

Tutorial
Solution (Vovuh)

1107E - Vasya and Binary String

Tutorial
Solution (Roms)

1107F - Vasya and Endless Credits

Tutorial
Solution (Roms)

1107G - Vasya and Maximum Profit

Tutorial
Solution (Roms)
  • Vote: I like it
  • +55
  • Vote: I do not like it

| Write comment?
»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Someone help with the solution for problem-B. I can't "easily" get it :( How did it go from the formula to (k−1)⋅9+x

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

    Any number's one digit sum equivalence is equal to number modulo 9

    Helpful Link:- https://math.stackexchange.com/questions/1789533/why-is-n-1-mod-91-equal-to-summing-all-digits-till-one-digit-is-left

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

    Assume you understand the given formula.

    Then, you will easily find that S(x) repeats every 9 integers.

    So, you can write it as my + n, y should be related to k, n should be related to x, m should be a constant.

    The numbers 1 to 9 are the first cycle. Its digital roots are itself.

    The numbers 10 to 18 are the second cycle.

    Randomly pick two integers from the first and the second cycle, throw it into the my + n, you should then get the formula.

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

    Just write a small function to print all results from 1 to 100, you can easily see the cycle pattern exist there. hope this can help.

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

Problem G:
For every diff[i], I calculated the range in which it is maximum and took maximum suffix from left side and maximum prefix from the right side to calculate the answer.
Before constructing the segment tree,just to verify the logic I submitted the code with O(n) function to calculate max prefix and suffix expecting a TLE, but surprisingly it got ACed. Submission: 49068594
Are the test cases weak or is the time complexity of this solution lesser than O(n^2)
EDIT: neal answered it here

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

For problem D, the brute force solution apparently runs fast enough.

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

    Yes,my brute force solution runs for 800+ ms.So I don't think this problem is very good and I don't know why this problem's difficulty is 1800. The time complexity just O(n²*c) ,c is the number of divisors of n

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

      c can be treated as a constant.

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

        I think c can't be treated as a constant...c changes when n changes...but certainly , c is always less than ......

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

On the one hand, if some segment of kind [1+lx , 1 + (l+1)x], where l is the value from the range [0;n/x−1], contains different digits then we cannot compress the given matrix.

Can someone explain me this with an example ??

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

    Please.. Someone explain me this with an example too.

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

    for example:- let us assume that the size of given matrix A is 8 and we are using x = 2, then values of Ceil(8/2) = 4,Ceil(7/2) = 4 which means B[4][4] is equal to A[8][8], A[7][8], A[8][7],A[7][7]. which will not be possible if these values are different.

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

Problem G: Solved it using segment trees(max segment tree) and lazy updates. Start iterating i from 1 to n. For a fixed i, a node j(<=i) will contain the answer for the segment j to i. So at each iteration, make a query from 1 to i. For the update part, when going from i to i+1, add (a — c[i+1]) to the range (1 to i) and maintain a stack to store the gap values for the range. For the stack update, if the new value i.e. (d[i+1] — d[i])^2 is smaller than the top value of stack, just add the negative value of it to the stack with it's range and also update it's value in the segment tree. If the new value if larger than the top value of stack, keep popping the top value(these values also need to be updated in the segment tree) until the new value is smaller than the stack's top value. Submission: 49103066

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

Why additional time is not given for Java programmers? Even the solution given in editorial for problem D is giving TLE in Java.

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

In task F we don't have to pick every credit, so shouldn't the formula be min(0, rest of formula)? Every Hungarian algorithm (that I found) assign all of n elements. Or just b[i]*k[i]<=a[i], but the statement doesn't include it?

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

Regarding the tutorial for problem E.

I think it should be l ≤ mid ≤ r instead of 1 ≤ mid ≤ r here.

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

    also mid<=r only if cnt=1

    • »
      »
      »
      6 years ago, # ^ |
        Vote: I like it +1 Vote: I do not like it
      dpdig, l, r, cnt = max{ansl, mid - 1 + dpdig, mid + 1, r, cnt - 1}

      what is the difference between these two?

      dpdig, l, r, cnt = max{ansl, mid - 1 + dpdig, mid, r, cnt}

      i mean is there something wrong with the second one?

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

Someone please explain the solution for problem-E, I didn't understand it from the tutorial.

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

    Made a solution just doing recursion on strings to show the idea (solution). I think this is a bit easier to follow.

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

      Nice Approach, I tried this and got AC, but before that I had tried to recursively remove each block of consecutive digits(same 1s or Os). Algorithm is right but I got TLE, I don't understand why it is. Can you explain what happen? HERE IS MY CODE: https://mirror.codeforces.com/contest/1107/submission/49218582

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

        The difficult part of this problem is not really to calculate the answer but to do it quickly. The reason why my code doesn't get TLE is that I practically only modify the beginning of the string, and if you think about it all the strings that is created will basically be a substring of the original string (except for the beginning part). So the number of strings I create are very limited.

        In your code you are not doing it nearly as neatly because you are deleting parts of the string everywhere (not just in the beginning). So you will create a ton of different strings, probably exponentially many, and because of that get TLE.

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

          Yeah I understand your code, that is your state either is substring of original string or combination of two substrings of original string. The number of different substring is nC2 ~ N^2 and the number of combination of 2 substrings is (N^2)C2 ~ N^4, so your complexity is O(N^4).

          And my code may be up to N! So it gets TLE.

          Thanks for your help!

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

            To be technical, its not either a substring of the original or a combination of two substrings. It is always a string on the form '0...0' + S[a:b] or '1...1' + S[a:b] so there are n3 possible different strings.

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

              Yep, sorry. So it runs in N^3 right?

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

                The algorithm will ever only visit n3 strings but for each string it will do at most n2 work. This is because for each string it might need to lookup n strings in a hashtable. So it runs in n5 with a very good constant, this could be lowered to n4 by playing around with indices instead of strings.

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

I will give a proof in this comment on why the formula for 1107B - Digital root works.

To see that the formula is true you can prove that and gives the same value or it is invariant under the formula. In the case of xj substracting 1 means that the trailing 0s become 9s so the disappear modulo 9 and if k is the first non-zero digit of xj then its value will decrease by 1. Then we can do something like this:

It's not hard to see that given a b-ary number system where b is a positive integer if we define a similar process then the number x will end up in Sb(x) which can be characterized like:

The proof is the same as the above just replace 10 with b and 9 with b - 1.

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

good

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

Can someone please explain the editorial of E in more detailed way ?

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

Can someone explain why DP calculation in E is correct i.e. why there exist 'mid', why optimal solution for DP doesn't contain more than one segment which will lead to 'cnt' number of 'dig' instead of just a segment [mid, r]?

Upd: Got it.

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

vovuh In D why can't we use binary search across the divisors of n to find x?

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

    Because the function isn't monotonic. For $$$n=12$$$ if the answer for $$$2$$$ and $$$4$$$ is true then it is not necessary that for $$$3$$$ the answer also will be true.

    Example (uncompressed):

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

A different approach for problem D here

Using gcd

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

Whosoever solves problem E.

Please let me know how the complexity of the solution is O(n^4).

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

For Problem G: I used Kadane's algorithm but it gives wrong answer on test 7 and test 36 only. Could anyone point out the issue with this approach. Code with some comments.

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

Simple bruteforce is enough for 1107D — Compression. https://mirror.codeforces.com/contest/1107/submission/160440328