pedrovictor48's blog

By pedrovictor48, history, 5 years ago, In English

Firstly i came up with this solution: https://mirror.codeforces.com/contest/1542/submission/121218884 I think it's O(n) in worst cases, and i got TLE. But shouldn't O(n) be sufficient for these constraints?

After the contest I also updated a O(logn) solution, and its similar to others that i saw submitted, but its getting TLE too: https://mirror.codeforces.com/contest/1542/submission/121269280

What am I missing here?

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

| Write comment?
»
5 years ago, hide # |
 
Vote: I like it +16 Vote: I do not like it

I didn't think much but Im certain that you should make p long long. Its possible for it to keep overflowing and doing a lot of interactions.

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

If $$$a$$$ is $$$10^9-1$$$ and $$$n$$$ is $$$10^9$$$,first $$$p$$$ will multipliy by $$$a$$$ and then $$$p$$$ is $$$10^9-1$$$,second $$$p$$$ will multiply by $$$a$$$ again and $$$p$$$ will be more than $$$\text{INT_MAX}$$$.It will be very likely that $$$p$$$ overflows and make it less than or equal to $$$n$$$,so this code will keep running and you will get TLE.