Codeforcer's blog

By Codeforcer, history, 4 years ago, In English

I cannot figure out why this code is getting TLE'd despite of everything being under order and constraint. I knew those mod operations were costly so I optimized them but still no luck.

Problem : https://mirror.codeforces.com/contest/1466/problem/E

Code

Update-1 : Resolved, It barely passed(1996 ms) after I changed some ll's to ints. Would still like to hear some advice or a good solution though!

Update-2 : The comments are absolute gold thanks guys! Made the necessary changes and made code more concise.

New Code

GNU C++17(64 bit) -> 453 ms

GNU C++17 ->1263 ms

Would still like to know more on the difference in time on the above 2 compilers.

Thanks!

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

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

I can't help you, but just wanted to let you know for some reason your code in 64-bit G++17 passes in 1.2 seconds.

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

Might be related to the fact that you're converting 5*10^5 numbers to binary by making 60 push_backs to an std::vector<long long> (causing it to reallocate and copy its contents multiple times), then copying them back to memory using 8 bytes per bit (later only accessing them once to count numbers with a given bit set), and then inefficiently converting the same 5*10^5 numbers to binary again, while converting a number to binary normally requires no action at all.