#include <bits/stdc++.h>
#include <unistd.h>
using namespace std;
constexpr int N = 10'000'007;
int d[N];
int sz=0;
int tab[N];
int miej[N];
static const int BUFSIZE = 1 << 16;
char buf[BUFSIZE];
int idx = 0, siz = 0;
inline char getChar() {
if (idx >= siz) {
siz = read(0, buf, BUFSIZE);
idx = 0;
if (siz == 0) return EOF;
}
return buf[idx++];
}
inline int readInt() {
int x = 0;
char c = getChar();
while (c!=EOF&&(c < '0' || c > '9')) c = getChar();
if (c==EOF)return -1;
while (c >= '0' && c <= '9') {
x = x * 10 + (c - '0');
c = getChar();
}
return x;
}
static const int OUT_BUF = 1 << 16;
char outbuf[OUT_BUF];
int out_idx = 0;
inline void flush() {
write(1, outbuf, out_idx);
out_idx = 0;
}
inline void putChar(char c) {
if (out_idx >= OUT_BUF) flush();
outbuf[out_idx++] = c;
}
inline void writeInt(int x) {
if (x == 0) {
putChar('0');
putChar('\n');
return;
}
char s[12];
int n = 0;
while(x > 0) {
s[n++] = char('0' + x % 10);
x /= 10;
}
while(n--)putChar(s[n]);
putChar('\n');
}
signed main()
{
cin.tie(0);
ios_base::sync_with_stdio(0);
int n=readInt(),q=readInt();
int in[q];
for(int i=0;i<q;++i)in[i]=readInt();
int primes[665'000];
int psz=0;
int sieve[n+1];
fill(sieve+2,sieve+n+1,1);
sieve[0]=sieve[1]=0;
for(int i=2;i<n+1;i++){
if(!sieve[i])continue;
for(int j=i+i;j<n+1;j+=i)sieve[j]=0;
}
for(int i=0;i<n+1;i++){
if(sieve[i]){
primes[psz]=i;
++psz;
}
}
for(int _=0;_<q;++_){
int x = in[_];
if(miej[x]){
miej[d[sz]]=miej[x];
swap(d[sz],d[miej[x]]);
--sz;
miej[x]=0;
}else{
miej[x]=++sz;
d[sz]=x;
}
if(n==1&&sz==1){
writeInt(1);
continue;
}
int res=0;
if(!sz){
writeInt(0);
continue;
}
for(int p=0;p<psz;++p){
if(primes[p]>((n*2)+sz-1)/sz)break;
int check=0;
for(int kam=1;kam<=sz;kam++){
check=max(check,++tab[d[kam]%primes[p]]);
}
for(int kam=1;kam<=sz;kam++){
tab[d[kam]%primes[p]]=0;
}
res=max(res,check);
}
writeInt(res);
}
flush();
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 | #include <bits/stdc++.h> #include <unistd.h> using namespace std; constexpr int N = 10'000'007; int d[N]; int sz=0; int tab[N]; int miej[N]; static const int BUFSIZE = 1 << 16; char buf[BUFSIZE]; int idx = 0, siz = 0; inline char getChar() { if (idx >= siz) { siz = read(0, buf, BUFSIZE); idx = 0; if (siz == 0) return EOF; } return buf[idx++]; } inline int readInt() { int x = 0; char c = getChar(); while (c!=EOF&&(c < '0' || c > '9')) c = getChar(); if (c==EOF)return -1; while (c >= '0' && c <= '9') { x = x * 10 + (c - '0'); c = getChar(); } return x; } static const int OUT_BUF = 1 << 16; char outbuf[OUT_BUF]; int out_idx = 0; inline void flush() { write(1, outbuf, out_idx); out_idx = 0; } inline void putChar(char c) { if (out_idx >= OUT_BUF) flush(); outbuf[out_idx++] = c; } inline void writeInt(int x) { if (x == 0) { putChar('0'); putChar('\n'); return; } char s[12]; int n = 0; while(x > 0) { s[n++] = char('0' + x % 10); x /= 10; } while(n--)putChar(s[n]); putChar('\n'); } signed main() { cin.tie(0); ios_base::sync_with_stdio(0); int n=readInt(),q=readInt(); int in[q]; for(int i=0;i<q;++i)in[i]=readInt(); int primes[665'000]; int psz=0; int sieve[n+1]; fill(sieve+2,sieve+n+1,1); sieve[0]=sieve[1]=0; for(int i=2;i<n+1;i++){ if(!sieve[i])continue; for(int j=i+i;j<n+1;j+=i)sieve[j]=0; } for(int i=0;i<n+1;i++){ if(sieve[i]){ primes[psz]=i; ++psz; } } for(int _=0;_<q;++_){ int x = in[_]; if(miej[x]){ miej[d[sz]]=miej[x]; swap(d[sz],d[miej[x]]); --sz; miej[x]=0; }else{ miej[x]=++sz; d[sz]=x; } if(n==1&&sz==1){ writeInt(1); continue; } int res=0; if(!sz){ writeInt(0); continue; } for(int p=0;p<psz;++p){ if(primes[p]>((n*2)+sz-1)/sz)break; int check=0; for(int kam=1;kam<=sz;kam++){ check=max(check,++tab[d[kam]%primes[p]]); } for(int kam=1;kam<=sz;kam++){ tab[d[kam]%primes[p]]=0; } res=max(res,check); } writeInt(res); } flush(); return 0; } |
English