1 解题思想
这道题需要计算 a^b % c 的值,其中b非常的大,大到只能使用数组来表示。这道题是ACM里面常见的快速幂的解题方式,这其中有一个数学的推论,可以看我代码里附带的那个解释。
总之,这个公式的意思就是,(a*b)%c=(a%c)*(b%c),因此我们可以在每一步计算结果之后都这么处理,防止溢出。
第二个算法部分其实很容易理解,就是可以做类似于二分的分割,比如当b是偶数的时候,我们可以转化为计算a^(b/2)后再平方,而对于基础,则再乘一个a就可以,总之你看代码就知道了
2 原题
Your task is to calculate ab mod 1337 where a is a positive integer and b is an extremely large positive integer given in the form of an array.
Example1:
a = 2
b = [3]
Result: 8
Example2:
a = 2
b = [1,0]
Result: 1024
Credits:
3 AC解
public class Solution {
/**
* 这道题是ACM里面入门问题,powmod,详细的算法可以参照这里:http://blog.csdn.net/xuruoxin/article/details/8578992
* */
// 判断是否大于0
public boolean morethanzero(int[] x){
for(int i=x.length-1;i>=0;i--){
if(x[i]>0) return true;
}
return false;
}
//高精度除法
public void div(int[] x,int y){
int tmp=0;
for(int i=0;i<x.length;i++){
x[i] += tmp*10;
tmp = x[i] % y;
x[i] = x[i] /y;
}
}
public int superPow(int a, int[] b) {
if (morethanzero(b) == false) return 1;
a=a%1337;
boolean isEven = false;
if(b[b.length-1] % 2 == 0) isEven = true;
div(b,2);
int result = superPow(a,b);
result = result % 1337;
result*=result;
result = result % 1337;
if(isEven==false){
result*=a;
result = result % 1337;
}
return result;
}
}