#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define sn second
typedef long long ll;
typedef vector<int> VI;
typedef vector<bool> VB;
typedef vector<char> VC;
typedef pair<int, int> PI;
struct Candidate {
int prime;
int rest;
int time;
int val;
};
bool operator<(Candidate a, Candidate b) {
return a.val < b.val;
}
int main() {
int n, q;
cin >> n >> q;
VB is_prime(n, true);
if (n > 0) is_prime[0] = false;
if (n > 1) is_prime[1] = false;
for (ll i = 2; i * i < n; i++) {
if (is_prime[i]) {
for (ll j = i * i; j < n; j += i) {
is_prime[j] = false;
}
}
}
VI primes;
vector<VI> rests;
vector<VI> updated;
priority_queue<Candidate> Q;
unordered_map<int, bool> D;
for (int i = 2; i < n; i++) {
if (is_prime[i]) {
primes.pb(i);
// prime_to_idx[i] = (int)primes.size();
rests.pb(VI(i, 0));
updated.pb(VI(i, 0));
}
}
#ifdef DEBUG
cout << primes.size() << endl;
for (int j = 0; j < primes.size(); j++) {
cout << primes[j] << " ";
}
cout << endl;
#endif
int x;
for (int i = 1; i <= q; i++) {
cin >> x;
// x--;
int dif = -1;
if (!D[x]) {
dif = 1;
D[x] = true;
}
else {
D[x] = false;
}
// cout << "Diff: " << x << " + " << dif << endl;
for (int j = 0; j < primes.size(); j++) {
int mod = x % primes[j];
rests[j][mod] += dif;
updated[j][mod] = i + 1;
Candidate c;
c.prime = j;
c.rest = mod;
c.time = i + 1;
c.val = rests[j][mod];
Q.push(c);
}
#ifdef DEBUG
for (int j = 0; j < primes.size(); j++) {
cout << "P: " << primes[j] << endl;
for (int k = 0; k < primes[j]; k++) {
cout << " " << k << " ";
}
cout << endl;
for (int k = 0; k < primes[j]; k++) {
cout << " " << rests[j][k] << " ";
}
cout << endl;
}
#endif
while (!Q.empty()) {
Candidate c = Q.top();
if (updated[c.prime][c.rest] > c.time) {
// cout << "POP " << c.prime << " " << c.rest << endl;
Q.pop();
}
else {
break;
}
}
cout << Q.top().val << endl;
#ifdef DEBUG
// cout << Q.top().val << " " << primes[Q.top().prime] << endl;
if (x == 2) {
while (!Q.empty()) {
cout << Q.top().val << " " << primes[Q.top().prime] << endl;
Q.pop();
}
break;
}
#endif
}
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 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 | #include <bits/stdc++.h> using namespace std; #define pb push_back #define fi first #define sn second typedef long long ll; typedef vector<int> VI; typedef vector<bool> VB; typedef vector<char> VC; typedef pair<int, int> PI; struct Candidate { int prime; int rest; int time; int val; }; bool operator<(Candidate a, Candidate b) { return a.val < b.val; } int main() { int n, q; cin >> n >> q; VB is_prime(n, true); if (n > 0) is_prime[0] = false; if (n > 1) is_prime[1] = false; for (ll i = 2; i * i < n; i++) { if (is_prime[i]) { for (ll j = i * i; j < n; j += i) { is_prime[j] = false; } } } VI primes; vector<VI> rests; vector<VI> updated; priority_queue<Candidate> Q; unordered_map<int, bool> D; for (int i = 2; i < n; i++) { if (is_prime[i]) { primes.pb(i); // prime_to_idx[i] = (int)primes.size(); rests.pb(VI(i, 0)); updated.pb(VI(i, 0)); } } #ifdef DEBUG cout << primes.size() << endl; for (int j = 0; j < primes.size(); j++) { cout << primes[j] << " "; } cout << endl; #endif int x; for (int i = 1; i <= q; i++) { cin >> x; // x--; int dif = -1; if (!D[x]) { dif = 1; D[x] = true; } else { D[x] = false; } // cout << "Diff: " << x << " + " << dif << endl; for (int j = 0; j < primes.size(); j++) { int mod = x % primes[j]; rests[j][mod] += dif; updated[j][mod] = i + 1; Candidate c; c.prime = j; c.rest = mod; c.time = i + 1; c.val = rests[j][mod]; Q.push(c); } #ifdef DEBUG for (int j = 0; j < primes.size(); j++) { cout << "P: " << primes[j] << endl; for (int k = 0; k < primes[j]; k++) { cout << " " << k << " "; } cout << endl; for (int k = 0; k < primes[j]; k++) { cout << " " << rests[j][k] << " "; } cout << endl; } #endif while (!Q.empty()) { Candidate c = Q.top(); if (updated[c.prime][c.rest] > c.time) { // cout << "POP " << c.prime << " " << c.rest << endl; Q.pop(); } else { break; } } cout << Q.top().val << endl; #ifdef DEBUG // cout << Q.top().val << " " << primes[Q.top().prime] << endl; if (x == 2) { while (!Q.empty()) { cout << Q.top().val << " " << primes[Q.top().prime] << endl; Q.pop(); } break; } #endif } return 0; } |
English