-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path19-1212.cpp
98 lines (95 loc) · 1.61 KB
/
19-1212.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
using namespace std;
enum { UP = 0, IT = 1, GA = -1 };
class Entry {
public:
int key, value, status;
Entry() {
key = 0;
value = 0;
status = UP;
};
Entry(int key, int value) {
this->key = key;
this->value = value;
status = IT;
}
void erase() {
status = GA;
}
};
class HashTable {
private:
Entry* hashTable;
int capa;
int hashfunc(int key) {
return key % capa;
}
public:
int cur;
HashTable(int capa) {
cur = 1;
this->capa = capa;
hashTable = new Entry[this->capa + 1];
}
int rotate(int start, int key) {
int index = 0;
if (key >= 0) {
index = start + hashfunc(abs(key));
}
else {
index = start + (capa - hashfunc(abs(key)));
}
if (index >= capa) {
index %= capa;
if (index == 0) {
index = capa;
}
}
return index;
}
bool put(int index) {
if (hashTable[index].status == IT) {
return false;
}
else {
hashTable[index] = Entry(index, index);
return true;
}
}
};
int check(HashTable* A, HashTable* B, int count) {
A->cur = A->rotate(A->cur, count);
B->cur = B->rotate(B->cur, -count);
if (A->put(A->cur)) {
return A->cur;
}
else {
A->cur = A->rotate(A->cur, -B->cur);
B->cur = B->rotate(B->cur, B->cur);
if (A->put(A->cur)) {
return A->cur;
}
else {
while (!A->put(A->cur)) {
A->cur = A->rotate(A->cur, 1);
B->cur = B->rotate(B->cur, -1);
}
return A->cur;
}
}
}
int main() {
int t, n, a, b, k;
cin >> t;
while (t--) {
cin >> a >> b >> n;
HashTable* A = new HashTable(a);
HashTable* B = new HashTable(b);
while (n--) {
cin >> k;
cout << check(A, B, k) << " ";
}
cout << endl;
}
}