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 <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

const int dh[] = {1, -1, 0, 0};
const int dw[] = {0, 0, 1, -1};

int g, cnt1 = 0, cnt2 = 0, cnt3 = 0;

void dfs(int h, int w, vector<vector<bool> >& b) {
	stack<P> s;
	s.push(P(h, w));
	bool flag1 = false, flag2 = false;
	while (!s.empty()) {
		int ch, cw;
		tie(ch, cw) = s.top();
		s.pop();
		b[ch][cw] = true;
		if (h == 0 && ch == g - 1) {
			if (!flag1) flag2 = true;
			if (w == cw) {
				flag2 = false;
				flag1 = true;
			}
		}
		for (int i = 0; i < 4; i++) {
			int nh = ch + dh[i], nw = cw + dw[i];
			if (nh < 0 || nh >= g || nw < 0 || nw >= 7) continue;
			if (b[nh][nw]) continue;
			s.push(P(nh, nw));
		}
	}
	cnt3 += flag1;
	cnt2 += flag2;
	cnt1 += !(flag1 || flag2);
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	ll n;
	int m, q;
	cin >> n >> m >> q;
	vector<bool> memo(m, false);
	for (int i = 0; i < q; i++) {
		int a;
		cin >> a;
		memo[a] = true;
	}

	g = m / __gcd(m, 7);

	vector< vector<bool> > b(g, vector<bool>(7, false));
	for (int i = 0; i < g; i++) {
		for (int j = 0; j < 7; j++) {
			int x = 7 * i + j;
			b[i][j] = memo[x % m];
		}
	}
	
	for (int i = 0; i < g; i++) {
		for (int j = 0; j < 7; j++) {
			if (!b[i][j]) dfs(i, j, b);
		}
	}

	ll ans = 0;
	ans += cnt3;
	ans += (n / g + 1) * cnt2;
	ans += (n / g) * cnt1;
	cout << ans << endl;
	return 0;
}