Блог пользователя alfaloo

Автор alfaloo, 22 месяца назад, По-английски

Hello, hope you are all well. As a fellow Codeforcer, I hate the feeling when my code gets hacked or stuck on a private test case for a trivial reason. Thus I wanted to create this thread where everyone is encouraged to share some niche case where their code unexpectedly broke :(

I'll start first:

  • In the Hello 2025 problem A, my unordered hash structure got TLE hacked. Solution is the use a custom hash function, courtesy of Neal's post.

  • In Codeforces Round 996 problem D, I tried printing a double to console in cpp. For large values, doubles defaults to scientific notation (e.g. 1.2345e+7), so I was stuck on that private test case for the whole contest :(. Solution is to cast the result to int or long long before printing.

  • I've read somewhere that sqrt and log functions can have precision issues at large values? I've never encountered an error when using them so far though. Edit: now I have... consider these two submissions for Codeforces Round 976 question B, one passes 346493439 and one fails 346486376

I hope my mistakes are helpful to you all :)

  • Проголосовать: нравится
  • +21
  • Проголосовать: не нравится

»
22 месяца назад, скрыть # |
 
Проголосовать: нравится +13 Проголосовать: не нравится

For your second point, no need to cast it. You can just use cout << fixed << your_double next time.

»
22 месяца назад, скрыть # |
 
Проголосовать: нравится +22 Проголосовать: не нравится
  • Don’t use normal polynomial hashing (even triple hashing) with small fixed modulos ($$$\approx 10^9$$$) in a round with open hacking because they are pretty easy to hack. It’s better to avoid using hashing in rounds with open hacking, but if you are forced to, make sure to randomize everything (even the alphabet).

  • Avoid dealing with doubles when 64-bit integers are involved.

  • Make sure that you don’t memset a large array of constant size in every testcase as many problem setters forget to include a test with max number of testcases in the pretests.

  • v.clear() doesn’t free the vector’s memory so if you are doing a small to large trick the memory complexity would still be $$$O(n \log n)$$$ which might MLE in some cases. However v.clear(); v.shrink_to_fit(); frees its memory and makes the memory complexity $$$O(n)$$$ in this case.

»
22 месяца назад, скрыть # |
 
Проголосовать: нравится +22 Проголосовать: не нравится

mine: never use unordered_map in codeforces contests

»
22 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I've read somewhere that sqrt and log functions can have precision issues at large values? I've never encountered an error when using them so far though, recently I have encountered that (sad).

»
22 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Here's one that has gotten me a few times: when you're solving a problem with $$$998244353$$$ or $$$10^9 + 7$$$ ($$$mod$$$), make sure you return your answer as ((answer % MOD) + MOD) % MOD because if your answer went negative, the % operator will not make it positive, so you have to add the mod to get it positive.

»
22 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Don't use "funny numbers" even in cases where you think any constant would work

Spoiler for "Nene vs. Monsters" problem

If I had a bug like this in contest and someone was targeting me with a hack then they'd get me anyway, but using less common numbers can protect from other hacks after the contest

»
12 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by alfaloo (previous revision, new revision, compare).