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: Accepted)

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>

using namespace std;

int main() {
	long long n;
	int m, q;
	cin >> n >> m >> q;

	vector<int> a(q);
	for (int i = 0; i < q; i++) {
		cin >> a[i];
	}

	int T = 0;
	for (int i = 1;; i++) {
		if (m * i % 7 == 0) {
			T = i;
			break;
		}
	}
	int H = m * T / 7;
	vector<vector<bool>> check1(H, vector<bool>(7));
	vector<vector<bool>> check2(H * 2, vector<bool>(7));
	for (int i = 0; i < q; i++) {
		for (int j = 0; j < T; j++) {
			int val = a[i] + m * j;
			int y = val / 7;
			int x = val % 7;
			check1[y][x] = true;
			check2[y][x] = true;
			check2[y + H][x] = true;
		}
	}

	function<void(vector<vector<bool>> &, int, int)> dfs = [&](vector<vector<bool>> &check, int y, int x) {
		const int dy[] = { 0, 1, 0, -1 };
		const int dx[] = { 1, 0, -1, 0 };
		check[y][x] = true;
		for (int k = 0; k < 4; k++) {
			int yy = y + dy[k];
			int xx = x + dx[k];
			if (yy < 0 || xx < 0 || yy >= check.size() || xx >= 7) {
				continue;
			}
			if (check[yy][xx]) {
				continue;
			}
			dfs(check, yy, xx);
		}
	};

	auto count = [&](auto v) {
		int ret = 0;
		for (int i = 0; i < v.size(); i++) {
			for (int j = 0; j < 7; j++) {
				if (!v[i][j]) {
					dfs(v, i, j);
					ret++;
				}
			}
		}
		return ret;
	};

	int one = count(check1);
	int two = count(check2);

	int diff = 2 * one - two;
	long long ans = n / H * one - (n / H - 1) * diff;
	cout << ans << endl;
}