#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int B = 600;
int cnt[B + 1][B + 1];
int max_val[B + 1];
int global_max_small = 0;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, q;
if (!(cin >> n >> q)) return 0;
vector<bool> A(n + 1, false);
int C = 0;
for (int i = 0; i < q; ++i) {
int x;
cin >> x;
bool is_add = !A[x];
A[x] = !A[x];
if (is_add) {
C++;
for (int K = 2; K <= B; ++K) {
int mod = x % K;
cnt[K][mod]++;
if (cnt[K][mod] > max_val[K]) {
max_val[K] = cnt[K][mod];
}
}
} else {
C--;
for (int K = 2; K <= B; ++K) {
int mod = x % K;
cnt[K][mod]--;
}
}
global_max_small = 0;
for (int K = 2; K <= B; ++K) {
if (!is_add && max_val[K] > 0) {
int true_max = 0;
for (int m = 0; m < K; ++m) {
if (cnt[K][m] > true_max) {
true_max = cnt[K][m];
}
}
max_val[K] = true_max;
}
if (max_val[K] > global_max_small) {
global_max_small = max_val[K];
}
}
int current_best = global_max_small;
int limit_K = min(n, n / max(1, current_best));
if (limit_K > B) {
for (int K = B + 1; K <= limit_K; ++K) {
int local_max = 0;
for (int start = 1; start <= min(K, n); ++start) {
int current_score = 0;
for (int pos = start; pos <= n; pos += K) {
if (A[pos]) current_score++;
}
if (current_score > local_max) {
local_max = current_score;
}
}
if (local_max > current_best) {
current_best = local_max;
}
}
}//.......................h................................
cout << current_best << "\n";
}
return 0;
}
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 | #include <iostream> #include <vector> #include <algorithm> using namespace std; const int B = 600; int cnt[B + 1][B + 1]; int max_val[B + 1]; int global_max_small = 0; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, q; if (!(cin >> n >> q)) return 0; vector<bool> A(n + 1, false); int C = 0; for (int i = 0; i < q; ++i) { int x; cin >> x; bool is_add = !A[x]; A[x] = !A[x]; if (is_add) { C++; for (int K = 2; K <= B; ++K) { int mod = x % K; cnt[K][mod]++; if (cnt[K][mod] > max_val[K]) { max_val[K] = cnt[K][mod]; } } } else { C--; for (int K = 2; K <= B; ++K) { int mod = x % K; cnt[K][mod]--; } } global_max_small = 0; for (int K = 2; K <= B; ++K) { if (!is_add && max_val[K] > 0) { int true_max = 0; for (int m = 0; m < K; ++m) { if (cnt[K][m] > true_max) { true_max = cnt[K][m]; } } max_val[K] = true_max; } if (max_val[K] > global_max_small) { global_max_small = max_val[K]; } } int current_best = global_max_small; int limit_K = min(n, n / max(1, current_best)); if (limit_K > B) { for (int K = B + 1; K <= limit_K; ++K) { int local_max = 0; for (int start = 1; start <= min(K, n); ++start) { int current_score = 0; for (int pos = start; pos <= n; pos += K) { if (A[pos]) current_score++; } if (current_score > local_max) { local_max = current_score; } } if (local_max > current_best) { current_best = local_max; } } }//.......................h................................ cout << current_best << "\n"; } return 0; } |
English