Пожалуйста, подпишитесь на официальный канал Codeforces в Telegram по ссылке https://t.me/codeforces_official. ×

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

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

86827890 problem:https://codeforces.com/contest/1372/problem/B it says TLE on the 4th test case. Can someone please help me to find a better solution.Thanks in advance.

p.s.:sorry if this isn't how you are supposed to use blogs, i wasn't sure where to post doubts in this website.

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

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

For odd numbers you are checking till n/2 making the complexity O(n). The constraints are too high for O(n). Try doing it in O(sqrt(n))

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

    thankyou, how will i know if the constraints are too high for my time complexity?

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

      Suppose you know your complexity and it is something like $$$O(f(n))$$$. Take the maximum possible $$$n$$$ and calculate $$$f(n)$$$. If it is less than $$$2 \cdot 10^8$$$ it fits in 1 second (it's only a rule of thumb but in practice it works very well).

      In your case maximum value of $$$n$$$ is $$$10^9$$$.

      • if complexity is $$$O(\sqrt{n})$$$: $$$\sqrt{10^9} \approx 31600 < 2 \cdot 10^8$$$, passes very well.

      • if complexity is $$$O(n)$$$: $$$10^9 > 2 \cdot 10^8$$$, won't pass unless you try very hard to squeeze it in.

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

There are several thousend working solutions and a tutorial

You are supposed to have a look into that resources.