The task is to raise a large number n (0 <= n <= 10 ^ 50) to the power of m (0 <m <1000). For this, of course, I use long arithmetic
typedef vector<long long> largen; And also binary exponentiation.
largen recbinpow(largen a, int n) { largen e; e.push_back(1); if (n == 0) return e; if (n % 2 == 1) return multiply(recbinpow(a, n - 1), a); else { largen b = recbinpow(a, n / 2); return multiply(b, b); } } But it still turned out to be too slow. I found out that if we take the base of the number system more, for example 10 ^ 9, then the exponentiation will be much faster.
const int base = 1000 * 1000 * 1000; largen multiply(largen a, largen b) { largen c(a.size() + b.size()); for (size_t i = 0; i<a.size(); ++i) for (int j = 0, carry = 0; j < (int)b.size() || carry; ++j) { long long cur = c[i + j] + a[i] * 1ll *(j < (int)b.size() ? b[j] : 0) + carry; carry = (long long)(cur / base); c[i + j] = (long long)(cur % base); } while (c.size() > 1 && c.back() == 0) c.pop_back(); return c; } But, for reasons unknown to me, the program does not work correctly on some values, for example, 999,999,999.
Debugging hours did not lead to anything. Please help me find an error program .