ilove_sundarKanya's blog

By ilove_sundarKanya, history, 3 weeks ago, In English

In today’s Div. 2 contest, I encountered a frustrating issue that taught me the importance of precision in competitive programming.

The problem logic seemed straightforward: identify odd perfect squares. Confidently, I implemented the logic in my code editor (VS Code), tested a few cases locally, and it seemed flawless. However, when I submitted the solution during the contest, it gave Wrong Answer on Test 1 repeatedly.

Problem:

I couldn’t accept that such an easy problem was slipping away. Frustration kicked in as I spent over 40 minutes debugging and even tried different C++ standards like C++17, C++20, and C++23, only to face the same result: Wrong Answer on Test 1. Disheartened, I shifted my focus to solving other problems, managing to complete Question B and Question C before circling back with just 30 minutes remaining.

After some reflection and experimenting, I finally pinpointed the issue:

Incorrect Code

It turns out that floating-point arithmetic caused the issue. sqrt(n) * sqrt(n) does not always yield exact results due to precision errors in floating-point operations.

I replaced it with a safer and integer-based alternative:

Correct Code

This resolved the issue instantly, but by then, the damage was done. The time lost on debugging and the rank I could have earned were irrecoverable.

I hope whoever reads this avoids such a mistake in the future.

Full text and comments »

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

By ilove_sundarKanya, history, 7 weeks ago, In English

I’d like to share a tip that could save you time and frustration, especially in competitive programming: avoid using C++’s pow function when working with integers.

Recently, I spent nearly an hour debugging a lengthy piece of code, only to realize that the issue stemmed from using pow. Despite its convenience, pow can sometimes introduce unexpected precision errors when working with integer calculations. This can lead to incorrect results, especially in competitive programming where accuracy is key.

Instead, consider writing a custom power function or using iterative multiplication for integer exponentiation. This approach is often more reliable and can help you avoid subtle bugs related to precision issues in pow.

Hope this tip saves you time in the future!

Thank you!

Full text and comments »

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