-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunordered_map.cpp
53 lines (47 loc) · 1.76 KB
/
unordered_map.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include<bits/stdc++.h>
using namespace std;
/// https://codeforces.com/blog/entry/62393
const int N = 2e5;
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);
}
};
//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<uint64_t,uint64_t> x) const { ///For Pair
// 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);
// }
//};
void insert_numbers(long long x) {
double start_time = clock();
unordered_map<long long, int, custom_hash> 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 : sum = %lld\n", x, sum);
double end_time = clock();
cerr<<"Time = "<<fixed<<setprecision(10)<<(end_time - start_time) / CLOCKS_PER_SEC<<'\n';
}
int main() {
insert_numbers(107897); ///custom hash- 0.21second and without custom hash 10second;
insert_numbers(126271);
}