本文共 1057 字,大约阅读时间需要 3 分钟。
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.
如果对模幂算法不了解可参考#include#include #include 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 & 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 b = { 1,0 }; //cout << modpow(4, 13, 497) << endl; cout << superPow(2, b);}
转载地址:http://dvdoi.baihongyu.com/