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

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

I keep getting TLE on test case 6 of the following question : https://codeforces.net/contest/559/problem/B here is a link to my code, https://codeforces.net/contest/559/submission/108124634 thank you!

  • Проголосовать: нравится
  • -6
  • Проголосовать: не нравится

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

Most probably your recursion is running into an infinite loop.

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

    Yeah, I checked for that but I am dividing the length by 2 at every call so I don't see that happening.

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

      actually your time complexity is O(n^2).

      Because you are calling recursion 4 times. So, you are forming a tree with height log2(n) with 4 calls everytime.

      hence, net complexity = 4^(log2(n))=n^2.

      (Feel free to Correct me if I am Wrong).

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

        Yeah, that seems to be the case. Maybe use memorization to try and speed it up, if you cannot reduce number of recursion nodes.

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

          https://codeforces.net/contest/559/submission/29503685 this code is almost same as mine and it got accepted. It has the same recursive calls.

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

            There is a tiny (but very significant) difference in the two codes: in the accepted version they use the functions directly in the return, but in yours you calculate the values before. The reason this matters is because in the accepted version, if the first part of the and statement returned false, the code won't evaluate the rest of the and statement, because it knows that the expression will be false (because x&0 = 0 for any value x). This means that in the accepted version, there is a good chance many of the recursive calls are actually skipped. Here is an accepted version of your code, where I explicitly skipped the second recursive call if the first one failed. 108130742 Note that you shouldn't rely on this in the future because it's an unproven heuristic, and I'm pretty sure it's possible to make a hack test that causes the code to run in $$$O(n^2)$$$.

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

              I tried submitting using the same fix and still this fails. Did I miss something? 108130830

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

                Changing the substring comparison back to the comparing them letter-by-letter made it pass pretty comfortably: 108131602. There are two reasons that this could've happened:

                1. Bad constant: because you first have to create two separate strings from the substrings and then compare them instead of just one pass through the string, this could have a 3x worse constant factor.
                2. Early breaking: when there is a mismatch, the for loop automatically breaks, possibly also making it faster.
            • »
              »
              »
              »
              »
              »
              »
              4 года назад, # ^ |
                Проголосовать: нравится 0 Проголосовать: не нравится

              oh I got it. but I am still getting tle at 91 https://codeforces.net/contest/559/submission/108131756

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

                It passes if you submit it in 64 bit: 108132405. In my experience, in most cases 64 bit is better (it's often faster and it has things like 128 bit ints), so if I were you, I would submit everything in 64 bit.

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

                  thanks a lot man! Is there any downside to submitting in 64 bit?

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

                  There are none that I know of, but I can't definitively say that 64 bit is always better. Maybe someone who understands the nuances of both better can comment here.

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

                  I once faced an issue with the 64 bit compiler while making a comparator:

                  This passes in both 14,17 but fails in 17(64-bit)
                  This change fixes it
                • »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  4 года назад, # ^ |
                    Проголосовать: нравится 0 Проголосовать: не нравится

                  Ah yes, I forgot about that. That is an example of the issue mentioned in this blog, where 64 bit c++ has weird precision issues when working with doubles. I think it's still worth it to submit in 64 bit, especially since it's possible to fix this issue by adding one line to your template (see the blog).

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

I somehow managed to improve your code and now it passes 90 test cases. Final Link: 108130830

Here's my submission to this problem: 108130352. Though, I don't see any difference between these but yours still fails, if you find any let me know as well.