Please subscribe to the official Codeforces channel in Telegram via the link https://t.me/codeforces_official. ×

neal's blog

By neal, 5 years ago,

C++ has always had the convenient data structures std::set and std::map, which are tree data structures whose operations take time. With C++11, we finally received a hash set and hash map in std::unordered_set and std::unordered_map. Unfortunately, I've seen a lot of people on Codeforces get hacked or fail system tests when using these. In this post I'll explain how it's possible to break these data structures and what you can do in order to continue using your favorite hash maps without worrying about being hacked .

So how are they hackable? We always assume hash maps are O(1) per operation (insert, erase, access, etc.). But this depends on a key assumption, which is that each item only runs into O(1) collisions on average. If our input data is completely random, this is a reasonable assumption. But this is no longer a safe bet when the input isn't random, especially so if someone is adversarially designing inputs to our code (a.k.a. hacking phase). In particular, if they know our hash function, they can easily generate a large number of different inputs that all collide, thus causing an O(n2) blow-up.

We'll prove that now by blowing up unordered_map. In order to do that, we first have to determine exactly how it's implemented. For this we can dig into gcc's implementation on GitHub: https://github.com/gcc-mirror/gcc.

After some searching around we run into unordered_map.h. Inside the file we can quickly see that unordered_map makes use of __detail::_Mod_range_hashing and __detail::_Prime_rehash_policy. From this we can guess that the map first hashes the input value and then mods by a prime number, and the result is used as the appropriate position in the hash table.

Some further searching for _Prime_rehash_policy leads us to hashtable_c++0x.cc. Here we can see that there is an array called __prime_list, and the hash table has a policy to resize itself when it gets too large. So we just need to find this list of primes.

The one include on this file leads us to hashtable-aux.cc. Aha, here is the list we're looking for.

One more thing: we need to know the hash function unordered_map uses before modding by these primes. It turns out to be quite simple: the map uses std::hash, which for integers is simply the identity function. Armed with this knowledge, we can insert lots of multiples of one of these primes to the map in order to get n2 blow-up. Not all of the primes work though, due to the resizing policy of the map; in order for a prime to work, we need the map to actually resize to this prime at some point in its set of operations. It turns out the right prime depends on the compiler version: for gcc 6 or earlier, 126271 does the job, and for gcc 7 or later, 107897 will work. Run the code below in Custom Invocation and see what output you get.

#include <ctime>
#include <iostream>
#include <unordered_map>
using namespace std;

const int N = 2e5;

void insert_numbers(long long x) {
clock_t begin = clock();
unordered_map<long long, int> numbers;

for (int i = 1; i <= N; i++)
numbers[i * x] = i;

long long sum = 0;

for (auto &entry : numbers)
sum += (entry.first / x) * entry.second;

printf("x = %lld: %.3lf seconds, sum = %lld\n", x, (double) (clock() - begin) / CLOCKS_PER_SEC, sum);
}

int main() {
insert_numbers(107897);
insert_numbers(126271);
}


Depending on which compiler version you are using, one of these two numbers will take much longer than the other. There are several other primes that also work; try some more for yourself!

Note that for other hash tables like cc_hash_table or gp_hash_table (see Chilli's helpful post), it's even easier to hack them. These hash tables use a modulo power of two policy, so in order to make a lot of collisions occur we can simply insert a lot of numbers that are equivalent, say, modulo 216.

Don't want to be hacked?

Let's look at how to safeguard these hash maps from collision attacks. To do this we can write our own custom hash function which we give to the unordered_map (or gp_hash_table, etc.). The standard hash function looks something like this:

struct custom_hash {
size_t operator()(uint64_t x) const {
return x;
}
};


However as we mentioned, any predictable / deterministic hash function can be reverse-engineered to produce a large number of collisions, so the first thing we should do is add some non-determinism (via high-precision clock) to make it more difficult to hack:

struct custom_hash {
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return x + FIXED_RANDOM;
}
};


See my post on making randomized solutions unhackable for more details. Awesome, so our hash is perfectly safe now, right? Not so fast. All we've done is add the same fixed number to every input to the function. But if two numbers a and b satisfy a = b (mod m), then a + x = b + x (mod m) for every x as well. Similar problems occur for other very simple hash functions: multiplying by a random large odd number (and overflowing mod 264) is likely effectively modulo p, but will be problematic for gp_hash_table's power of two policy; the same situation occurs for xor-ing with a random number.

A slightly better hash function like the following may look enticing:

struct custom_hash {
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
x ^= FIXED_RANDOM;
return x ^ (x >> 16);
}
};


However, if you are using a gp_hash_table this actually still leaves you susceptible to hacks from a strong enough adversary. In particular, after inserting the numbers (1 << 16) + 1, (2 << 16) + 2, (3 << 16) + 3, ..., into this hash table, all of the outputs will be equivalent modulo 216. (Do you see why?)

So we want a better hash function, ideally one where changing any input bit results in a 50-50 chance to change any output bit. Note for example that in the hash function x + FIXED_RANDOM, this property is not satisfied at all; for example, changing a higher bit in x results in a 0% chance of changing a lower bit of the output.

Personally, I like to use splitmix64, which is extremely high-quality and fast; credit goes to Sebastiano Vigna for designing it. Adding all this together, we have our safe custom hash function:

struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
// http://xorshift.di.unimi.it/splitmix64.c
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}

size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};


Now we can simply define our unordered_map or our gp_hash_table as follows:

unordered_map<long long, int, custom_hash> safe_map;
gp_hash_table<long long, int, custom_hash> safe_hash_table;


Once we use these in our program above, it runs very quickly:

x = 107897: 0.035 seconds, sum = 2666686666700000
x = 126271: 0.031 seconds, sum = 2666686666700000

=====
Used: 109 ms, 9204 KB

• +752

| Write comment?
 » 5 years ago, # | ← Rev. 2 →   +1 Added to my snippets ;). Amazing Work.
 » 5 years ago, # |   0 c++ 17 when set with same key has size larger than 8 it will use RBT to store data.
•  » » 5 years ago, # ^ | ← Rev. 2 →   +8 Are you saying unordered_set transitions to using red-black tree when it encounters 8 collisions in the same location? This isn't true. In the code snippet I posted above, insert_numbers(107897) in G++17 takes about as long as insert_numbers(126271) in G++14.
•  » » » 5 years ago, # ^ |   +3 Oh, i am wrong,it was implemented in jdk1.8.
•  » » » » 5 years ago, # ^ |   0 Do you have a link to where it says this? I'm interested in reading the documentation for it.
•  » » » » » 5 years ago, # ^ |   +3
•  » » » » » 3 years ago, # ^ |   -8 do tell me some computer science books to read.Especially ones that have bizzare concepts and algorithms.PS: Big Fan and already have CLRS, CP handbook etc. Also i need help cause i am not from Computer Science Background.
 » 5 years ago, # | ← Rev. 2 →   +3 very useful blog ✋✋ 
•  » » 5 years ago, # ^ |   0 Yes very useful blog
 » 5 years ago, # |   +5 How convenient! Thanks a lot!
 » 5 years ago, # |   0 very cool. keep it up!
 » 5 years ago, # |   0 Cool! I'm curious how many people actually do anti-hashing hacks in contest. Could you put the standard unordered_map runtimes on the inputs to use as comparisons to the benchmarks you put at the end? Or does it simply take way too much time to even record?
•  » » 5 years ago, # ^ |   +10 The thing about this specific hack is that if anyone successfully makes this hack on anyone else in the contest, their test will be added to system tests which will leave you in trouble. A few examples of recent problems where you can fail for using unprotected unordered_map include 1027F - Session in BSU and 1039C - Network Safety.As far as runtime, it gets a bit slower with the custom hash but not too much. Pure unordered_map gives anywhere between 0.00s and 0.04s on non-adversarial cases when running with Custom Invocation, vs. 0.03s with custom hash. If you're concerned with speed then gp_hash_table with the custom hash is the way to go, since it uses power of two modding and linear probing rather than prime modding and collision chaining.
•  » » » 5 years ago, # ^ |   0 Oh, I wasn't that concerned about the speed of your custom hash. I was curious about the speed of std::unordered_map on the adversarial case that you've created. However, reading it more closely, you have N = 105, so if it really is causing an O(n2) blowup on std::unordered_map, then it's probably too slow to bother recording the time.
•  » » » » 5 years ago, # ^ |   +5 Ah. Run the code from the post in Custom Invocation :)
 » 5 years ago, # | ← Rev. 2 →   +21 I like (uintptr_t)main. :) This pointer should be random for every run because of OS security issue.
•  » » 5 years ago, # ^ | ← Rev. 2 →   +3 Interesting idea. Unfortunately when I tried it on Codeforces just now, it gave the same result every time. :(
•  » » » 5 years ago, # ^ |   +10 Really!? That's too sad. T_T It worked for most other OJ platforms...
•  » » » » 5 years ago, # ^ |   0 You may try new char
 » 5 years ago, # | ← Rev. 2 →   +16 Nicely written, as always. Thanks!Whenever someone talks about hacking hashmaps, I think of this problem: https://ipsc.ksp.sk/2014/real/problems/h.html
 » 5 years ago, # | ← Rev. 2 →   -13 Thanks for this helpful blog. For completeness, it should be noted that the last definitiongp_hash_table safe_hash_table; should be preceded by #include using namespace __gnu_pbds; The following is a slight update to your test program. Update
 » 5 years ago, # |   +5 How does it compare with alternating max_load_factor of the hash table? because it is runs slower as compared to this trick (Arpa's Blog): unordered_set numbers; numbers.max_load_factor(0.25); numbers.reserve(1<<18); this gives output of: x = 107897: 0.018 seconds, sum = 2666686666700000 x = 126271: 0.031 seconds, sum = 2666686666700000 ===== Used: 61 ms, 10600 KB 
•  » » 5 years ago, # ^ |   0 This doesn't make it unhackable, it just changes the prime number that breaks it. Try calling insert_numbers(1056323); instead: x = 107897: 0.043 seconds, sum = 2666686666700000 x = 126271: 0.015 seconds, sum = 2666686666700000 x = 1056323: 1.149 seconds, sum = 2666686666700000 ===== Used: 1201 ms, 10456 KB 
•  » » » 5 years ago, # ^ |   0 I am not sure I understand how it "only" changes the prime number because according to the code, you are inserting numbers with same modulo wrt the prime. Running on equal modulo numbers with: unordered_set numbers; numbers.max_load_factor(0.25); numbers.reserve(1<<20); // NOTICE THE CHANGE which gives output of: x = 107897: 0.010 seconds, sum = 2666686666700000 x = 126271: 0.015 seconds, sum = 2666686666700000 x = 1056323: 0.016 seconds, sum = 2666686666700000 ===== Used: 78 ms, 14864 KB Also reserve must change according to the elements to be inserted (upper bound to be a power of two). Maybe it's because of rehash scheme when max_load_factor is achieved in the bucket under consideration.
•  » » » » 5 years ago, # ^ |   0 When you call .reserve() you are changing the internal capacity of the map, which means you are effectively changing the internal prime number modulo it uses out of this list.So yes if you change the capacity again, it will work well on the previous prime number I gave you, but there will be a new number in the list that is problematic.
•  » » » » » 4 months ago, # ^ |   0 How do you find the prime number from the list which will be able to cause a blowup?
•  » » » 4 years ago, # ^ | ← Rev. 2 →   0 What if i need unordered_map , int> mp; here first is pair . What if more complex such as use (1,2,3,4) as first , i meant for struct data type first .
•  » » » » 4 years ago, # ^ |   0 Read the comment right below this one. https://codeforces.com/blog/entry/62393?#comment-464775
•  » » » » » 12 months ago, # ^ |   -11 are you too lazy, to copy comment?
 » 5 years ago, # |   0 like splitmix64 is there a good hash function for pairs too?
•  » » 5 years ago, # ^ |   0 If you have a pair of integers you'd like to hash, you can use the custom hash function above on each of them to get two values a and b. Then combine them in any way you like, e.g., a + b.The one issue with a + b is that swapping the two elements of the pair will lead to the same hash value. This isn't a problem from a theory point of view since "O(1) collisions on average" is still valid, but to avoid this situation you can switch to a non-symmetric function such as 3 * a + b or a ^ (b >> 1).
•  » » » 4 years ago, # ^ | ← Rev. 2 →   0 And how would you go about using unordered_set with strings as keys?Here is an idea to use a random seed in the MurmurHashUnaligned2 which is the hash function that C++ uses by default for hashing strings: https://stackoverflow.com/a/34976823/10017885 although here it is written that even with using a randomized seed MurmurHash can be hacked: https://en.wikipedia.org/wiki/MurmurHash#Vulnerabilities
•  » » » » 4 years ago, # ^ | ← Rev. 2 →   0 sha256(constant random string + desired string) --> never hacked againBut I doubt anyone would care enough to hack your murmurhash solution, if you ever used it.
•  » » » 4 years ago, # ^ |   0 And what fuction would you recommend for hashing ints?
•  » » » » 4 years ago, # ^ |   0 Read the post.
•  » » » » » 4 years ago, # ^ | ← Rev. 2 →   0 In your post you provide a function for hashing long longs and I am interested in a good function for hashing ints.Well, I suppose the same function would also work but maybe for ints we could have a function that is faster and also works.
•  » » 5 years ago, # ^ |   0 Using the idea in neal's comment struct custom_hash { static uint64_t splitmix64(uint64_t x) { // http://xorshift.di.unimi.it/splitmix64.c x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(pair x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x.first + FIXED_RANDOM)^(splitmix64(x.second + FIXED_RANDOM) >> 1); } }; 
 » 4 years ago, # | ← Rev. 7 →   -10 Thanks for this blog, neal. Can you recommend a fast hash function that is not difficult to remember (for gp_hash_table)?Can xorshift64 be such function? uint64_t xorshift64(uint64_t x) { x ^= x << 13; x ^= x >> 7; x ^= x << 17; return x; }  UPD. Okay, this is bad hash function.Next code: #include typedef uint64_t ull; uint64_t hash(uint64_t x) { x ^= x << 13; x ^= x >> 7; x ^= x << 17; return x; } int main() { int cnt = 0; for (ull x = ull(1) << 0; x < (ull(1) << 32); x++) { cnt += ((hash(x) & ((1 << 20) - 1)) == 0); } std::cout << cnt << std::endl; } returns cnt = 4095.UPD2. I got idea about calculation polinomial hash from s, where x = s[0]+(s[1]<<16)+(s[2]<<32)+(s[3]<<48). Tested it and it is fast.
 » 4 years ago, # |   0 very helpful !!you write very good and you need just another blog like this one to be in "Top contributors List".
 » 4 years ago, # |   0 neal Why use size_t as the return value of operator(), why not int64_t, does it affect the performance of functions
 » 4 years ago, # |   +11 #include #include #include #include using namespace std; const int N = 2e5; void insert_numbers(long long x) { clock_t begin = clock(); unordered_map numbers; mt19937 rng(0); for (int i = 0; i < N; i++) numbers[uniform_int_distribution(0, 1e6)(rng) * x] = i; long long sum = 0; for (auto &entry : numbers) sum += (entry.first / x) * entry.second; printf("x = %lld: %.3lf seconds, sum = %lld\n", x, (double) (clock() - begin) / CLOCKS_PER_SEC, sum); } int main() { insert_numbers(107897); } Why does this code take more than 2 seconds in custom invocation with C++17, while the same code with the 1e6 replaced by 1e9 takes less than 100 ms?(also, replacing 1e6 by 1e5 makes the running time over 10 seconds)
•  » » 4 years ago, # ^ |   +11 Good question. This is actually quite tricky. It's because the default hash function returns a size_t, and on Codeforces size_t is a 32-bit integer. This means that multiplying by an integer up to 1e9 actually overflows 32 bits when hashed and ends up with a number that is no longer a multiple of our prime.
 » 4 years ago, # |   0 I didn't get it. So if I have an array like [1,1,1,1......,1], your hash function is not deterministic because hash(1) != hash(1) because it uses some FIXED_RANDOM. If the FIXED_RANDOM would be the same for all numbers, then I think we are the begining. Correct me if I am wrong. Thanks.
 » 4 years ago, # |   0 Just wanted to ask this, that for largest value possible in long long int x, this x += 0x9e3779b97f4a7c15 expression will overflow bounds of uint64.Also the argument for hash requires unsigned int64 value, but if we have negative numbers to hash too, then what happens.Thanks.
 » 4 years ago, # |   +1 Thanks a lot for this post! It was in detail and very useful..
 » 4 years ago, # |   0 Absolutely beautiful!
 » 4 years ago, # | ← Rev. 3 →   0 Hey Nice Blog, But I have a doubt.... I have submitted same code(both have your custom_hash). Problem : Social Network My Solutions : unordered_map , unordered_setI have a doubt that, i am getting TLE while using custom_hash with unordered set, but got ac while using same custom hash in unordered map. is there any reason for this? does your custom hash works faster on map than set or anything else?
•  » » 4 years ago, # ^ |   0 It's not the custom hash. Your exist function passes the entire set by value instead of by reference.
•  » » » 4 years ago, # ^ |   +8 Got it !! And sorry for asking wrong question.btw, thanks got ac by making it refernce.
•  » » » » 4 years ago, # ^ |   -8 Can you please send the improved code?
•  » » » » » 4 years ago, # ^ |   0 means??
 » 4 years ago, # |   -8 Can we use this custom hash in unordered set as well??
•  » » 4 years ago, # ^ |   0 yes, u can see my code.
•  » » » 4 years ago, # ^ |   0 Can u please give link to your code
•  » » » » 4 years ago, # ^ |   0
 » 4 years ago, # |   0 Using map mp; gives me TLE.I want to use Unordered_map to avoid TLE.How can i make it? Any help is appreciated. Thanks.Question : 1225D - Power ProductsMy Submission : 77023902neal Please help
•  » » 4 years ago, # ^ | ← Rev. 2 →   0 The complexity of your program with map is $O(n^2)$, assuming that $a_i \leq n$. Using an unordered_map will just remove a log factor, try improving your complexity by more than that.
•  » » » 9 months ago, # ^ |   0 Hi, how exactly can we write a hash for a vector?
 » 4 years ago, # |   0 size_t is 32 bit in 32 bit compilers. Is using 64 bit hash function splitmix64 good then? Or do you know any better hash function for 32 bit?
•  » » 4 years ago, # ^ |   +5 I think this comment on stack overflow is quite great.https://stackoverflow.com/a/12996028/4275047
 » 3 years ago, # |   0 I'm getting this weird compiler warning on macOS when I make a basic unordered_map: ld: warning: direct access in function 'std::_Hashtable, std::allocator >, std::__detail::_Select1st, std::equal_to, custom_hash, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits >::_M_rehash_aux(unsigned long, std::integral_constant)' from file '/var/folders/_4/k4frvr6d1h75g4ggfm5kbs580000gn/T//ccK8c6lm.o' to global weak symbol 'custom_hash::operator()(unsigned long long) const::FIXED_RANDOM' from file '/var/folders/_4/k4frvr6d1h75g4ggfm5kbs580000gn/T//ccK8c6lm.o' means the weak symbol cannot be overridden at runtime. This was likely caused by different translation units being compiled with different visibility settings. These are my compiler flags g++-9 -std=c++17 -O2 -Wl,-stack_size -fsanitize=undefined -Wl, 0x10000000 -Wall -Wshadow -Wextra -o $1$1.cpp 
 » 3 years ago, # |   +1 Does this custom hash increases running time because i used this custom hash in a problem and it got Time Limit Exceeded as verdict and without custom hash function it got accepted Link to Accepted solution and Link to TLE solution .neal or somebody please help...
•  » » 3 years ago, # ^ |   0  We always assume hash maps are O(1) per operation (insert, erase, access, etc.). But this depends on a key assumption, which is that each item only runs into O(1) collisions on average. If our input data is completely random, this is a reasonable assumption. But this is no longer a safe bet when the input isn't random, especially so if someone is adversarially designing inputs to our codeSo if the input is random, custom hash will be worse.
 » 3 years ago, # |   +3 i tried using the above hash function for this quesn — https://www.codechef.com/LRNDSA10/problems/MATTEGbut i still get TLEmy solution — https://www.codechef.com/submit/complete/37329776what am i doing wrong?
 » 3 years ago, # | ← Rev. 2 →   0 Hi I have tried to change (unordered_)map to many thing like this ones but every time I get TLE on last testcase; I think this idea should be change but if anybody can help me, I ll be happy. link of submission
 » 3 years ago, # |   +3 I ran into this problem while upsolving. Turns out that test case 31 problem F from round 701 was specifically designed to blow up unordered maps. Quite nasty to do that but at least I learnt something. I'm glad I found your post because I had no idea what was going on.
 » 3 years ago, # |   0 Can unordered set collation cause wrong answer ? If anyone know plz reply.
 » 2 years ago, # |   +6 Bump because of recent contest hacks on problem C for this reason.
•  » » 2 years ago, # ^ |   +9 Bump
 » 17 months ago, # | ← Rev. 2 →   0 Absolutely perfect! I wanted to increase my knowledge upon this matter and understand what is going underneath the hood explaining the so much hacks we've seen in recent contests for UNORDERED hash map. I think it is not safe at all to use that unordered version.. This blog is bumpped by hacks every now and then lol
 » 16 months ago, # |   0 My submission for 1561D1 - Up the Strip (simplified version) is getting TLEed using your custom hash! Submission : 164724313
•  » » 16 months ago, # ^ |   +1 Have you considered the possibility that your algorithm might be garbage?When runtime is important, don't use a hash map unless you absolutely have to. It's one of the slowest O(1) algorithms.
•  » » 12 months ago, # ^ |   0
 » 14 months ago, # | ← Rev. 2 →   0 Without using custom_hash : x = 107897: 0.119 seconds, sum = 2666686666700000 x = 126271: 0.119 seconds, sum = 2666686666700000 using custom_hash. : x = 107897: 0.154 seconds, sum = 2666686666700000 x = 126271: 0.164 seconds, sum = 2666686666700000 :using c++ 17.why time is not decreasing ? neal
•  » » 14 months ago, # ^ |   +13 It depends on your specific compiler version. Try some other primes from the list above until you figure out which one is bad for yours in particular
 » 12 months ago, # |   0 TLE solution with your custom hash : 2206 msAC solution with normal map : 118 msWhy?Can you please clarify why this had happened?
•  » » 12 months ago, # ^ |   +8 I think .clear() is very slow for hash maps in general
•  » » » 12 months ago, # ^ |   0 I also thought that but don't know why it is technically very slow ,can you please come up with details what are the technical reasons .clear() is slow if you have time someday?
•  » » » » 12 months ago, # ^ | ← Rev. 2 →   +1 It's due to a bug on GCC, clear() works in a time complexity of $O(\mathbf{capacity})$. However, due to the bug, clear() does not clear the capacity (i.e. "deallocate") after clearing, therefore the repeated use of the function takes a massive amount of time.
•  » » » » » 12 months ago, # ^ |   0 this bug is in every version of gcc or just in gcc 9.2.1 of atcoder?
•  » » » » » » 12 months ago, # ^ |   0 The bug still exists in the latest version (at least up to GCC 11, from what I know) on major Online Judges.
 » 4 months ago, # |   0 Why always using these cryptic hashfunctions with XORs and Shift operations?Carter-Wegman should do the job perfectly fine, right? https://en.wikipedia.org/wiki/Universal_hashing#Hashing_integers mt19937 gen(chrono::steady_clock::now().time_since_epoch().count()); ll ra = gen(), rb = gen(); // shall be two random numbers ll p = (1 << 31) - 1; // prime > |U| - bigger than universe size ll carter_wegmann(ll a){ return (ra * a + rb) % p; // the hashmap does a subsequent mod bucket_count } 
•  » » 4 months ago, # ^ |   0 This looks like it'll probably work fine, but a few reasons that come to mind: mod is much slower than xor, shift, and multiplication with a fixed constant splitmix64 as mentioned above is a perfect bijective mapping from $[0, 2^{64})$ onto itself; what you wrote above compresses 64-bit numbers into 31-bit numbers which may not be ideal
•  » » » 4 months ago, # ^ |   0 regarding mod: It seems like mod p with p being a Mersenne prime is actually pretty cheap because it can be implemented with bitshifts and addition (See: https://en.wikipedia.org/wiki/Lehmer_random_number_generator#Implementation and https://sortingsearching.com/2020/05/21/hashing.html) Tomek Czajka (the second link) even says that good compilers do that automatically. regarding the mapping: Yeah thats probably something this can't do. There are other Mersenne Primes (https://oeis.org/A000043) and $2^{61} - 1$ seems to be the greatest below $2^{64}$. Thus the interval can be expanded. But surely its no bijection. So, ... thanks for pointing out the advantages ;)
 » 4 months ago, # |   0 **https://cses.fi/problemset/task/1621** I tried to solve this problem with standard unordered_set and got Tle in the last test case...then i Used the custom_hash and got an AC . Really insightful post. Thank You
 » 3 months ago, # |   0 For some reason, $107897$ and $126271$ no longer seem to result in an $n^2$ blow-up on C++ 20.I'm not sure exactly why this is the case, since they worked almost a year ago when I made the tests for https://codeforces.com/contest/1748/problem/C. This solution used to get TLE, but now passes only on C++ 20.I have tried several other primes from hashtable-aux.cc and none of them seem to work. neal
•  » » 3 months ago, # ^ |   +8 That's not too surprising on a new compiler version. Some of the primes should still work though. Keep trying more!
 » 12 days ago, # |   -24 Hi Neal! Thank you a lot for the post! Why I don't see any improvements with your hash? I am using the g++ -std=c++20Without your hash x = 107897: 0.055 seconds, sum = 2666686666700000 x = 126271: 0.039 seconds, sum = 2666686666700000 And with your hash x = 107897: 0.069 seconds, sum = 2666686666700000 x = 126271: 0.045 seconds, sum = 2666686666700000 On average, they look similar and I didn't see the performance difference as it was described in the post. Why?
•  » » 12 days ago, # ^ |   +10 The hash decreases the probability of an O(n^2) blowup, it doesn't change the complexity or commit to some improvement in the other cases, so...
•  » » 4 days ago, # ^ |   0 .ComplaintFrame { display: inline-block; position: absolute; top: 0; right: -1.4em; } .ComplaintFrame a { text-decoration: none; color: #ff8c00; opacity: 0.5; } .ComplaintFrame a:hover { opacity: 1; } ._ComplaintFrame_popup p, ._ComplaintFrame_popup button { margin-top: 1rem; } ._ComplaintFrame_popup input[type=submit] { padding: 0.25rem 2rem; } ._ComplaintFrame_popup ul { margin-top: 1em !important; margin-bottom: 1em !important; } Based on my recent tests, using $172933$ and $85229$ with g++ -std=c++20 version yields better results. In fact, for different g++ versions, you need to find different numbers, as neal mentioned. You can test within the prime number table to find them.