File size: 1,162 Bytes
ff444f7 |
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 |
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const int MAX = 1 << 13;
int solve() {
int N, K;
cin >> N >> K;
vector<int> A(N);
for (int i = 0; i < N; i++) {
cin >> A[i];
A[i] %= K;
}
vector<int> first_sofar(K, N + 1);
vector<vector<int>> first(N, vector<int>(K));
for (int i = N - 1; i >= 0; --i) {
int d = 0;
for (int j = i; j < N; ++j) {
d = (d + A[j]) % K;
first_sofar[d] = min(first_sofar[d], j + 1);
}
first[i] = first_sofar;
}
vector<int> dist(MAX, N + 1);
vector<bool> visit(MAX);
dist[0] = 0;
for (;;) {
int x = -1;
for (int j = 0; j < MAX; ++j) {
if (!visit[j] && dist[j] < N && (x == -1 || dist[x] > dist[j])) {
x = j;
}
}
if (x == -1) {
break;
}
visit[x] = 1;
for (int d = 0; d < K; d++) {
dist[x ^ d] = min(dist[x ^ d], first[dist[x]][d]);
}
}
int res = 0;
for (int i = 0; i < MAX; ++i) {
res += (dist[i] != N + 1);
}
return res;
}
int main() {
int T;
cin >> T;
for (int t = 1; t <= T; t++) {
cout << "Case #" << t << ": " << solve() << endl;
}
return 0;
}
|