statement.txt
======================
197. Nice Patterns Strike Back
time limit per test: 0.5 sec.
memory limit per test: 65536 KB
input: standard
output: standard



You might have noticed that there is the new fashion among rich people to have their yards tiled with black and white tiles, forming a pattern. The company Broken Tiles is well known as the best tiling company in our region. It provides the widest choices of nice patterns to tile your yard with. The pattern is nice if there is no square of size 2 × 2, such that all tiles in it have the same color. So patterns on the figure 1 are nice, while patterns on the figure 2 are not.





The president of the company wonders whether the variety of nice patterns he can provide to the clients is large enough. Thus he asks you to find out the number of nice patterns that can be used to tile the yard of size N × M. Now he is interested in the long term estimation, so he suggests N ≤ 10100. However, he does not like big numbers, so he asks you to find the answer modulo P.

Input

The input file contains three integer numbers: N (1 ≤ N ≤ 10100), M (1 ≤ M ≤ 5) and P (1 ≤ P ≤ 10000).

Output

Write the number of nice patterns of size N × M modulo P to the output file.

Sample test(s)

Input

Test #1

2 2 5

Test #2

3 3 23

Output

Test #1

4

Test #2

0


Author:	Andrew Stankevich
Resource:	Petrozavodsk Winter Trainings 2003
Date:	2003-02-06

=================
p197.py
======================
def matrix_multiply(A, B, mod):
    n = len(A)
    C = [[0] * n for _ in range(n)]
    for i in range(n):
        for j in range(n):
            for k in range(n):
                C[i][j] += A[i][k] * B[k][j]
            C[i][j] %= mod

    return C


def matrix_vector_multiply(A, v, mod):
    n = len(A)
    result = [0] * n
    for i in range(n):
        for j in range(n):
            result[i] += A[i][j] * v[j]
        result[i] %= mod

    return result


def matrix_power(matrix, n, mod):
    result = [
        [1 if i == j else 0 for j in range(len(matrix))]
        for i in range(len(matrix))
    ]
    while n > 0:
        if n & 1:
            result = matrix_multiply(result, matrix, mod)
        matrix = matrix_multiply(matrix, matrix, mod)
        n >>= 1
    return result


def count_nice_patterns(N, M, P):
    transition_matrix = [[0 for _ in range(1 << M)] for _ in range(1 << M)]

    for current in range(1 << M):
        for next_state in range(1 << M):
            valid = True
            for i in range(1, M):
                if (
                    (current >> (i - 1)) & 1
                    and (current >> i) & 1
                    and (next_state >> (i - 1)) & 1
                    and (next_state >> i) & 1
                ):
                    valid = False
                    break

                if (
                    not ((current >> (i - 1)) & 1)
                    and not ((current >> i) & 1)
                    and not ((next_state >> (i - 1)) & 1)
                    and not ((next_state >> i) & 1)
                ):
                    valid = False
                    break

            if valid:
                transition_matrix[current][next_state] = 1

    start_state = [1] * (1 << M)

    result_matrix = matrix_power(transition_matrix, N - 1, P)
    start_state = matrix_vector_multiply(result_matrix, start_state, P)

    return sum(start_state) % P


N, M, P = map(int, input().split())
print(count_nice_patterns(N, M, P))

=================
