TopCoder problem "FactorialTower" used in SRM 283 (Division I Level Two)



Problem Statement

    

Note: this problem statement contains an image that may not display properly if viewed outside the applet.

Given an int m and a int[] a containing n numbers, return the result of the following expression:

 

Definition

    
Class:FactorialTower
Method:exprValue
Parameters:int[], int
Returns:int
Method signature:int exprValue(int[] a, int m)
(be sure your method is public)
    
 

Notes

-The expression is calculated from top to bottom. See example 5.
-"x mod y" means the remainder of x divided by y.
-Assume 0! = 1.
 

Constraints

-a will contain between 1 and 50 elements, inclusive.
-Each element of a will be between 0 and 2147483647, inclusive.
-m will be between 1 and 40000, inclusive.
 

Examples

0)
    
{2,2,2,2}
10
Returns: 6
Last digit of 65536.
1)
    
{3,8}
100
Returns: 76
(3!8!) mod 100 = 76.
2)
    
{775,967,1,1,1}
39877
Returns: 4832
3)
    
{0}
40000
Returns: 1
Remember that 0! is 1.
4)
    
{5,4,123456789,987654321,687955994,1162330089,195458992,1687386387,
1092101949,1086091397,902287014,341660341,1600834655,131565486,1494304912,
384585199,1883009662,916903507,1895924341,397197724,1883021244,1616061080,
1437763283,621297237,1883141487,282508826,285957625,1249530903,1484124572,
1822993209,1668351723,174237628,1589170712,992959839,1281777023,1158939002,
1488964134,1806511164,1519656093,159671689,542573000,1571768398,1203500608,
1053387535,793607740,1535801392,1798307613,552983945,901802936,1023595016}
40000
Returns: 0
5)
    
{2,3,2}
36864
Returns: 4096
The expression is calculated from top to bottom.

(2! to the power of (3!2!)) mod 36864 = 4096.

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=5961

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=8080&pm=5961

Writer:

gevak

Testers:

PabloGilberto , brett1479 , Olexiy

Problem categories:

Math, Simple Search, Iteration