Phobiceron's blog

By Phobiceron, history, 4 years ago, In English

86827890 problem:https://mirror.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.

  • Vote: I like it
  • -9
  • Vote: I do not like it

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

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 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

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

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

      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 years ago, # ^ |
          Vote: I like it +5 Vote: I do not like it

        Thankyou very much, the rule of thumb will help me a lot :D

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

There are several thousend working solutions and a tutorial

You are supposed to have a look into that resources.