#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#include <unordered_set>
using namespace std;
#define JOIN_(X, Y) X##Y
#define JOIN(X, Y) JOIN_(X, Y)
#define TMP JOIN(tmp, __LINE__)
#define PB push_back
#define SZ(x) int((x).size())
#define REP(i, n) for (int i = 0, TMP = (n); i < TMP; ++i)
#define FOR(i, a, b) for (int i = (a), TMP = (b); i <= TMP; ++i)
#define FORD(i, a, b) for (int i = (a), TMP = (b); i >= TMP; --i)
#ifdef DEBUG
#define DEB(x) (cerr << x)
#else
#define DEB(x)
#endif
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
os << "vector[";
for (const auto &e : v) {
os << e << ", ";
}
os << "]";
return os;
}
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef unsigned int uint;
const int INF = 1e9 + 9;
struct Sieve {
int n;
vi primes;
vector<bool> is_prime;
void fill(int _n) {
n = _n;
primes.clear();
is_prime.clear();
is_prime.resize(n + 1, true);
FOR(i, 2, sqrt(n) + 1) {
if (is_prime[i]) {
for (int j = i * 2; j <= n; j += i) {
is_prime[j] = false;
}
}
}
FOR(i, 2, n) {
if (is_prime[i]) {
primes.PB(i);
}
}
}
};
struct Case {
int n, q;
vi cnt_mod2;
vector<bool> bits;
Sieve sieve;
unordered_set<int> current;
vi cnt;
void solve() {
cin >> n >> q;
sieve.fill(n);
#ifdef DEBUG
DEB("primes:\n");
for (int prime : sieve.primes) {
DEB(prime << " ");
}
DEB("\n");
#endif
cnt_mod2.resize(2);
bits.resize(n + 1);
REP(_, q) {
int pos;
cin >> pos;
int prev_bit = int(bits[pos]);
bits[pos].flip();
if (bits[pos]) {
current.emplace(pos);
} else {
current.erase(pos);
}
int next_bit = int(bits[pos]);
int diff = next_bit - prev_bit;
cnt_mod2[pos % 2] += diff;
DEB("cnt_mod_2 = " << cnt_mod2 << "\n");
cnt.resize(n + 1);
int result = max(cnt_mod2[0], cnt_mod2[1]);
result = max(result, get_result());
cout << result << "\n";
}
}
int get_result() {
DEB("SZ(current) = " << SZ(current) << "\n");
int min_result = SZ(current) / 2;
DEB("min_result = " << min_result << "\n");
if (SZ(current) <= 2) {
return SZ(current);
}
int result = 0;
for (int k : sieve.primes) {
DEB(" k=" << k << " n / k = " << n / k
<< " < min_result = " << min_result << "\n");
if (n / k <= min_result) {
break;
}
for (int x : current) {
int cand = ++cnt[x % k];
result = max(result, cand);
min_result = max(min_result, result + 1);
DEB(" cand = " << cand << " result = " << result << "\n");
}
for (int x : current) {
cnt[x % k] = 0;
}
}
return result;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int z = 1;
// cin >> z;
while (z--) {
Case().solve();
}
}
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 | #include <algorithm> #include <bitset> #include <cmath> #include <cstdlib> #include <ctime> #include <deque> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #include <unordered_set> using namespace std; #define JOIN_(X, Y) X##Y #define JOIN(X, Y) JOIN_(X, Y) #define TMP JOIN(tmp, __LINE__) #define PB push_back #define SZ(x) int((x).size()) #define REP(i, n) for (int i = 0, TMP = (n); i < TMP; ++i) #define FOR(i, a, b) for (int i = (a), TMP = (b); i <= TMP; ++i) #define FORD(i, a, b) for (int i = (a), TMP = (b); i >= TMP; --i) #ifdef DEBUG #define DEB(x) (cerr << x) #else #define DEB(x) #endif template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { os << "vector["; for (const auto &e : v) { os << e << ", "; } os << "]"; return os; } typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; typedef unsigned int uint; const int INF = 1e9 + 9; struct Sieve { int n; vi primes; vector<bool> is_prime; void fill(int _n) { n = _n; primes.clear(); is_prime.clear(); is_prime.resize(n + 1, true); FOR(i, 2, sqrt(n) + 1) { if (is_prime[i]) { for (int j = i * 2; j <= n; j += i) { is_prime[j] = false; } } } FOR(i, 2, n) { if (is_prime[i]) { primes.PB(i); } } } }; struct Case { int n, q; vi cnt_mod2; vector<bool> bits; Sieve sieve; unordered_set<int> current; vi cnt; void solve() { cin >> n >> q; sieve.fill(n); #ifdef DEBUG DEB("primes:\n"); for (int prime : sieve.primes) { DEB(prime << " "); } DEB("\n"); #endif cnt_mod2.resize(2); bits.resize(n + 1); REP(_, q) { int pos; cin >> pos; int prev_bit = int(bits[pos]); bits[pos].flip(); if (bits[pos]) { current.emplace(pos); } else { current.erase(pos); } int next_bit = int(bits[pos]); int diff = next_bit - prev_bit; cnt_mod2[pos % 2] += diff; DEB("cnt_mod_2 = " << cnt_mod2 << "\n"); cnt.resize(n + 1); int result = max(cnt_mod2[0], cnt_mod2[1]); result = max(result, get_result()); cout << result << "\n"; } } int get_result() { DEB("SZ(current) = " << SZ(current) << "\n"); int min_result = SZ(current) / 2; DEB("min_result = " << min_result << "\n"); if (SZ(current) <= 2) { return SZ(current); } int result = 0; for (int k : sieve.primes) { DEB(" k=" << k << " n / k = " << n / k << " < min_result = " << min_result << "\n"); if (n / k <= min_result) { break; } for (int x : current) { int cand = ++cnt[x % k]; result = max(result, cand); min_result = max(min_result, result + 1); DEB(" cand = " << cand << " result = " << result << "\n"); } for (int x : current) { cnt[x % k] = 0; } } return result; } }; int main() { ios::sync_with_stdio(false); cin.tie(0); int z = 1; // cin >> z; while (z--) { Case().solve(); } } |
English