Binary_Crusader2503's blog

By Binary_Crusader2503, history, 47 hours ago, In English

In the contest Hello 2025 Question 2 (https://mirror.codeforces.com/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.

»
47 hours ago, # |
  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.

  • »
    »
    47 hours ago, # ^ |
    Rev. 3   Vote: I like it 0 Vote: I do not like it

    we have also an exploitable in c++, the "unordered_map", then it is important to know how your tools works xD

    Language is a tools, you need to know some aspects in deep to get the best performance, whether it is the tool or some algorithms.

»
47 hours ago, # |
  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.

»
47 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

Even I got tle but I did not use counter I used simple dictionary. https://mirror.codeforces.com/contest/2057/submission/299699660

»
47 hours ago, # |
  Vote: I like it 0 Vote: I do not like it
  • »
    »
    47 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Plus my c++ code is what i submitted during the contest and it had the same time complexity as the editorial code.

  • »
    »
    47 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Unordered map... Hash problem.

»
47 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

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