QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 32 MB Total points: 10

#11654. Ant

Statistics

A Byteotian ant is walking along the edges of ABCDEFGH cube:

problem_11654_1.png

It tries to find out, in how many ways it can go from one given vertex, to another given vertex, walking along exactly $ k $ edges (when the ant enters an edge, it will not turn back and will finally reach the second end of this edge). If the ant goes through some edge $ x $ times, we count this edge $ x $ times. The ant would like to have interesting routes, that is if the ant is in some vertex, it would like to leave this vertex using an edge other than the edge recently used to enter this vertex (i.e. it want not to use the same edge twice in a row).

Our ant is not so smart, because it can only count using integers from 0 to $p-1$, for some $ p $, so you should compute the result modulo $ p $.

Write a program which:

  • reads the starting and the ending vertex of the ant's route, number of edges on the ant's route, and integer $ p $,
  • computes number of interesting routes, which satisfy the ant's requests, modulo $ p $,
  • writes the answer to the standard output.

Input Format

The first line of the standard input contains two capital English letters $v_{1}$ and $v_{2}$ ($ A ≤ v_{1}, v_{2} ≤ H $, $v_{1} ≠ v_{2}$), separated by a single space. The two letters denote the starting and ending vertex of the ant's route respectively. The second line contains two integers $ k $ and $ p $ ($1 ≤ k ≤ 2\,000\,000\,000$, $2 ≤ p ≤ 1\,000\,000\,000$), separated by a single space.

Output Format

Exactly one integer is to be written on the standard output. This integer is the number of interesting routes from the vertex $ v_{1}$ to the vertex $v_{2}$, containing exactly $ k $ edges, modulo $ p $.

Example

Input

A B
3 100

Output

2
problem_11654_2.png

Discussions

About Discussions

The discussion section is only for posting: Editorials, General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues. Submitting multiple issues may cause your account to be banned.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.