码迷,mamicode.com
首页 > 其他好文 > 详细

UVA 10692 Huge Mod

时间:2014-05-01 09:03:35      阅读:540      评论:0      收藏:0      [点我收藏+]

标签:style   ext   width   strong   color   int   cti   line   rgb   table   new   

Problem X

Huge Mod 

Input: standard input

Output: standard output

Time Limit: 1 second

bubuko.com,布布扣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, bubuko.com,布布扣 should meanbubuko.com,布布扣 not bubuko.com,布布扣. 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.

Input

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.

Output

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.

Sample Input

Sample Output

10 4 2 3 4 5 100 2 5 2 53 3 2 3 2 # 
Case #1: 2 Case #2: 25 Case #3: 35

 

 

题意:求出 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个点真吭。。。

 

UVA 10692 Huge Mod,布布扣,bubuko.com

UVA 10692 Huge Mod

标签:style   ext   width   strong   color   int   cti   line   rgb   table   new   

原文地址:http://www.cnblogs.com/acvc/p/3702412.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!