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.
如果对模幂算法不了解可参考
http://blog.csdn.net/taoyanqi8932/article/details/51918879
#include <iostream>
#include <vector>
#include <string>
using namespace std;
const int modulus = 1024;
int modpow(int base, int exponent, int modulus) {
int result = 1;
while (exponent > 0) {
if (exponent & 1) {
// multiply in this bit's contribution while using modulus to keep result small
result = (result * base) % modulus;
}
// move to the next bit of the exponent, square (and mod) the base accordingly
exponent >>= 1;
base = (base * base) % modulus;
}
return result;
}
int superPow(int a, vector<int>& b)
{
int n = b.size();
int result = 1;
for (int i = n - 1; i >= 0; i--) {
result = (result*modpow(a, b[i], modulus)) % modulus;
a = modpow(a, 10,modulus);
}
return result;
}
int main()
{
vector<int>b = { 1,0 };
//cout << modpow(4, 13, 497) << endl;
cout << superPow(2, b);
}