Power towers
Time limit2sMemory limit512 MB
Given lists of positive integers, compute each power tower modulo M, where the tower can be astronomically large.
- Level
Hard8 of 10
- Topics
- Number theory, Recursion, Math, Implementation
- Solved
- No attempts yet
Problem
You are given a non-empty list of positive integers . The power tower of this list is the number
The exponents are evaluated from the top down, so the value is .
A power tower grows very large even when the list holds only a few small integers. The power tower of is , and the power tower of is , which has 358 decimal digits.
Compute each power tower modulo a given positive integer .
Input
The first line contains two positive integers and . Each of the next lines contains a positive integer followed by positive integers . Consecutive numbers on a line are separated by exactly one space.
Output
Print lines. Line contains the power tower of the -th list modulo .
Constraints
- The lengths of all lists sum to at most .
Hint
When , the answer for each list is the last decimal digit of its power tower. Most of the lists in the sample input have a power tower too large to fit in a 32-bit or 64-bit integer.