DMOPC '20 Contest 3 P5 - Tower of Power

View as PDF

Submit solution


Points: 25 (partial)
Time limit: 2.0s
Memory limit: 256M

Author:
Problem type

Ben is playing around with some tower blocks!

Each of his N tower blocks are labelled with an integer, ai. He stacks them vertically, with a1 at the bottom and aN at the top.

Ben built his tower and is very proud of it, but is now wondering how powerful it is. As everyone knows, a single tower block's power is equal to its label.

However, in a tower consisting of multiple blocks, the power grows very quickly. In a tower of N blocks, the power is equal to a1(a2(a3)). Formally, if P(a1,a2,,aN) is the power of a tower consisting of N blocks with labels ai, then P(a1,a2,,aN)=a1P(a2,a3,,aN), with P(aN)=aN.

Ben really wants to know the power of his tower. However, knowing his number might be way too big, he will be happy if you can tell him the power modulo M. Can you help him?

Constraints

1N106

2M109

1ai109

Subtask 1 [1/15]

1N3

M is prime.

Subtask 2 [14/15]

No additional constraints.

Input Specification

The first line will contain N, the number of blocks in Ben's tower, and M, the modulus.

The next line will contain N integers, the labels on Ben's blocks, from a1 to aN.

Output Specification

Output the power of Ben's tower, mod M.

Sample Input 1

Copy
3 5
2 3 2

Sample Output 1

Copy
2

Explanation for Sample Output 1

The power of Ben's tower is 2(32)295122(mod5).

Note that you do not need to get the correct output on this case to pass the first subtask.

Sample Input 2

Copy
3 17
3 5 7

Sample Output 2

Copy
12

Sample Input 3

Copy
12 35929738
62525611 69201951 54844075 40933790 64603110 102648769 67604167 54424854 69048209 51968609 55767140 95916210

Sample Output 3

Copy
9996111

Comments


  • 3
    Kevy3030  commented on Feb. 2, 2021, 12:41 a.m.

    case 61 :/