This page is still under construction.

Parts of this page are still being built. What you see may change.

Ant

Time limit1sMemory limit128 MB

Summary
Count walks of exactly k edges from one cube vertex to another, never reusing the edge just used, modulo p.
Level

Medium7 of 10

Topics
Matrix, Dynamic programming, Math, Combinatorics
Solved
No attempts yet

Problem

An ant walks along the edges of a cube ABCDEFGHABCDEFGH.

cube

The ant wants to find out in how many ways it can travel from one given vertex to another given vertex by crossing exactly kk edges. (Once the ant steps onto an edge it never turns back partway, and always walks all the way to the other end of that edge.) If the ant crosses some edge xx times, that edge is counted xx times.

The ant only wants to count interesting routes: whenever it arrives at a vertex, it wants to leave that vertex through an edge different from the one it just used to enter it (it never uses the same edge twice in a row).

The ant can only count integers from 00 to p−1p - 1 for some pp, so report the answer modulo pp.

The cube is structured as follows: the bottom face is the square ABCDABCD (edges ABAB, BCBC, CDCD, DADA), the top face is the square EFGHEFGH (edges EFEF, FGFG, GHGH, HEHE), and the vertical edges AEAE, BFBF, CGCG, DHDH connect the two faces (12 edges in total).

Write a program that:

  • reads the starting vertex, the ending vertex, the number of edges on the route, and the integer pp,
  • computes the number of interesting routes that satisfy the conditions above, modulo pp,
  • writes the answer to standard output.

Input

The first line contains two uppercase English letters v1v_1 and v2v_2 (A≤v1,v2≤HA \le v_1, v_2 \le H, v1≠v2v_1 \ne v_2), separated by a single space, denoting the starting and the ending vertex respectively. The second line contains two integers kk and pp (1≤k≤2 000 000 0001 \le k \le 2\,000\,000\,000, 2≤p≤1 000 000 0002 \le p \le 1\,000\,000\,000), separated by a single space.

Output

Output a single integer: the number of interesting routes from vertex v1v_1 to vertex v2v_2 using exactly kk edges, modulo pp.

Hint

hint

Examples4

  1. Example 1

    Input
    A B
    3 100
    
    Expected output
    2
    
  2. Example 2

    Input
    A B
    1 1000
    
    Expected output
    1
    
  3. Example 3

    Input
    A G
    1 1000
    
    Expected output
    0
    
  4. Example 4

    Input
    A G
    3 1000
    
    Expected output
    6