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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#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())
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;

#ifdef LOCAL
auto operator<<(auto& o, auto x) -> decltype(x.first, o);
auto operator<<(auto& o, auto x) -> decltype(x.end(), o) {
  o << "{";
  for (int i = 0; auto y : x) o << ", " + !i++ * 2 << y;
  return o << "}"; }
auto operator<<(auto& o, auto x) -> decltype(x.first, o) {
  return o << "(" << x.first << ", " << x.second << ")"; }
void __print(auto... x) { ((cerr << x << " "), ...) << endl; }
#define debug(x...) __print("[" #x "]:", x)
#else
#define debug(...) 2137
#endif

const int nax = 1e7 + 10;
const int nax_q = 1e6 + 10;
const int threshold = 1e4; // TODO, must be >= sqrt(n)
const int bnd = 2 * nax / threshold + 2;
const int bnd2 = 3 * nax / threshold + 2;
const int num_p_small = 2e3;
int pos[nax], freq[nax_q], freq_big[nax_q], mx_pr[nax], u_pr[nax];
int cnt[num_p_small][threshold];
unordered_map<ll, int> cnt_big;

const int LIM = nax;
bitset<LIM> isPrime;
vi eratosthenes() {
  const int S = (int)round(sqrt(LIM)), R = LIM / 2;
  vi pr = {2}, sieve(S+1); pr.reserve(int(LIM/log(LIM)*1.1));
  vector<pii> cp;
  for (int i = 3; i <= S; i += 2) if (!sieve[i]) {
    cp.push_back({i, i * i / 2});
    for (int j = i * i; j <= S; j += 2 * i) sieve[j] = 1;
  }
  for (int L = 1; L <= R; L += S) {
    array<bool, S> block{};
    for (auto &[p, idx] : cp)
      for (int i=idx; i < S+L; idx = (i+=p)) block[i-L] = 1;
    rep(i,0,min(S, R - L))
      if (!block[i]) pr.push_back((L + i) * 2 + 1);
  }
  for(int i = 0; i < pr.size(); i++) {
    isPrime[pr[i]] = 1;
  }
  return pr;
}

int main() {
  cin.tie(0)->sync_with_stdio(0);
  
  eratosthenes();
  vector<int> p;
  for(int i = 2; i <= threshold; i++) {
    if(isPrime[i]) {
      p.push_back(i);
    }
  }
  for(int i = threshold + 1; i < nax; i++) {
    if(isPrime[i]) {
      for(int j = i; j < nax; j += i) {
        mx_pr[j] = i;
      }
    }
  }
  u_pr[0] = 1;
  
  int n, q;
  cin >> n >> q;
  
  bool take_big = 1;
  
  int tot_big = 0, tot_rebuild = 0;
  
  int ans_small = 0, ans_big = 0;
  int c = 0;
  vector<int> u;
  u.reserve(q);
  for(int rep = 0; rep < q; rep++) {
    int id;
    cin >> id;
    
    if(pos[id]) {
      swap(u[pos[id] - 1], u[sz(u) - 1]);
      u.pop_back();
      pos[id] = 0;
      c--;
      
      for(int i = 0; i < p.size(); i++) {
        int r = id % p[i];
        freq[cnt[i][r]]--;
        if(cnt[i][r] == ans_small && !freq[cnt[i][r]]) ans_small--;
        cnt[i][r]--;
      }
      
      if(c > bnd2) take_big = 0;
      if(!take_big && c <= bnd) {
        take_big = 1;
        
        ans_big = 0;
        cnt_big.clear();
        for(int i = 0; i < nax_q; i++) freq_big[i] = 0;
        for(int i = 0; i < bnd; i++) {
          vector<int> to_del;
          to_del.reserve(bnd);
          for(int j = 0; j < i; j++) {
            ll pr = mx_pr[abs(u[j] - u[i])];
            if(!u_pr[pr]) {
              to_del.push_back(pr);
              u_pr[pr] = 1;
              int r = u[i] % pr;
              ll x = (pr << 24) + r;
              cnt_big[x]++;
              freq_big[cnt_big[x]]++;
              ans_big = max(ans_big, cnt_big[x]);
            }
          }
          for(int pr : to_del) {
            u_pr[pr] = 0;
          }
        }
        tot_rebuild++;
      }
      
      if(take_big) {
        vector<int> to_del;
        to_del.reserve(sz(u));
        for(int x : u) {
          ll pr = mx_pr[abs(x - id)];
          if(!u_pr[pr]) {
            to_del.push_back(pr);
            u_pr[pr] = 1;
            int r = id % pr;
            ll x = (pr << 24) + r;
            freq_big[cnt_big[x]]--;
            if(cnt_big[x] == ans_big && !freq_big[cnt_big[x]]) ans_big--;
            cnt_big[x]--;
          }
        }
        
        for(int pr : to_del) {
          u_pr[pr] = 0;
        }
      }
    } else {
      u.push_back(id);
      pos[id] = u.size();
      c++;
      
      for(int i = 0; i < p.size(); i++) {
        int r = id % p[i];
        cnt[i][r]++;
        freq[cnt[i][r]]++;
        ans_small = max(ans_small, cnt[i][r]);
      }
      
      if(c > bnd2) take_big = 0;
      
      if(take_big) {
        vector<int> to_del;
        to_del.reserve(sz(u));
        for(int x : u) {
          ll pr = mx_pr[abs(x - id)];
          if(!u_pr[pr]) {
            to_del.push_back(pr);
            u_pr[pr] = 1;
            int r = id % pr;
            ll x = (pr << 24) + r;
            cnt_big[x]++;
            freq_big[cnt_big[x]]++;
            ans_big = max(ans_big, cnt_big[x]);
          }
        }
        
        for(int pr : to_del) {
          u_pr[pr] = 0;
        }
      }
    }
    
    if(!c) cout << 0 << '\n';
    else {
      int ans = ans_small;
      if(take_big) ans = max(ans, ans_big + 1);
      cout << ans << '\n';
      
      if(take_big) tot_big++;
    }
  }
  
  debug(n, q, tot_big, tot_rebuild);
}