#include<bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define ST first
#define ND second
#define PB push_back
using ll = long long;
using ld = long double;
using pi = pair<int,int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
#ifdef LOCAL
#define DTP(x, y) \
auto operator<<(auto &o, auto a)->decltype(y, o) \
{ \
o << "("; \
x; \
return o << ")"; \
}
DTP(o << a.first << ", " << a.second, a.second);
DTP(for (auto i : a) o << i << ", ", all(a));
void dump(auto... x) { ((cerr << x << ", "), ...) << '\n'; }
#define debug(x...) cerr << setw(4) << __LINE__ << ":[" #x "]: ", dump(x)
#else
#define debug(...) 0
#endif
const int PConst=5000;
int n, q;
vi Solve_small(vector<pi> &I){
vi Seave(PConst+1, 1);
vi Primes;
rep(i, 2, PConst+1)
if(Seave[i]){
Primes.PB(i);
for(int j=i*i; j<=PConst; j+=i)
Seave[j]=0;
}
vvi L(PConst, vi(PConst));
vi G(q+1);
int res=0;
int cnt=0;
vi Ans(q);
for(auto p: Primes)
G[0]+=p;
for(auto &[x,t]: I){
for(auto p: Primes){
if(t){
if(L[p][x%p]==res)
++res;
--G[L[p][x%p]];
++L[p][x%p];
++G[L[p][x%p]];
}else{
if(L[p][x%p]==res && G[res]==1)
--res;
--G[L[p][x%p]];
--L[p][x%p];
++G[L[p][x%p]];
}
}
Ans[cnt++]=res;
}
return Ans;
}
// const int Leng=400;
// vo Solve_Big(vector<pi> &I){
// vi B(1e7+1);
// vi Primes={2};
// B[2]=2;
// for(ll i=2; i<=1e7; ++i){
// if(B[i]==0)
// B[i]=i;
// for(ll p: Primes){
// if(p*i>1e7 || p>B[i])
// break;
// B[p*i]=p;
// }
// }
// B[0]=0;
// B[1]=1;
// for(int 1e7-1; i>=2; --i){
// int x=i;
// while(B[x]!=x)
// x=x/B[x];
// B[i]=x;
// }
// int x=0;
// int curr=0;
// vi A
// while(x<)
// }
void Solve()
{
cin >> n >> q;
vector<pi> I(q);
vi T(1e7+1);
rep(i, 0, q){
cin >> I[i].ST;
T[I[i].ST]=1-T[I[i].ST];
I[i].ND=T[I[i].ST];
}
vi Ans1=Solve_small(I);
// vi Ans2=Solve_Big(i);
rep(i, 0, q)
cout << Ans1[i] << "\n";
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int t=1;
// cin >> t;
while(t--)
{
Solve();
}
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 | #include<bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i = (a); i < (b); i++) #define all(x) begin(x), end(x) #define sz(x) (int)(x).size() #define ST first #define ND second #define PB push_back using ll = long long; using ld = long double; using pi = pair<int,int>; using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; #ifdef LOCAL #define DTP(x, y) \ auto operator<<(auto &o, auto a)->decltype(y, o) \ { \ o << "("; \ x; \ return o << ")"; \ } DTP(o << a.first << ", " << a.second, a.second); DTP(for (auto i : a) o << i << ", ", all(a)); void dump(auto... x) { ((cerr << x << ", "), ...) << '\n'; } #define debug(x...) cerr << setw(4) << __LINE__ << ":[" #x "]: ", dump(x) #else #define debug(...) 0 #endif const int PConst=5000; int n, q; vi Solve_small(vector<pi> &I){ vi Seave(PConst+1, 1); vi Primes; rep(i, 2, PConst+1) if(Seave[i]){ Primes.PB(i); for(int j=i*i; j<=PConst; j+=i) Seave[j]=0; } vvi L(PConst, vi(PConst)); vi G(q+1); int res=0; int cnt=0; vi Ans(q); for(auto p: Primes) G[0]+=p; for(auto &[x,t]: I){ for(auto p: Primes){ if(t){ if(L[p][x%p]==res) ++res; --G[L[p][x%p]]; ++L[p][x%p]; ++G[L[p][x%p]]; }else{ if(L[p][x%p]==res && G[res]==1) --res; --G[L[p][x%p]]; --L[p][x%p]; ++G[L[p][x%p]]; } } Ans[cnt++]=res; } return Ans; } // const int Leng=400; // vo Solve_Big(vector<pi> &I){ // vi B(1e7+1); // vi Primes={2}; // B[2]=2; // for(ll i=2; i<=1e7; ++i){ // if(B[i]==0) // B[i]=i; // for(ll p: Primes){ // if(p*i>1e7 || p>B[i]) // break; // B[p*i]=p; // } // } // B[0]=0; // B[1]=1; // for(int 1e7-1; i>=2; --i){ // int x=i; // while(B[x]!=x) // x=x/B[x]; // B[i]=x; // } // int x=0; // int curr=0; // vi A // while(x<) // } void Solve() { cin >> n >> q; vector<pi> I(q); vi T(1e7+1); rep(i, 0, q){ cin >> I[i].ST; T[I[i].ST]=1-T[I[i].ST]; I[i].ND=T[I[i].ST]; } vi Ans1=Solve_small(I); // vi Ans2=Solve_Big(i); rep(i, 0, q) cout << Ans1[i] << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int t=1; // cin >> t; while(t--) { Solve(); } return 0; } |
English