Cod sursa(job #2937139)

Utilizator toma_ariciuAriciu Toma toma_ariciu Data 9 noiembrie 2022 23:21:30
Problema Numere 2 Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 4.23 kb
#include <fstream>
#include <cstring>
#include <vector>

using namespace std;

ifstream fin("numere2.in");
ofstream fout("numere2.out");

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 lgput(Int x, int p)
{
    if(p == 1)
        return x;
    if(p % 2 == 0)
    {
        Int k = lgput(x, p / 2);
        k *= k;
        return k;
    }
    if(p % 2 == 1)
    {
        Int k = lgput(x, p - 1);
        k *= x;
        return k;
    }
}

int main()
{
    char str[105];
    fgets(str, 105, stdin);
    Int n = str;
    for(int i = 335; i; i--)
    {
        Int aux = 0, pow2 = 1, zero = 0;
        while(lgput(pow2, i) < n)
            pow2 *= 2;
        for(Int j = pow2; !(j == zero); j /= 2)
            if(lgput(aux + j, i) <= n)
                aux += j;
        if(lgput(aux, i) == n)
        {
            fout << aux[aux.size() - 1];
            for(int i = aux.size() - 2; i >= 0; i--)
            {
                if(aux[i] < 1000)
                    fout << 0;
                if(aux[i] < 100)
                    fout << 0;
                if(aux[i] < 10)
                    fout << 0;
            }
            fout << '\n' << i;
            return 0;
        }
    }
    return 0;
}