QOJ.ac

QOJ

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

#11654. Ant

統計

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
About Issues

We understand that our problem archive is not perfect. If you find any issues with the problem, including the statement, scoring configuration, time/memory limits, test cases, etc.

You may use this form to submit an issue regarding the problem. A problem moderator will review your issue and proceed it properly.

STOP! Before you submit an issue, please READ the following guidelines:

  1. This is not a place to publish a discussion, editorial, or requests to debug your code. Your issue will only be visible by you and problem moderators. Other users will not be able to view or reply your issues.
  2. Do not submit duplicated issues. If you have already submitted one, please wait for an moderator to review it. Submitting multiple issues will not speed up the review process and might cause your account to be banned.
  3. Issues must be filed in English or Chinese only.
  4. Be sure your issue is related to this problem. If you need to submit an issue regarding another problem, contest, category, etc., you should submit it to the corresponding page.

Active Issues 0

No issues in this category.

Closed/Resolved Issues 0

No issues in this category.