#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
bool sito[5000010];
unordered_map<long long,bool>m;
vector<long long>v;
vector<pair<long long,long long>>w;
struct hashpair{
template <class T1,class T2>
int operator() (const pair<T1,T2>&p)const
{
return (hash<int>()(p.first)^hash<int>()(p.second))<<1;
}
};
unordered_map<pair<long long,long long>,int,hashpair>k;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long n;
cin>>n;
int q;
cin>>q;
int d=0;
for(int i=2;i*i<=n;i++){
if(sito[i]==0){
v.push_back(i);
for(int j=i*i;j<=n;j+=i){
sito[j]=1;
}
}
}
if(n==3){
v.push_back(2);
v.push_back(3);
}
int wynik=0;
for(int z=0;z<q;z++){
long long a;
cin>>a;
for(int i=0;i<w.size();i++){
if(a%w[i].first!=w[i].second){
break;
}
if(i+1==w.size()){
wynik=0;
}
}
if(m[a]==1){
m[a]=0;
d--;
for(int i=0;i<v.size();i++){
int temp=wynik;
int reszta=--k[{v[i],a%v[i]}];
wynik=max(wynik,reszta);
if(temp<wynik){
w.clear();
}
if(reszta==wynik){
w.push_back({v[i],a%v[i]});
}
}
}else{
m[a]=1;
d++;
for(int i=0;i<v.size();i++){
int temp=wynik;
int reszta=++k[{v[i],a%v[i]}];
wynik=max(wynik,reszta);
if(temp<wynik){
w.clear();
}
if(wynik==reszta){
w.push_back({v[i],a%v[i]});
}
}
}
if(d>0&&wynik==0){
wynik=1;
w.push_back({1,0});
}
cout<<wynik<<"\n";
}
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 | #include <iostream> #include <unordered_map> #include <vector> using namespace std; bool sito[5000010]; unordered_map<long long,bool>m; vector<long long>v; vector<pair<long long,long long>>w; struct hashpair{ template <class T1,class T2> int operator() (const pair<T1,T2>&p)const { return (hash<int>()(p.first)^hash<int>()(p.second))<<1; } }; unordered_map<pair<long long,long long>,int,hashpair>k; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n; cin>>n; int q; cin>>q; int d=0; for(int i=2;i*i<=n;i++){ if(sito[i]==0){ v.push_back(i); for(int j=i*i;j<=n;j+=i){ sito[j]=1; } } } if(n==3){ v.push_back(2); v.push_back(3); } int wynik=0; for(int z=0;z<q;z++){ long long a; cin>>a; for(int i=0;i<w.size();i++){ if(a%w[i].first!=w[i].second){ break; } if(i+1==w.size()){ wynik=0; } } if(m[a]==1){ m[a]=0; d--; for(int i=0;i<v.size();i++){ int temp=wynik; int reszta=--k[{v[i],a%v[i]}]; wynik=max(wynik,reszta); if(temp<wynik){ w.clear(); } if(reszta==wynik){ w.push_back({v[i],a%v[i]}); } } }else{ m[a]=1; d++; for(int i=0;i<v.size();i++){ int temp=wynik; int reszta=++k[{v[i],a%v[i]}]; wynik=max(wynik,reszta); if(temp<wynik){ w.clear(); } if(wynik==reszta){ w.push_back({v[i],a%v[i]}); } } } if(d>0&&wynik==0){ wynik=1; w.push_back({1,0}); } cout<<wynik<<"\n"; } return 0; } |
English