Binary_Crusader2503's blog

By Binary_Crusader2503, history, 21 month(s) ago, In English

In the contest Hello 2025 Question 2 (https://codeforces.me/contest/2057/problem/B).I encountered an interesting issue where the same logic, when implemented in Python, resulted in a Time Limit Exceeded (TLE) error, while the C++ version worked flawlessly within the constraints. This raises an important question about the fairness of using Python in competitive programming. Python, being an interpreted language with slower I/O operations and dynamic typing, tends to be slower than C++, especially when handling large inputs within strict time limits. Contest organizers should ensure that Python solutions are adequately supported by adjusting time limits and testing across all allowed languages to ensure fairness. If Python is offered as a valid contest language, it’s important to apply time multipliers, and set reasonable input sizes that account for Python’s slower performance. As a participant, while optimizing my solution is my responsibility, it’s crucial for contest organizers to consider these language-specific challenges and create a level playing field for all competitors. Python Code:-299626739 C++code:-299702848

I hope that the organizer will atlest try to compensate for this.

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

| Write comment?
»
21 month(s) ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Unfortunately, python has a very exploitable hashing algorithm (due to its predictability). So, pretty much, you should never use the dictionary in python. But if you do, you can do something like this: 299613409. It just xors keys with a random number before adding them to the dictionary. This makes it less predictable.

»
21 month(s) ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

This is not Python related, at least not directly. The reason your solution got TLE is not because Python is slower than C++, it's because Counter uses a hash table and someone submitted a hack that causes collisions in the hash table, resulting in $$$O(n^2)$$$ time complexity. C++ users which used unordered_map got TLE for the very same reason.

In general, using any data structure that uses hashing with its default hash means you're almost certainly going to get hacked/FST-ed. Either use a custom, safer hash, or use structures that don't rely on hashing.