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<iostream>
#include<algorithm>
#include<queue>
using namespace std;
long n;int m,q,a[1<<17];
bool f[7][1<<17];
int dx[]={0,1,0,-1},dy[]={1,0,-1,0};
void bfs(int x,int y,long _n)
{
	queue<pair<int,int> >P;
	f[x][y]=true;
	P.push(make_pair(x,y));
	while(!P.empty())
	{
		pair<int,int>p=P.front();P.pop();
		for(int r=0;r<4;r++)
		{
			int tx=p.first+dx[r],ty=p.second+dy[r];
			if(tx<0||ty<0||tx>=7||ty>=_n||f[tx][ty])continue;
			f[tx][ty]=true;
			P.push(make_pair(tx,ty));
		}
	}
}
main()
{
	cin>>n>>m>>q;
	if(n<=100000)
	{
		for(int i=0;i<q;i++)
		{
			cin>>a[i];
		}
		for(int i=0;i<7;i++)
		{
			for(int j=0;j<n;j++)
			{
				int b=(j*7+i)%m;
				if(upper_bound(a,a+q,b)!=lower_bound(a,a+q,b))f[i][j]=true;
			}
		}
		long ans=0;
		for(int i=0;i<7;i++)
		{
			for(int j=0;j<n;j++)
			{
				if(!f[i][j])
				{
					ans++;
					bfs(i,j,n);
				}
			}
		}
		cout<<ans<<endl;
	}
	else if(m%7==0)
	{
		for(int i=0;i<7;i++)
		{
			for(int j=0;j<m/7*2;j++)
			{
				int b=(j*7+1)%m;
				if(upper_bound(a,a+q,b)!=lower_bound(a,a+q,b))f[i][j]=true;
			}
		}
		long count1=0;
		for(int i=0;i<7;i++)
		{
			for(int j=0;j<m/7*2;j++)
			{
				if(!f[i][j])
				{
					count1++;
					bfs(i,j,m/7*2);
				}
			}
		}
		for(int i=0;i<7;i++)
		{
			for(int j=0;j<m/7*3;j++)
			{
				f[i][j]=false;
				int b=(j*7+i)%m;
				if(upper_bound(a,a+q,b)!=lower_bound(a,a+q,b))f[i][j]=true;
			}
		}
		long count2=0;
		for(int i=0;i<7;i++)
		{
			for(int j=0;j<m/7*3;j++)
			{
				if(!f[i][j])
				{
					count2++;
					bfs(i,j,m/7*3);
				}
			}
		}
		long kousa=count2-count1;
		long syokou=count1-kousa*2;
		cout<<syokou+kousa*(n/(m/7))<<endl;
	}
}