#P5255. Beautiful Tiling Patterns

    ID: 18491 Type: Default 1000ms 256MiB

Beautiful Tiling Patterns

Beautiful Tiling Patterns

In modern architecture, wealthy households often have villas with yards paved in black and white tiles. However, not every tiling is considered aesthetically pleasing. A tiling is regarded as beautiful if there is no 2 × 2 subgrid in which all tiles share the same color. In other words, for any 2 × 2 square in the grid, at least one tile must be different from the others.

Formally, given a grid of size N × M, every cell must be colored either black or white. A tiling is beautiful if for every 2 × 2 block with cells at positions (i, j), (i, j+1), (i+1, j) and (i+1, j+1) (where applicable) the following condition holds:

\[ \text{Not}(a = b = c = d)\] where a, b, c, d represent the colors (0 for white and 1 for black) of the four cells.

Your task is to compute the total number of beautiful tiling schemes for an N × M grid and output the answer modulo a given integer P.

inputFormat

The input consists of a single line with three integers N, M, and P, where:

  • N (number of rows)
  • M (number of columns)
  • P (the modulus)

You may assume that N and M are positive integers.

outputFormat

Output a single integer representing the number of beautiful tiling schemes modulo P.

sample

1 1 100
2