-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path772d.cpp
More file actions
176 lines (138 loc) · 3.86 KB
/
772d.cpp
File metadata and controls
176 lines (138 loc) · 3.86 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pi = pair<ll, ll>;
using vi = vector<ll>;
using vpi = vector<pi>;
using vb = vector<bool>;
const ll ALPHABET_SIZE = 3;
const ll mod = 1e9 + 7;
// trie node
struct TrieNode {
struct TrieNode *children[ALPHABET_SIZE];
// isEndOfWord is true if the node represents
// end of a word
bool isEndOfWord;
};
// Returns new trie node (initialized to NULLs)
struct TrieNode *getNode(void) {
struct TrieNode *pNode = new TrieNode;
pNode->isEndOfWord = false;
for (ll i = 0; i < ALPHABET_SIZE; i++)
pNode->children[i] = NULL;
return pNode;
}
// If not present, inserts key llo trie
// If the key is prefix of trie node, just
// marks leaf node
void insert(struct TrieNode *root, string key) {
struct TrieNode *pCrawl = root;
for (ll i = 0; i < key.length(); i++) {
ll index = key[i] - '0';
if (!pCrawl->children[index])
pCrawl->children[index] = getNode();
pCrawl = pCrawl->children[index];
}
// mark last node as leaf
pCrawl->isEndOfWord = true;
}
// Returns true if key presents in trie, else
// false
bool search(struct TrieNode *root, string key) {
struct TrieNode *pCrawl = root;
for (ll i = 0; i < key.length(); i++) {
ll index = key[i] - '0';
if (!pCrawl->children[index])
return false;
pCrawl = pCrawl->children[index];
}
return (pCrawl->isEndOfWord);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n;
cin >> n;
ll p;
cin >> p;
vi a(n);
for (ll i = 0; i < n; i++) {
cin >> a[i];
}
vector<string> bits(n);
for (ll i = 0; i < n; i++) {
string s;
bool found = false;
bool last0 = false;
for (ll b = 35; b >= 0; b--) {
if ((1LL << b) & a[i]) {
s += '1';
found = true;
last0 = false;
} else if (found && !last0) {
s += '0';
last0 = true;
} else if (found && last0) {
s.pop_back();
s += '2';
last0 = false;
}
}
bits[i] = s;
}
for (int i = 0; i < n; i++) {
ll lst = -1;
for (int j = 1; j < bits[i].length(); j++) {
if (bits[i][j - 1] == '1' && bits[i][j] == '2') {
lst = j;
}
if (bits[i][j - 1] == '1' && bits[i][j] == '0') lst = -1;
if (bits[i][j] == '0' && lst != -1) {
bits[i][lst] = '0';
bits[i][j] = '2';
}
}
}
struct TrieNode *root = getNode();
for (ll i = 0; i < n; i++)
insert(root, bits[i]);
vi reachable(2e5 + 8);
reachable[0] = 1;
reachable[1] = 1;
for (int i = 2; i < 2e5 + 8; i++) {
reachable[i] += reachable[i - 1] + reachable[i - 2];
reachable[i] %= mod;
}
vi psum(2e5 + 9);
for (int i = 0; i < 2e5 + 8; i++) {
psum[i + 1] = psum[i] + reachable[i];
psum[i + 1] %= mod;
}
ll tot = 0;
function<ll(TrieNode *, ll, ll)> dfs = [&](TrieNode *v, ll val, ll len) {
ll cursum = 0;
for (ll i = 0; i < 3; i++) {
if (!v->children[i]) continue;
ll va = dfs(v->children[i], i, len + (i == 2 ? 2 : 1));
cursum += va % mod;
cursum %= mod;
}
ll reach = 0;
if (v->isEndOfWord && len > 0) {
reach += (p - len < 0 ? 0 : psum[p - len + 1]);
reach %= mod;
reach -= cursum;
reach %= mod;
tot += reach;
tot %= mod;
}
if (val == 0) return 0LL;
return (reach + cursum) % mod;
};
dfs(root, -1, 0);
tot %= mod;
if (tot < 0) tot += mod;
tot %= mod;
cout << tot << endl;
return 0;
}