UVA 10692 Huge Mod

Problem X

Huge Mod 

Input: standard input
Output: standard output
Time Limit: 1 second

2^3^4^5 mod 10 = ?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, 2^3^2 should mean2^(3^2)=2^9=512 not (2^3)^2=8^2=64. 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 a 1, a 2, a 3, ... , a N and m(=10000)
you only need to compute a 1^a 2^a 3^...^a N 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 a 1 , a 2 , a 3 , ... , a N  would follow. You can safely assume that 1<=a i <=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 a 1 ^a 2 ^a 3 ^...^a N  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个点真吭。。。

 

转载于:https://www.cnblogs.com/acvc/p/3702412.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值