#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
const int MAXN = 10000005;
const int B = 3162;
bool has_stone[MAXN];
vector<int> small_primes;
int cnt[450][B + 5];
int max_p[450];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, q;
if (!(cin >> n >> q)) return 0;
vector<bool> is_p(B + 1, true);
//sito erastotenesa
for (int i = 2; i <= B; i++) {
if (is_p[i]) {
small_primes.push_back(i);
for (int j = i * i; j <= B; j += i) is_p[j] = false;
}
}
int total_stones = 0;
vector<int> active_stones;
vector<int> results;
results.reserve(q);
for (int i = 0; i < q; i++) {
int x;
cin >> x;
bool adding = !has_stone[x];
has_stone[x] = adding;
total_stones += (adding ? 1 : -1);
int current_max = 0;
for (size_t j = 0; j < small_primes.size(); j++) {
int p = small_primes[j];
int rem = x % p;
if (adding) {
cnt[j][rem]++;
max_p[j] = max(max_p[j], cnt[j][rem]);
}
else {
if (cnt[j][rem] == max_p[j]) {
cnt[j][rem]--;
int new_max = 0;
for (int k = 0; k < p; k++) {
new_max = max(new_max, cnt[j][k]);
}
max_p[j] = new_max;
}
else {
cnt[j][rem]--;
}
}
current_max = max(current_max, max_p[j]);
}
if (current_max < n / B + 2 && total_stones > 0) {
if (adding) {
active_stones.push_back(x);
}
else {
active_stones.erase(find(active_stones.begin(), active_stones.end(), x));
}
int max_large = 0;
for (int stone : active_stones) {
if (stone == x) continue;
int dist = abs(stone - x);
if (dist > B && (n / dist + 1 > current_max)) {
int count = 0;
int rem = x % dist;
for (int pos = (rem == 0 ? dist : rem); pos <= n; pos += dist) {
if (has_stone[pos]) count++;
}
max_large = max(max_large, count);
}
}
current_max = max(current_max, max_large);
}
else if (total_stones > 0) {
active_stones.clear();
}
if (total_stones == 0) current_max = 0;
results.push_back(current_max);
}
for (int res : results) {
cout << res << "\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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | #include <iostream> #include <vector> #include <cmath> #include <algorithm> using namespace std; const int MAXN = 10000005; const int B = 3162; bool has_stone[MAXN]; vector<int> small_primes; int cnt[450][B + 5]; int max_p[450]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, q; if (!(cin >> n >> q)) return 0; vector<bool> is_p(B + 1, true); //sito erastotenesa for (int i = 2; i <= B; i++) { if (is_p[i]) { small_primes.push_back(i); for (int j = i * i; j <= B; j += i) is_p[j] = false; } } int total_stones = 0; vector<int> active_stones; vector<int> results; results.reserve(q); for (int i = 0; i < q; i++) { int x; cin >> x; bool adding = !has_stone[x]; has_stone[x] = adding; total_stones += (adding ? 1 : -1); int current_max = 0; for (size_t j = 0; j < small_primes.size(); j++) { int p = small_primes[j]; int rem = x % p; if (adding) { cnt[j][rem]++; max_p[j] = max(max_p[j], cnt[j][rem]); } else { if (cnt[j][rem] == max_p[j]) { cnt[j][rem]--; int new_max = 0; for (int k = 0; k < p; k++) { new_max = max(new_max, cnt[j][k]); } max_p[j] = new_max; } else { cnt[j][rem]--; } } current_max = max(current_max, max_p[j]); } if (current_max < n / B + 2 && total_stones > 0) { if (adding) { active_stones.push_back(x); } else { active_stones.erase(find(active_stones.begin(), active_stones.end(), x)); } int max_large = 0; for (int stone : active_stones) { if (stone == x) continue; int dist = abs(stone - x); if (dist > B && (n / dist + 1 > current_max)) { int count = 0; int rem = x % dist; for (int pos = (rem == 0 ? dist : rem); pos <= n; pos += dist) { if (has_stone[pos]) count++; } max_large = max(max_large, count); } } current_max = max(current_max, max_large); } else if (total_stones > 0) { active_stones.clear(); } if (total_stones == 0) current_max = 0; results.push_back(current_max); } for (int res : results) { cout << res << "\n"; } return 0; } |
English