Periodic Points

Time limit2sMemory limit128 MB

Summary
Count periodic points of period n for a piecewise linear map on [0,m] modulo a given value, detecting infinite solution cases.
Level

Hard9 of 10

Topics
Math, Geometry, Simulation
Solved
No attempts yet

Problem

Computing the number of fixed points, and more generally the number of periodic orbits, of a dynamical system is a question of interest across many fields of research. However, the dynamics can be very complicated to describe, even in seemingly simple models. In this problem you must count the periodic points of period nn of a piecewise linear map ff that maps the real interval [0,m][0, m] into itself. That is, given a map f:[0,m]→[0,m]f : [0, m] \rightarrow [0, m], you must compute the number of solutions of the equation fn(x)=xf^n(x) = x for x∈[0,m]x \in [0, m], where fnf^n is ff iterated nn times:

fn=f∘⋯∘f∘f⏟n times,f^n = \underbrace{f \circ \cdots \circ f \circ f}_{n \text{ times}},

and ∘\circ denotes composition of maps, (g∘h)(x)=g(h(x))(g \circ h)(x) = g(h(x)).

The maps satisfy the following properties:

  • mm is a positive integer, and ff sends every integer of [0,m][0, m] to an integer of [0,m][0, m]: for every k∈{0,1,…,m}k \in \{0, 1, \dots, m\} we have f(k)∈{0,1,…,m}f(k) \in \{0, 1, \dots, m\}.
  • For every k∈{0,1,…,m−1}k \in \{0, 1, \dots, m - 1\}, the map ff is linear on the interval [k,k+1][k, k+1]. That is, for every x∈[k,k+1]x \in [k, k+1] its image is f(x)=(k+1−x) f(k)+(x−k) f(k+1)f(x) = (k + 1 - x)\,f(k) + (x - k)\,f(k + 1), so the graph of ff on [k,k+1][k, k+1] is a straight line segment.

Because there may be many periodic points, output the result modulo a given integer. If there are infinitely many solutions, print Infinity instead.

Input

The input consists of several test cases, separated by single blank lines. Each test case begins with a line containing the integer mm (1≤m≤801 \le m \le 80). The next line describes the map ff: it contains the m+1m + 1 integers f(0),f(1),…,f(m)f(0), f(1), \dots, f(m), each between 00 and mm inclusive. The test case ends with a line containing two integers separated by a space: nn (1≤n≤50001 \le n \le 5000) and the modulus mod\mathit{mod} (2≤mod≤100002 \le \mathit{mod} \le 10000).

The input ends with a line containing a single 00.

Output

For each test case, output the number of solutions of the equation fn(x)=xf^n(x) = x in the interval [0,m][0, m], taken modulo mod\mathit{mod}. If there are infinitely many solutions, print Infinity instead.

Examples1

  1. Example 1

    Input
    2
    2 0 2
    2 10
    
    3
    0 1 3 2
    1 137
    
    3
    2 3 0 3
    20 10000
    
    0
    
    Expected output
    4
    Infinity
    9074