天天看点

UVA 10692 Huge ModProblem X

UVA 10692 Huge ModProblem X

the

operator for exponentiation is different from the addition, subtraction,

multiplication or division operators in the sense that the default associativity

for exponentiation goes right to left instead of left to right. so unless we

mess it up by placing parenthesis, 

UVA 10692 Huge ModProblem X

 should mean

UVA 10692 Huge ModProblem X

 not 

UVA 10692 Huge ModProblem X

. this leads to the

obvious fact that if we take the levels of exponents higher (i.e., 2^3^4^5^3),

the numbers can become quite big. but let‘s not make life miserable. we being

the good guys would force the ultimate value to be no more than 10000.

given a1, a2, a3, ... ,

an and m(=10000)

you only need to compute

a1^a2^a3^...^an mod m.

there

can be multiple (not more than 100) test cases. each test case will be presented

in a single line. the first line of each test case would contain the value for

m(2<=m<=10000). the next number of that line would be n(1<=n<=10).

then n numbers - the values for a1,

a2,

a3,

... , an would

follow. you can safely assume that 1<=ai<=1000.

the end of input is marked by a line containing a single hash (‘#‘) mark.

for

each of the test cases, print the test case number followed by the value of

a1^a2^a3^...^an mod

m on one line. the sample output shows the exact format for printing the test

case number.

题意:求出 a0^a1^a2......a^n%m的值。

sl: 以前做过一个a^b mod 1e9+7 的题,那个很显然是费马小定理。碰见这个题目傻逼了。

百度学习一翻知:a^x=a^(x%phi[[m]+phi[m]) (phi[m]<=x)   很显然一个递归的式子。

哎,但是当时每次都是对x%phi[mod] 傻叉了。应该递归求解。 

 坑了我4个点真吭。。。