# Super Pow

class Solution {
public:
int superPow(int a, vector<int>& b) {
int ans = 1;
a = a % 1337;
while (morethanzero(b))
{
if (IsOdd(b))
ans = (ans * a) % 1337;
div(b, 2);
a = (a * a) % 1337;
}
return ans;
}

void div(vector<int> &x, int y)
{
int tmp = 0;
for (int i = 0; i < x.size(); i++)
{
x[i] += tmp * 10;
tmp = x[i] % y;
x[i] = x[i] / y;
}
}

bool IsOdd(const vector<int> & nums)
{
if (nums.back() & 1)
return true;
else
return false;
}

bool morethanzero(vector<int> & x) {
for (int i = x.size() - 1; i >= 0; i--) {
if (x[i] > 0) return true;
}
return false;
}
};

a^1234567 % k = (a^1234560 % k) * (a^7 % k) % k = (a^123456 % k)^10 % k * (a^7 % k) % k

class Solution {
const int base = 1337;
int powmod(int a, int k) //a^k mod 1337 where 0 <= k <= 10
{
a %= base;
int result = 1;
for (int i = 0; i < k; ++i)
result = (result * a) % base;
return result;
}
public:
int superPow(int a, vector<int>& b) {
if (b.empty()) return 1;
int last_digit = b.back();
b.pop_back();
return powmod(superPow(a, b), 10) * powmod(a, last_digit) % base;
}
};

• 本文已收录于以下专栏：

举报原因： 您举报文章：Super Pow 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)