#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
//mt19937 mrand(random_device{}());
const ll mod=1000000007;
//int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
int gcd(int a,int b) { return b?gcd(b,a%b):a;}
// head
const int N=1e7+5;
#define INF 0x3f3f3f3f
template<class T> inline void read(T &x) {
x=0; int c=getchar(),f=1;
for (;!isdigit(c);c=getchar()) if (c==45) f=-1;
for (;isdigit(c);c=getchar()) (x*=10)+=f*(c-'0');
}
int n,q,spf[N],is_active[N],n_sqrt,cnt[N],offset[3500],freq_small[N],freq[N],divs_buff[3000],small_max,large_max,active_list[N],active_idx[N],active_size;
void sieve(int n) {
rep(i,2,n+1) if (spf[i]==0) for (int j=i;j<=n;j+=i) if (spf[j]==0) spf[j]=i;
}
struct factor { int p,e;}factors[30];
int n_factors;
inline void factorize(int d) {
n_factors=0;
while (d>1) {
int p=spf[d];
int e=0;
while (d%p==0) {
d/=p;
e++;
}
factors[n_factors++]={p,e};
}
}
void active_add(int a) {
active_list[active_size]=a;
active_idx[a]=active_size;
active_size++;
}
void active_remove(int a) {
int i=active_idx[a],last=active_list[active_size-1];
active_list[i]=last;
active_idx[last]=i;
active_size--;
}
int compute_max_through(int a) {
int res=0;
VI touched;
rep(i,0,active_size) {
int b=active_list[i];
if (b==a) continue;
int diff=abs(b-a);
factorize(diff);
int n_divs=0;
divs_buff[n_divs++]=1;
rep(j,0,n_factors) {
int pw=1,prev_cnt=n_divs;
rep(k,0,factors[j].e) {
pw*=factors[j].p;
rep(l,0,prev_cnt) divs_buff[n_divs++]=pw*divs_buff[l];
}
}
rep(j,0,n_divs) {
int d=divs_buff[j];
if (d>n_sqrt) {
if (freq[d]==0) touched.pb(d);
freq[d]++;
res=max(res,freq[d]);
}
}
}
for (const auto &d:touched) freq[d]=0;
return res;
}
int calc_large_max() {
if (active_size<=1) return active_size;
int res=1;
rep(i,0,active_size) res=max(res,compute_max_through(active_list[i])+1);
return res;
}
int main() {
scanf("%d%d",&n,&q);
sieve(n);
n_sqrt=max(2,(int)sqrt((double)n));
while ((n_sqrt+1)*(n_sqrt+1)<=n) n_sqrt++;
int total=0;
rep(d,2,n_sqrt+1) {
offset[d]=total;
total+=d;
}
int max_chain_large=(n-1)/(n_sqrt+1)+1;
freq_small[0]=total;
rep(i,0,q) {
int a;
scanf("%d",&a);
if (is_active[a]) {
int max_through_large=1;
bool need_large=(large_max>1)&&(small_max<large_max||small_max<=max_chain_large);
if (need_large) max_through_large=compute_max_through(a)+1;
is_active[a]=false;
active_remove(a);
rep(d,2,n_sqrt+1) {
int r=a%d;
int idx=offset[d]+r;
int curr_cnt=cnt[idx];
cnt[idx]=curr_cnt-1;
freq_small[curr_cnt]--;
freq_small[curr_cnt-1]++;
}
while (small_max>0&&freq_small[small_max]==0) small_max--;
if (large_max>active_size) large_max=active_size;
if (need_large&&max_through_large>=large_max) {
if (active_size<=1) large_max=active_size;
else large_max=calc_large_max();
}
} else {
int new_large_candidate=1;
bool need_large=(small_max<max_chain_large);
if (need_large) new_large_candidate=compute_max_through(a)+1;
is_active[a]=true;
active_add(a);
rep(d,2,n_sqrt+1) {
int r=a%d;
int idx=offset[d]+r;
int curr_cnt=cnt[idx];
cnt[idx]=curr_cnt+1;
freq_small[curr_cnt]--;
freq_small[curr_cnt+1]++;
}
while (freq_small[small_max+1]>0) small_max++;
if (need_large) large_max=max(large_max,new_large_candidate);
}
printf("%d\n",max(small_max,large_max));
}
}
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 | #include <bits/stdc++.h> using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #define per(i,a,n) for (int i=n-1;i>=a;i--) #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) typedef vector<int> VI; typedef long long ll; typedef pair<int,int> PII; typedef double db; //mt19937 mrand(random_device{}()); const ll mod=1000000007; //int rnd(int x) { return mrand() % x;} ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} int gcd(int a,int b) { return b?gcd(b,a%b):a;} // head const int N=1e7+5; #define INF 0x3f3f3f3f template<class T> inline void read(T &x) { x=0; int c=getchar(),f=1; for (;!isdigit(c);c=getchar()) if (c==45) f=-1; for (;isdigit(c);c=getchar()) (x*=10)+=f*(c-'0'); } int n,q,spf[N],is_active[N],n_sqrt,cnt[N],offset[3500],freq_small[N],freq[N],divs_buff[3000],small_max,large_max,active_list[N],active_idx[N],active_size; void sieve(int n) { rep(i,2,n+1) if (spf[i]==0) for (int j=i;j<=n;j+=i) if (spf[j]==0) spf[j]=i; } struct factor { int p,e;}factors[30]; int n_factors; inline void factorize(int d) { n_factors=0; while (d>1) { int p=spf[d]; int e=0; while (d%p==0) { d/=p; e++; } factors[n_factors++]={p,e}; } } void active_add(int a) { active_list[active_size]=a; active_idx[a]=active_size; active_size++; } void active_remove(int a) { int i=active_idx[a],last=active_list[active_size-1]; active_list[i]=last; active_idx[last]=i; active_size--; } int compute_max_through(int a) { int res=0; VI touched; rep(i,0,active_size) { int b=active_list[i]; if (b==a) continue; int diff=abs(b-a); factorize(diff); int n_divs=0; divs_buff[n_divs++]=1; rep(j,0,n_factors) { int pw=1,prev_cnt=n_divs; rep(k,0,factors[j].e) { pw*=factors[j].p; rep(l,0,prev_cnt) divs_buff[n_divs++]=pw*divs_buff[l]; } } rep(j,0,n_divs) { int d=divs_buff[j]; if (d>n_sqrt) { if (freq[d]==0) touched.pb(d); freq[d]++; res=max(res,freq[d]); } } } for (const auto &d:touched) freq[d]=0; return res; } int calc_large_max() { if (active_size<=1) return active_size; int res=1; rep(i,0,active_size) res=max(res,compute_max_through(active_list[i])+1); return res; } int main() { scanf("%d%d",&n,&q); sieve(n); n_sqrt=max(2,(int)sqrt((double)n)); while ((n_sqrt+1)*(n_sqrt+1)<=n) n_sqrt++; int total=0; rep(d,2,n_sqrt+1) { offset[d]=total; total+=d; } int max_chain_large=(n-1)/(n_sqrt+1)+1; freq_small[0]=total; rep(i,0,q) { int a; scanf("%d",&a); if (is_active[a]) { int max_through_large=1; bool need_large=(large_max>1)&&(small_max<large_max||small_max<=max_chain_large); if (need_large) max_through_large=compute_max_through(a)+1; is_active[a]=false; active_remove(a); rep(d,2,n_sqrt+1) { int r=a%d; int idx=offset[d]+r; int curr_cnt=cnt[idx]; cnt[idx]=curr_cnt-1; freq_small[curr_cnt]--; freq_small[curr_cnt-1]++; } while (small_max>0&&freq_small[small_max]==0) small_max--; if (large_max>active_size) large_max=active_size; if (need_large&&max_through_large>=large_max) { if (active_size<=1) large_max=active_size; else large_max=calc_large_max(); } } else { int new_large_candidate=1; bool need_large=(small_max<max_chain_large); if (need_large) new_large_candidate=compute_max_through(a)+1; is_active[a]=true; active_add(a); rep(d,2,n_sqrt+1) { int r=a%d; int idx=offset[d]+r; int curr_cnt=cnt[idx]; cnt[idx]=curr_cnt+1; freq_small[curr_cnt]--; freq_small[curr_cnt+1]++; } while (freq_small[small_max+1]>0) small_max++; if (need_large) large_max=max(large_max,new_large_candidate); } printf("%d\n",max(small_max,large_max)); } } |
English