Cod sursa(job #2611041)

Utilizator david.teacaDavid Stefan Teaca david.teaca Data 6 mai 2020 10:47:14
Problema Numere 2 Scor 95
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.93 kb
#include <bits/stdc++.h>
using namespace std;
class Int {
public:
    Int(): Size(1), A{0} {}
    Int(const char *S): Size(0), A{0} {
            int N(strlen(S) - 2);
            for (int pow = 1; N >= 0; --N) {
                A[Size] += pow * (S[N] - '0');
                pow *= 10;
                if (pow == Base) {
                    pow = 1;
                    ++Size;
                }
            }
            ++Size;
        }
    Int(const int *S, const int N): Size(0), A{0} {
        for (; Size < N; ++Size)
            A[Size] = S[Size];
    }
    Int(int N) : Size(0), A{0} {
        for (; N; N /= Base)
            A[Size++] = N % Base;
        if (!Size) ++Size;
    }
    inline int operator[] (const int i) const {
        return A[i];
    }
    inline int size() const {
        return Size;
    }
    bool operator < (const Int& B) const {
        for (int i = max(Size, B.size()) - 1; i >= 0; --i)
            if (A[i] != B[i])
                return A[i] < B[i];
        return false;
    }
    bool operator <= (const Int& B) const {
        for (int i = max(Size, B.size()) - 1; i >= 0; --i)
            if (A[i] != B[i])
                return A[i] < B[i];
        return true;
    }
    bool operator == (const Int& B) const {
        for (int i = max(Size, B.size()) - 1; i >= 0; --i)
            if (A[i] != B[i])
                return false;
        return true;
    }
    inline Int operator += (const Int& B) {
        const int SizeB(B.size());
        int i, t(0);
        for (i = 0; i < Size || i < SizeB || t; ++i, t /= Base)
            A[i] = (t += A[i] + B[i]) % Base;
        Size = i;
        return *this;
    }
    inline Int operator + (const Int& B) const {
        const int SizeB = (B.size());
        int C[MaxSize];
        memcpy(C, A, sizeof(C));
        int i, t(0);
        for (i = 0; i < Size || i < SizeB || t; ++i, t /= Base)
            C[i] = (t += C[i] + B[i]) % Base;
        return Int(C, i);
    }
    inline Int operator *= (const Int& B) {
        const int SizeB(B.size());
        int i, j, t, C[MaxSize] = {0};
        int SizeC(0);
        for (i = 0; i < Size; i++) {
            for (t = 0, j = 0; j < SizeB || t; ++j, t /= Base)
                C[i + j] = (t += C[i + j] + A[i] * B[j]) % Base;
            if (i + j > SizeC) SizeC = i + j;
        }
        memcpy(A, C, sizeof(A));
        Size = SizeC;
        return *this;
    }
    inline Int operator *= (const int& B) {
        int i, t = 0;
        for (i = 0; i < Size || t; i++, t /= Base)
            A[i] = (t += A[i] * B) % Base;
        Size = i;
        return *this;
    }
    inline Int operator /= (const int& B) {
        int i, t(0);
        for (i = Size - 1; i >= 0; i--, t %= B)
              A[i] = (t = t * Base + A[i]) / B;
        for (; Size > 1 && !A[Size - 1]; Size--);
        return *this;
    }
    inline Int Pow(int y) {
        Int res(1), x(*this);
        while (y) {
            if (y & 1)
                res *= x;
            x *= x;
            y >>= 1;
        }
        return res;
    }
private:
    static const int MaxSize = 500, Base = 1000;
    int Size, A[MaxSize];
};
inline void Write(const Int &A) {
    for (int i = A.size() - 1; i >= 0; --i)
        cout << A[i];
}
char Number[111];
Int N, aux, step, res, unu(1);
int main() {
    ios::sync_with_stdio(false);
    freopen("numere2.in", "r", stdin);
    freopen("numere2.out", "w", stdout);
    fgets(Number, 111, stdin);
    N = Number;
    for (int pow = 335; pow; --pow) {
        step = 1;
        while (step.Pow(pow) < N)
            step *= 2;
        res = 0;
        while (unu <= step) {
            aux = res + step;
            if (aux.Pow(pow) <= N)
                res = aux;
            step /= 2;
        }
        if (res.Pow(pow) == N) {
            Write(res);
            cout << '\n' << pow;
            exit(0);
        }
    }
}