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: 



Sample input:
7 7 3
1 3 5


Sample output:
4

Submission status: Wrong Answer

Solution code:
from queue import Queue

n,m,q=map(int,input().split())
a=list(map(int,input().split()))

def count_group(size):
    cc = [False]*size
    for i in range(q):
        j = 0
        while a[i]+m*j < size:
            cc[a[i]+m*j] = True
            j += 1

    gg = [0]*size
    ox = [-1, 0, 1, 0]
    oy = [0, -7, 0, 7]
    def bfs(i, g):
        qq = Queue()
        qq.put(i, False)
        while not qq.empty():
            ii = qq.get(False)
            gg[ii] = g
            for t in range(4):
                if ii % 7  == 0 and ox[t] == -1: continue
                if (ii+1) % 7  == 0 and ox[t] == 1: continue
                j = ii+ox[t]+oy[t]
                if 0 <= j < size:
                    if cc[j] or gg[j] != 0: continue
                    qq.put(j, False)

    maxg = 0
    for i in range(size):
        if not cc[i] and gg[i] == 0:
            maxg += 1
            bfs(i, maxg)

    return maxg

block = (m+7)//7*7*7
if n*7 <= block:
    print(count_group(n*7))
    exit(0)
c1 = count_group(block)
c2 = count_group(block*2)
c3 = count_group(block+((n*7)%block))
print(c1 + ((n*7-block)//block)*(c2-c1) + (c3-c1))

Submission status: Accepted

Solution code:
import sys
import numpy as np
import numba
from numba import njit, b1, i4, i8

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

@njit((i8, i8, i8[:]), cache=True)
def solve_naive(H, M, A):
    visited = np.zeros(7 * H, np.bool_)
    for a in A:
        visited[a::M] = 1
    st = np.empty(7 * H + 10, np.int64)
    ncomp = 0
    for v in range(7 * H):
        if visited[v]:
            continue
        ncomp += 1
        st[0] = v
        p = 1
        while p:
            p -= 1
            v = st[p]
            x, y = divmod(v, 7)
            for d in (1, -1, 7, -7):
                w = v + d
                if w < 0 or w >= 7 * H:
                    continue
                x1, y1 = divmod(w, 7)
                if abs(x - x1) + abs(y - y1) != 1:
                    continue
                if visited[w]:
                    continue
                visited[w] = True
                st[p] = w
                p += 1
    return ncomp

N, M, Q = map(int, readline().split())
A = np.array(readline().split(), np.int64)

if N <= 5 * M:
    x = solve_naive(N, M, A)
else:
    q, r = divmod(N, M)
    a, b = solve_naive(r + 3 * M, M, A), solve_naive(r + 4 * M, M, A)
    x = a + (q - 3) * (b - a)
print(x)

Submission status: Runtime Error

Solution code:
n,m,q=map(int,input().split())
a=list(map(int,input().split()))

def count_group(size):
    cc = [False]*size
    for i in range(q):
        j = 0
        while a[i]+m*j < size:
            cc[a[i]+m*j] = True
            j += 1

    gg = [0]*size
    ox = [-1, 0, 1, 0]
    oy = [0, -7, 0, 7]
    def dfs(i, g):
        if cc[i] or gg[i] != 0: return
        gg[i] = g
        for t in range(4):
            if i % 7  == 0 and ox[t] == -1: continue
            if (i+1) % 7  == 0 and ox[t] == 1: continue
            j = i+ox[t]+oy[t]
            if 0 <= j < size:
                dfs(j, g)

    maxg = 0
    for i in range(size):
        if not cc[i] and gg[i] == 0:
            maxg += 1
            dfs(i, maxg)

    return maxg

block = (m+7)//7*7*7
if n <= block:
    print(count_group(n*7))
    exit(0)
c1 = count_group(block)
c2 = count_group(block*2)
c3 = count_group(block+((n*7)%block))
print(c1 + ((n*7-block)//block)*(c2-c1) + (c3-c1))