Anubhabghosh's blog

By Anubhabghosh, history, 4 years ago, In English

Problem link: https://mirror.codeforces.com/contest/1423/problem/K

submission link: https://mirror.codeforces.com/contest/1423/submission/97905194

I don't understand why I am getting TLE can anyone help?

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

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

Instead of running an upper bound for each case, maybe try keeping an array where a[i] is the number of primes till i.

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

Replace cout with printf. std::cout is too slow for printing.

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

    That's a myth. With non-synchronisation with stdio, cout is as fast as printf, if not faster.

»
4 years ago, # |
  Vote: I like it +10 Vote: I do not like it

First of all, cout.tie(NULL) doesn't do anything (literally).

Your code gets TLE because endl is slow (and unnecessary here).

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

    Thank you very much. Now I understand why it was giving TLE. After replacing endl with "\n" now it is giving ac.