Cod sursa(job #814116)

Utilizator a_h1926Heidelbacher Andrei a_h1926 Data 15 noiembrie 2012 19:59:43
Problema Ridicare la putere in timp logaritmic Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.6 kb
#include <fstream>
#include <algorithm>
#include <cstring>

using namespace std;

typedef long long LL;

const int MaxC = 50;
const int Base = 10;

class Huge {
	public: int n[MaxC];

	Huge () {
		memset (n, 0, sizeof (n));
	}

	Huge(int A) {
	    *this = A;
	}

    inline void operator = (Huge A) {
		memcpy (n, A.n, sizeof (A.n));
	}

	inline void operator = (LL A) {
		memset (n, 0, sizeof (n));
		for (; A > 0; A /= Base)
            n[++n[0]] = A%Base;
	}

	inline LL operator% (LL B) const {
        LL T = 0;
        for (int i = n[0]; i > 0; --i)
            T = (T*10+n[i])%B;
        return T;
    }

	inline Huge operator* (Huge A) const {
		Huge C;	int i, j; LL T;
		for (i = 1; i <= n[0]; ++i) {
			for (T = 0, j = 1; j <= A.n[0] || T; ++j, T /= Base)
				C.n[i+j-1] = (T += (C.n[i+j-1]+1LL*n[i]*A.n[j]))%Base;
			if (i+j-2 > C.n[0])
                C.n[0] = i+j-2;
		}
		return C;
	}
};

Huge A, S;
LL B, Mod = 1999999973;

inline Huge Pow(Huge X, LL P) {
    Huge R = 1;
    while (P > 0) {
        if (P%2)
            R = (X*R)%Mod, --P;
        else
            X = (X*X)%Mod, P /= 2;
    }
    return R;
}

void Read() {
    ifstream fin("lgput.in");//("classictask.in");
    LL a;
    fin >> a >> B;// >> Mod;
    A = a;
    fin.close();
}

void Print() {
    ofstream fout("lgput.out");//("classictask.out");
    S = Pow(A, B);
    if (S.n[0] == 0)
        S.n[0] = 1;
    for (int i = S.n[0]; i > 0; --i)
        fout << S.n[i];
    fout << "\n";
    fout.close();
}

int main() {
    Read();
    Print();
    return 0;
}