Cooper1214's blog

By Cooper1214, history, 2 years ago, In English

So, I have encountered a strange problem while solving 1541B — Pleasent Pairs
My first solution gets accepted ( Solution 1 ) , while the second one got TLE ( Solution 2).

I don't understand why this is happening. They both are almost same. Any help?
Thank you.

  • Vote: I like it
  • +4
  • Vote: I do not like it

| Write comment?
»
2 years ago, # |
  Vote: I like it +3 Vote: I do not like it

This happens because an overflow of i*j expression (i * j can be 4*10^10 that more than can be placed in int (2^31-1 (2'147'483'647) is the max int value) ) so the cycle doesn't end when i*j > 2*n and you get TLE. You just can write 1LL*i*j > 2*n and this will get accepted.