-
Notifications
You must be signed in to change notification settings - Fork 0
/
1045.cpp
62 lines (56 loc) · 949 Bytes
/
1045.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
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
int hashTable[100010] = {0};
int gbc(int a, int b){
if(b == 0){
return a;
}
return gbc(b, a % b);
}
bool isprime(int x){
for(int i = 1; i <= (int)sqrt(x); i++){
if(gbc(x, i) != 1){
return false;
}
}
return true;
}
int main(){
int ms, n, m;
scanf("%d %d %d", &ms, &n, &m);
while(!isprime(ms)){
ms++;
}
for(int i = 0; i < n; i++){
int key;
scanf("%d", &key);
int h;
for(int j = 0; j < ms; j++){
h = (key + j * j) % ms;
if(hashTable[h] == 0){
hashTable[h] = key;
break;
}
if(j == ms - 1){
printf("%d cannot be inserted.\n", key);
}
}
}
double ans = 0.0;
for(int i = 0; i < m; i++){
int q;
scanf("%d", &q);
int j = 0, hq;
for(; j <= ms; j++){
ans++;
hq = (q + j * j) % ms;
if(hashTable[hq] == q || hashTable[hq] == 0){
break;
}
}
}
printf("%.1lf\n", ans / (1.0 * m));
return 0;
}