forked from marioyc/Online-Judge-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path241B.cpp
105 lines (72 loc) · 2.33 KB
/
241B.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
99
100
101
102
103
104
105
#include <cstdio>
using namespace std;
#define MAXN 50000
#define NUMB 30
#define MOD 1000000007
int trie[MAXN * NUMB][2],nodes = 1;
int cont[MAXN * NUMB],cont1[MAXN * NUMB][NUMB];
void insert(int a){
int pos = 0;
for(int i = 29;i >= 0;--i){
int b = (a >> i & 1);
if(trie[pos][b] == 0) trie[pos][b] = nodes++;
pos = trie[pos][b];
++cont[pos];
for(int j = 29;j >= 0;--j)
if(a >> j & 1) ++cont1[pos][j];
}
}
int a[MAXN];
int main(){
int n;
long long m;
scanf("%d %I64d",&n,&m);
for(int i = 0;i < n;++i){
scanf("%d",&a[i]);
insert(a[i]);
}
int lo = 0,hi = (1 << 30) - 1,mi;
while(lo < hi){
mi = (lo + hi + 1) >> 1;
long long total = 0;
for(int i = 0;i < n;++i){
int aux = (mi ^ a[i]),pos = 0;
for(int j = 29;j >= 0;--j){
int opp = (~a[i] >> j & 1);
if((mi >> j & 1) == 0 && trie[pos][opp])
total += cont[ trie[pos][opp] ];
int b = (aux >> j & 1);
if(trie[pos][b]) pos = trie[pos][b];
else break;
}
}
if(total < 2*m) hi = mi - 1;
else lo = mi;
}
long long ans = 0;
long long total = 0;
for(int i = 0;i < n;++i){
int aux = (lo ^ a[i]),pos = 0;
for(int j = 29;j >= 0;--j){
int opp = (~a[i] >> j & 1);
if((lo >> j & 1) == 0 && trie[pos][opp]){
total += cont[ trie[pos][opp] ];
long long sum = 0;
for(int k = 29;k >= 0;--k){
if(a[i] >> k & 1) sum = sum * 2 + cont[ trie[pos][opp] ] - cont1[ trie[pos][opp] ][k];
else sum = sum * 2 + cont1[ trie[pos][opp] ][k];
}
ans += sum;
}
int b = (aux >> j & 1);
if(trie[pos][b]) pos = trie[pos][b];
else break;
}
}
ans /= 2;
total /= 2;
if(total > m)
ans -= (long long)(total - m) * (lo + 1);
printf("%I64d\n",ans % MOD);
return 0;
}