Multiples Of The Prime
libstdc++ sizes unordered_map from a fixed table of primes: ..., 92203, 99733, 107897, 116731, 126271, ..., reserve(n) rounds the bucket count up to the first prime at or above n, and std::hash on an integer is the identity function. A solution counts values in an unordered_map<uint64_t,int> and calls reserve(100000) first, for speed, which pins the bucket count at 107897. A Codeforces hacker reads that submitted source and sends this test:
// hack input: 100000 values, all multiples of 107897
for (int i = 1; i <= 100000; i++)
printf("%llu\n", (unsigned long long)i * 107897);
Every key satisfies hash(k) % 107897 == 0, so all 100000 land in bucket 0 and each insertion scans the whole chain. Measured on GCC 15, the same loop over 100000 ordinary values finishes in under 0.01 s and this one takes 4.9 s, so the solution times out.
More than one option is correct. Which changes actually defend against this?
Sign in to answer questions and track your progress
Sign In