P15992 [PA 2026] 原题加强 2 / Wersja dla profesjonalistów 2
EuphoricStar · · 题解
设
考虑如何确定性解决单次询问:
给定
a_0, a_1, \ldots, a_{m - 1} ,选择一对整数(A, B) (A \ge 2, 0 \le B < A ),最大化\sum\limits_{i = 0}^{m - 1} [a_i \bmod A = B] 。
首先若选若干个数使得它们模
注意到可以选择
对于一个
证明:设
a 中有k \ge \frac{m}{2} 个数模p 同余。设其中x 个相邻同余点对的距离\le 2 ,k - x 个相邻同余点对的距离\ge 3 ,有不等式:m \ge x + 3(k - x) 移项,得
x \ge \frac{3k - m}{2} \ge \frac{m}{4} 。
而这些数至多有
考虑如何处理动态询问。一个想法是设计均摊策略。设某次操作前
例如我们可以批量处理之后的
现在我们希望把所有满足「
类似上面单次询问的分析,我们设一个阈值
移项得:
也就是说,对于一个质数
而这样的
总时间复杂度
:::info[代码]
#include <bits/stdc++.h>
#define pb emplace_back
#define fst first
#define scd second
#define mkp make_pair
#define mems(a, x) memset((a), (x), sizeof(a))
using namespace std;
using ll = long long;
using ull = unsigned long long;
using db = double;
using ldb = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
namespace IO {
const int maxn = 1 << 20;
char ibuf[maxn], *iS, *iT, obuf[maxn], *oS = obuf;
inline char gc() {
return (iS == iT ? iT = (iS = ibuf) + fread(ibuf, 1, maxn, stdin), (iS == iT ? EOF : *iS++) : *iS++);
}
template<typename T = int>
inline T read() {
char c = gc();
T x = 0;
bool f = 0;
while (c < '0' || c > '9') {
f |= (c == '-');
c = gc();
}
while (c >= '0' && c <= '9') {
x = (x << 1) + (x << 3) + (c ^ 48);
c = gc();
}
return f ? ~(x - 1) : x;
}
inline int reads(char *s) {
char c = gc();
int len = 0;
while (isspace(c)) {
c = gc();
}
while (!isspace(c) && c != EOF) {
s[len++] = c;
c = gc();
}
s[len] = '\0';
return len;
}
inline string reads() {
char c = gc();
string s;
while (isspace(c)) {
c = gc();
}
while (!isspace(c) && c != EOF) {
s += c;
c = gc();
}
return s;
}
inline void flush() {
fwrite(obuf, 1, oS - obuf, stdout);
oS = obuf;
}
struct Flusher {
~Flusher() {
flush();
}
} AutoFlush;
inline void pc(char ch) {
if (oS == obuf + maxn) {
flush();
}
*oS++ = ch;
}
inline void write(char *s) {
for (int i = 0; s[i]; ++i) {
pc(s[i]);
}
}
inline void write(const char *s) {
for (int i = 0; s[i]; ++i) {
pc(s[i]);
}
}
template<typename T>
inline void write(T x) {
static char stk[64], *tp = stk;
if (x < 0) {
x = ~(x - 1);
pc('-');
}
do {
*tp++ = x % 10;
x /= 10;
} while (x);
while (tp != stk) {
pc((*--tp) | 48);
}
}
template<typename T>
inline void writesp(T x) {
write(x);
pc(' ');
}
template<typename T>
inline void writeln(T x) {
write(x);
pc('\n');
}
}
using IO::read;
using IO::reads;
using IO::write;
using IO::pc;
using IO::writesp;
using IO::writeln;
const int maxn = 10000100;
const int maxm = 1000100;
int n, m, mpr[maxn], pr[maxm], tot, a[maxm], b[maxm], c[maxn], stk[maxn];
int d[maxm], f[maxn], g[maxm], ans[maxm];
bool vis[maxn];
void solve() {
n = read();
m = read();
for (int i = 2; i <= n; ++i) {
if (!vis[i]) {
pr[++tot] = mpr[i] = i;
}
for (int j = 1; j <= tot && i * pr[j] <= n; ++j) {
vis[i * pr[j]] = 1;
mpr[i * pr[j]] = pr[j];
if (i % pr[j] == 0) {
break;
}
}
}
set<int> S;
for (int i = 1; i <= m; ++i) {
a[i] = read();
if (S.find(a[i]) != S.end()) {
S.erase(a[i]);
d[i] = -1;
} else {
S.insert(a[i]);
d[i] = 1;
}
}
S.clear();
for (int l = 1, r; l <= m; l = r + 1) {
int len = (int)S.size();
r = min(l + max(len / 4, 1) - 1, m);
if (len >= 2) {
int tt = 0;
for (int x : S) {
b[++tt] = x;
}
int top = 0;
for (int i = 1; i <= tt; ++i) {
for (int j = 1; j <= min(tt - 1, 4); ++j) {
int x = abs(b[i] - b[i + j > tt ? i + j - tt : i + j]);
while (x > 1) {
int y = mpr[x];
if (!c[y]) {
stk[++top] = y;
}
++c[y];
while (x % y == 0) {
x /= y;
}
}
}
}
for (int i = 1; i <= top; ++i) {
int p = stk[i];
if (c[p] >= len * 7 / 32) {
int mx = 0;
g[0] = 1e9;
auto upd = [&](int x, int y) -> void {
--g[f[x]];
++g[f[x] += y];
mx = max(mx, f[x]);
if (!g[mx]) {
--mx;
}
};
for (int x : S) {
upd(x % p, 1);
}
for (int j = l; j <= r; ++j) {
upd(a[j] % p, d[j]);
ans[j] = max(ans[j], mx);
}
for (int x : S) {
g[f[x % p]] = 0;
f[x % p] = 0;
}
for (int j = l; j <= r; ++j) {
int x = a[j];
g[f[x % p]] = 0;
f[x % p] = 0;
}
}
c[p] = 0;
}
}
for (int i = l; i <= r; ++i) {
if (d[i] == 1) {
S.insert(a[i]);
} else {
S.erase(a[i]);
}
int len = (int)S.size();
if (len <= 1) {
ans[i] = len;
} else if (len == 2) {
ans[i] = 1 + (*S.rbegin() - *S.begin() > 1);
} else if (len == 3) {
int x = *S.begin(), y = *next(S.begin()), z = *S.rbegin();
ans[i] = 2 + (__gcd(y - x, z - y) > 1);
}
}
}
for (int i = 1; i <= m; ++i) {
writeln(ans[i]);
}
}
int main() {
int T = 1;
// scanf("%d", &T);
while (T--) {
solve();
}
return 0;
}
:::