CRUELL - Cô giáo dạy toán, phần I

Tác giả: ladpro98

Ngôn ngữ: C++

#include <bits/stdc++.h>

using namespace std;
typedef vector<int> BigInt;
const int BASE = 10000;

BigInt N;
int p, n;

BigInt operator + (BigInt a, BigInt b) {
    BigInt c; int carry = 0;
    for(int i = 0; i < a.size() || i < b.size(); i++) {
        if (i < a.size()) carry += a[i];
        if (i < b.size()) carry += b[i];
        c.push_back(carry % BASE);
        carry /= BASE;
    }
    if (carry) c.push_back(carry);
    return c;
}

BigInt operator * (BigInt a, int b) {
    BigInt c; int carry = 0;
    for(int i = 0; i < a.size(); i++) {
        carry += a[i] * b;
        c.push_back(carry % BASE);
        carry /= BASE;
    }
    if (carry) c.push_back(carry);
    return c;
}

BigInt operator * (BigInt a, BigInt b) {
    BigInt c(a.size() + b.size() + 2, 0);
    for (int i = 0; i < a.size(); i++) {
        int carry = 0;
        for (int j = 0; j < b.size(); j++) {
            int k = i + j;
            c[k] += a[i] * b[j] + carry;
            carry = c[k] / BASE;
            c[k] %= BASE;
        }
        if (carry) c[i + b.size()] += carry;
    }
    while (*c.rbegin() == 0 && c.size() > 0)
        c.erase(c.begin() + c.size() - 1);
    return c;
}

void Print(BigInt a) {
    int L = a.size(), len = 0;
    char buffer[15055];
    len += sprintf(buffer, "%d", a[L - 1]);
    for(int i = L - 2; i >= 0; i--)
        len += sprintf(buffer + len, "%04d", a[i]);
    int cnt = 0;
    for(int i = 0; i < len; i++) {
        cnt++;
        if (cnt > 70) {printf("\n"); cnt = 1;}
        printf("%c", buffer[i]);
    }
}

BigInt Pow(int p) {
    if (p == 1) return N;
    BigInt x = Pow(p >> 1);
    x = x * x;
    if (p & 1) x = x * N;
    return x;
}

int main()
{
    scanf("%d %d", &n, &p);
    while (n) {N.push_back(n % BASE); n /= BASE;}
    BigInt res = Pow(p);
    Print(res);
    return 0;
}

Download