Problem:
Max Score: 500 Points

Problem Statement

We have a grid with n rows and 7 columns. We call it a calendar. The cell at i-th row and j-th column is denoted (i, j).

Initially, each cell at (i, j) contains the integer 7i + j - 8, and each cell is white.

Snuke likes painting, so he decided integer m, and did q operations with a calendar.

・In i-th operation, he paint black on the cell in which an integer is written such remainder of dividing by m is a_i.

Please count the number of connected white parts.

Note that if two adjacent cells are white, the cells belong to the same connected part.

Input Format

The input format is following:

n m q
a_1 a_2 ... a_q

Output Format

Print the number of connected part in one line.

Constraints

n ≤ 10^{12}

7n is divisible by m.

1 ≤ q ≤ m ≤ 10^5

0 ≤ a_1 < a_2 < ... < a_q < m

Scoring

Subtask 1 [100 points]

n ≤ 100000.

Subtask 2 [90 points]

m is divisible by 7.

a_{i + 1} - a_i = 1.

Subtask 3 [200 points]

m is divisible by 7.

Subtask 4 [110 points]

There are no additional constraints.

Sample Input 1

7 7 3
1 3 5

Sample Output 1

4

The calendar looks like this:

Sample Input 2

10 14 8
5 6 7 8 9 10 11 12

Sample Output 2

10

The calendar looks like this:

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cmath>
using namespace std;

typedef pair<int, int> P;
#define rep(i, n) for (int i=0; i<(n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007

long long N;
int M, Q;
bool X[100000][7];
int A[100001];
int U[700000], Rk[700000];
bool F[700000];
int QC;

int find(int x) {
  if (U[x] == x) return x;
  return U[x] = find(U[x]);
}

void unite(int x, int y) {
  x = find(x), y = find(y);
  if (x == y) return;
  if (Rk[x] < Rk[y]) swap(x, y);
  if (U[y] == y) QC--;
  U[y] = x;
  Rk[x] += Rk[y];
  Rk[y] = 0;
  if (F[y]) F[x] = true;
  F[y] = false;
}

bool same(int x, int y) {
  return find(x) == find(y);
}

signed main() {
  ios::sync_with_stdio(false); cin.tie(0);
  cin >> N >> M >> Q;
  for (int i=0; i<7*M; i++) U[i] = i, Rk[i] = 1;
  rep(i, Q) {
    int a;
    cin >> a;
    rep(k, 7) {
      int x = a + k*M;
      X[x/7][x%7] = true;
    }
  }
  if (N == 0) {
    cout << 0 << "\n";
    return 0;
  }
  /*
  rep(i, M) {
    rep(j, 7)cout<<X[i][j];
    cout<<"\n";
  }
  */
  rep(x, M) {
    for (int i=1; i<7; i++) {
      if (!X[x][i] && !X[x][i-1]) {
        unite(7*x+i, 7*x+i-1);
      }
    }
  }

  if (N<M) {
    rep(x, N) {
      if (x > 0) {
        rep(i, 7) {
          if (!X[x][i] && !X[(x+M-1)%M][i]) {
            unite(7*x+i, 7*((x+M-1)%M)+i);
          }
        }
      }
    }
    int c = 0;
    rep(x, N) {
      rep(i, 7) {
        if (X[x][i]) continue;
        if (U[7*x+i] == 7*x+i) c++;
      }
    }
    cout << c << "\n";
    return 0;
  }
  rep(x, M) {
    rep(i, 7) {
      if (!X[x][i]) QC++;
    }
    if (x>0)
    rep(i, 7) {
      if (!X[x][i] && !X[(x+M-1)%M][i]) {
        unite(7*x+i, 7*((x+M-1)%M)+i);
      }
    }
  }
  long long sum = QC;
  rep(i, 7) {
    if (!X[0][i] && !X[M-1][i]) {
      unite(7*0+i, 7*(M-1)+i);
      F[find(i)] = true;
    }
  }
  int c = 0;
  rep(x, M) {
    rep(i, 7) {
      if (X[x][i]) continue;
      if (U[7*x+i] == 7*x+i && !F[7*x+i]) c++;
    }
  }
  sum += 1LL * c * (N/M);

  for (int i=0; i<7*M; i++) U[i] = i, Rk[i] = 1, F[i] = false;
  rep(x, M) {
    for (int i=1; i<7; i++) {
      if (!X[x][i] && !X[x][i-1]) {
        unite(7*x+i, 7*x+i-1);
      }
    }
  }
  rep(i, 7) {
    if (!X[0][i] && !X[M-1][i]) {
      unite(7*0+i, 7*(M-1)+i);
      F[find(i)] = true;
    }
  }
  rep(x, N%M) {
    rep(i, 7) {
      if (!X[x][i]) QC++;
    }
    if (x>0)
    rep(i, 7) {
      if (!X[x][i] && !X[(x+M-1)%M][i]) {
        unite(7*x+i, 7*((x+M-1)%M)+i);
      }
    }
  }
  c = 0;
  rep(x, N%M) {
    rep(i, 7) {
      if (X[x][i]) continue;
      if (U[7*x+i] == 7*x+i && !F[7*x+i]) c++;
    }
  }
  sum += c;
  cout<<sum<<"\n";
  return 0;
}