#include <bits/stdc++.h>
using namespace std;
const int N = 1e7 + 10;
int pf[N];
vector<int> primes;
int K = 0;
int active[N];
int where[N];
const int M = 200;
int mod_count[M][2000];
int mem[25'000'000];
int *count_of_count[M];
int highest_count[M];
int C = 1;
int mark[N];
int current_count[N];
mt19937_64 gen(987);
int main() {
cin.tie(0)->sync_with_stdio(0);
auto T0 = clock();
memset(where, -1, sizeof where);
iota(pf, pf+N, 0);
for (int i = 2; i < N; i++) if (pf[i] == i) {
primes.push_back(i);
if (i > N/i + 1) continue;
for (int j = i*i; j < N; j += i) pf[j] = min(pf[j], i);
}
primes.resize(M);
auto T1 = clock();
cerr << fixed << setprecision(6);
cerr << "Sieve: " << double(T1-T0) / CLOCKS_PER_SEC << '\n';
count_of_count[0] = mem;
for (int i = 0; i+1 < M; i++) {
int p = primes[i];
count_of_count[i+1] = count_of_count[i] + 1 + (N+p-1) / p;
}
int n, q;
cin >> n >> q;
vector<int> xs(q);
for (int &x: xs) cin >> x;
auto update = [&](int x, bool skip_M = false) -> int {
bool insert = where[x] < 0;
if (insert) {
active[K] = x;
where[x] = K++;
} else {
int i = where[x];
int y = active[--K];
active[i] = y;
where[y] = i;
where[x] = -1;
}
if (skip_M) return 0;
int res = 0;
for (int i = 0; i < M; i++) {
int p = primes[i], r = x % p;
int &c = mod_count[i][r];
assert(c >= 0);
count_of_count[i][c]--;
c += insert ? 1 : -1;
count_of_count[i][c]++;
if (insert) {
if (highest_count[i] == c-1) highest_count[i]++;
} else {
if (count_of_count[i][highest_count[i]] == 0) highest_count[i]--;
}
res = max(res, highest_count[i]);
}
assert(res >= K/2);
return res;
};
vector<vector<tuple<int,int,int>>> best(q);
vector<int> res(q);
for (int t = 0; t < q; t++) {
res[t] = update(xs[t]);
if (K <= 1) continue;
set<int> done;
auto consider = [&](int p) {
if (p <= primes[M-1]) return;
if (res[t] >= (n+p-1)/p) return;
if (done.count(p)) return;
done.insert(p);
set<int> ys;
for (int k = 0; k < K; k++) {
int y = active[k] % p;
if (mark[y] != C) current_count[y] = 0, mark[y] = C;
if (++current_count[y] > K/3) ys.insert(y);
}
for (int y: ys) {
best[t].emplace_back(p, y, current_count[y]);
res[t] = max(res[t], current_count[y]);
}
C++;
};
int Z = max(3, min(30, 100/K));
for (int z = 0; z < Z; z++) {
int i = gen() % K, j = gen() % K;
while (j == i) j = gen() % K;
for (int x = abs(active[j]-active[i]); x > 1; x /= pf[x]) {
if (pf[x] != pf[x/pf[x]]) consider(pf[x]);
}
}
}
auto make_unique = [&](auto &v) {
sort(begin(v), end(v));
auto it = unique(begin(v), end(v));
v.erase(it, end(v));
};
for (int t = q-1; t >= 0; t--) {
make_unique(best[t]);
for (auto tpl: best[t]) res[t] = max(res[t], get<2>(tpl));
int x = xs[t];
bool insert = where[x] < 0;
update(x, true);
if (t > 0) {
for (auto [p, y, c]: best[t]) {
c += (insert ? 1 : -1) * (x%p == y);
if (c > K/3) best[t-1].emplace_back(p, y, c);
}
}
}
auto T2 = clock();
cerr << "Pass 1: " << double(T2-T1) / CLOCKS_PER_SEC << '\n';
for (int t = 0; t < q; t++) {
int x = xs[t];
bool insert = where[x] < 0;
update(x, true);
if (t > 0) {
for (auto [p, y, c]: best[t-1]) {
c += (insert ? 1 : -1) * (x%p == y);
if (c > K/3) best[t].emplace_back(p, y, c);
}
}
make_unique(best[t]);
for (auto tpl: best[t]) res[t] = max(res[t], get<2>(tpl));
}
auto T3 = clock();
cerr << "Pass 2: " << double(T3-T2) / CLOCKS_PER_SEC << '\n';
for (int k: res) cout << k << '\n';
}
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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | #include <bits/stdc++.h> using namespace std; const int N = 1e7 + 10; int pf[N]; vector<int> primes; int K = 0; int active[N]; int where[N]; const int M = 200; int mod_count[M][2000]; int mem[25'000'000]; int *count_of_count[M]; int highest_count[M]; int C = 1; int mark[N]; int current_count[N]; mt19937_64 gen(987); int main() { cin.tie(0)->sync_with_stdio(0); auto T0 = clock(); memset(where, -1, sizeof where); iota(pf, pf+N, 0); for (int i = 2; i < N; i++) if (pf[i] == i) { primes.push_back(i); if (i > N/i + 1) continue; for (int j = i*i; j < N; j += i) pf[j] = min(pf[j], i); } primes.resize(M); auto T1 = clock(); cerr << fixed << setprecision(6); cerr << "Sieve: " << double(T1-T0) / CLOCKS_PER_SEC << '\n'; count_of_count[0] = mem; for (int i = 0; i+1 < M; i++) { int p = primes[i]; count_of_count[i+1] = count_of_count[i] + 1 + (N+p-1) / p; } int n, q; cin >> n >> q; vector<int> xs(q); for (int &x: xs) cin >> x; auto update = [&](int x, bool skip_M = false) -> int { bool insert = where[x] < 0; if (insert) { active[K] = x; where[x] = K++; } else { int i = where[x]; int y = active[--K]; active[i] = y; where[y] = i; where[x] = -1; } if (skip_M) return 0; int res = 0; for (int i = 0; i < M; i++) { int p = primes[i], r = x % p; int &c = mod_count[i][r]; assert(c >= 0); count_of_count[i][c]--; c += insert ? 1 : -1; count_of_count[i][c]++; if (insert) { if (highest_count[i] == c-1) highest_count[i]++; } else { if (count_of_count[i][highest_count[i]] == 0) highest_count[i]--; } res = max(res, highest_count[i]); } assert(res >= K/2); return res; }; vector<vector<tuple<int,int,int>>> best(q); vector<int> res(q); for (int t = 0; t < q; t++) { res[t] = update(xs[t]); if (K <= 1) continue; set<int> done; auto consider = [&](int p) { if (p <= primes[M-1]) return; if (res[t] >= (n+p-1)/p) return; if (done.count(p)) return; done.insert(p); set<int> ys; for (int k = 0; k < K; k++) { int y = active[k] % p; if (mark[y] != C) current_count[y] = 0, mark[y] = C; if (++current_count[y] > K/3) ys.insert(y); } for (int y: ys) { best[t].emplace_back(p, y, current_count[y]); res[t] = max(res[t], current_count[y]); } C++; }; int Z = max(3, min(30, 100/K)); for (int z = 0; z < Z; z++) { int i = gen() % K, j = gen() % K; while (j == i) j = gen() % K; for (int x = abs(active[j]-active[i]); x > 1; x /= pf[x]) { if (pf[x] != pf[x/pf[x]]) consider(pf[x]); } } } auto make_unique = [&](auto &v) { sort(begin(v), end(v)); auto it = unique(begin(v), end(v)); v.erase(it, end(v)); }; for (int t = q-1; t >= 0; t--) { make_unique(best[t]); for (auto tpl: best[t]) res[t] = max(res[t], get<2>(tpl)); int x = xs[t]; bool insert = where[x] < 0; update(x, true); if (t > 0) { for (auto [p, y, c]: best[t]) { c += (insert ? 1 : -1) * (x%p == y); if (c > K/3) best[t-1].emplace_back(p, y, c); } } } auto T2 = clock(); cerr << "Pass 1: " << double(T2-T1) / CLOCKS_PER_SEC << '\n'; for (int t = 0; t < q; t++) { int x = xs[t]; bool insert = where[x] < 0; update(x, true); if (t > 0) { for (auto [p, y, c]: best[t-1]) { c += (insert ? 1 : -1) * (x%p == y); if (c > K/3) best[t].emplace_back(p, y, c); } } make_unique(best[t]); for (auto tpl: best[t]) res[t] = max(res[t], get<2>(tpl)); } auto T3 = clock(); cerr << "Pass 2: " << double(T3-T2) / CLOCKS_PER_SEC << '\n'; for (int k: res) cout << k << '\n'; } |
English