#include <bits/stdc++.h>
using namespace std;
//roziwazanie z https://www.youtube.com/live/jxYu1MGGjBc moze wejdzie na subpunkty ;(
bool plansza[10000009];
int n,q;
bool notprime[10000009];
unordered_set<int> p;
int md[10000009];
void sito() {
int q = sqrt(n);
for (int i = 2; i <= q; i++) {
if (!notprime[i]) {
for (int j = i + i; j < n; j += i) {
notprime[j] = true;
}
}
}
}
int sprawdz(int x) {
for (auto s: p) {
md[s%x]++;
}
int ans = 0;
for (auto s: p) {
ans = max(ans, md[s%x]);
md[s%x] = 0;
}
return ans;
}
int solve() {
int wyn = sprawdz(2);
int d = p.size();
for (int i = 3; i<=(2*n/d);i++) {
if (!notprime[i]) {
wyn = max(wyn,sprawdz(i));
}
}
return wyn;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> q;
sito();
while (q--) {
int x;
cin >> x;
if (!plansza[x]) {
p.insert(x);
}else {
p.erase(x);
}
plansza[x] = !plansza[x];
if (p.size() == 0) {
cout << 0 << endl;
continue;
}
cout << solve() << endl;
}
}
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 | #include <bits/stdc++.h> using namespace std; //roziwazanie z https://www.youtube.com/live/jxYu1MGGjBc moze wejdzie na subpunkty ;( bool plansza[10000009]; int n,q; bool notprime[10000009]; unordered_set<int> p; int md[10000009]; void sito() { int q = sqrt(n); for (int i = 2; i <= q; i++) { if (!notprime[i]) { for (int j = i + i; j < n; j += i) { notprime[j] = true; } } } } int sprawdz(int x) { for (auto s: p) { md[s%x]++; } int ans = 0; for (auto s: p) { ans = max(ans, md[s%x]); md[s%x] = 0; } return ans; } int solve() { int wyn = sprawdz(2); int d = p.size(); for (int i = 3; i<=(2*n/d);i++) { if (!notprime[i]) { wyn = max(wyn,sprawdz(i)); } } return wyn; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); cin >> n >> q; sito(); while (q--) { int x; cin >> x; if (!plansza[x]) { p.insert(x); }else { p.erase(x); } plansza[x] = !plansza[x]; if (p.size() == 0) { cout << 0 << endl; continue; } cout << solve() << endl; } } |
English