toTalmeSS's blog

By toTalmeSS, history, 8 years ago, In English

Hello... I got TLE on the following problem. I myself found out that it's taking an awful amount of time for inputs with eight or nine digits! How can I optimize my code and reduce the complexity? I tried to devise a formula, but couldn't go anywhere. I can still try if any of you could tell me how to approach...

Thanks in advance.

Problem link My code

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

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

You need to find a formula to solve this problem, you can't iterate over 109 elements.

  • Actually, one simple observation is enough to solve the problem: For every number x that has a negative sign, the will be a number x + M with a positive sign. There are N / 2 total positive numbers, so the answer is simply .
C++ Code