Cod sursa(job #2610624)

Utilizator MocalinnoMoca Andrei Catalin Mocalinno Data 5 mai 2020 11:22:16
Problema Numere 2 Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.99 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;
    }
    int operator[](const int i) const {
        return A[i];
    }
    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 1;
            if (B[i] < A[i])
                return 0;
        }
        return 0;
    }
    bool operator<=(const Int &B) const {
        for (int i = max(Size, B.size()) - 1; i >= 0; i--)
        {
            if (A[i] < B[i])
                return 1;
            if (B[i] < A[i])
                return 0;
        }
        return 1;
    }
    bool operator==(const Int &B) const {
        for (int i = max(Size, B.size()) - 1; i >= 0; i--)
        {
            if (A[i] != B[i])
                return 0;
        }
        return 1;
    }
    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;
    }
    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);
    }
    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;
    }
    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;
    }
    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;
    }
private:
    static const int MaxSize = 500, Base = 1000;
    int Size;
    int A[MaxSize];
};
Int Pow(Int x, int y)
{
    Int ret = 1;
    for (; y; y >>= 1)
    {
        if (y & 1) ret *= x;
        x *= x;
    }
    return ret;
}
void Write(const Int &A)
{
    printf("%d", A[A.size() - 1]);
    for (int i = A.size() - 2; i >= 0; i--) printf("%03d", A[i]);
}
int main()
{
    freopen("numere2.in", "r", stdin);
    freopen("numere2.out", "w", stdout);
    char Number[105];
    fgets(Number, 105, stdin);
    Int N = Number;
    for (int pow = 335; pow; pow--)
    {
        Int step = 1;
        while(Pow(step, pow) < N)
            step *= 2;
        Int Sol;
        while (true)
        {
            if (Pow(Sol + step, pow) <= N)
                Sol += step;

            if (step == 1) break;
            step /= 2;
        }
        if (Pow(Sol, pow) == N)
        {
            Write(Sol);
            printf("\n%d\n", pow);
            return 0;
        }
    }
}