Cod sursa(job #985160)

Utilizator dariusdariusMarian Darius dariusdarius Data 16 august 2013 16:30:24
Problema Numere 2 Scor 85
Compilator cpp Status done
Runda Arhiva de probleme Marime 4.28 kb
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
class H10
{
    private: int v[225];
    public:
        H10() {memset(v,0,sizeof(v));v[0]=1;}
        H10(int x)
        {
            if(x==-1)
            {
                v[0]=115;
                for(int i=1;i<=v[0];i++)
                    v[i]=9;
                return;
            }
            memset(v,0,sizeof(v));
            do
            {
                v[++v[0]]=x%10;
                x/=10;
            }while(x);
        }
        H10(int x,int y)
        {
            H10 X(x);
            for((*this)=H10(1);y;y>>=1)
            {
                if(y&1) (*this)=(*this)*X;
                if(y!=1)
                    X=X*X;
            }

        }
        H10(const H10 &x,int y)
        {
            if(x.v[0]*y>220)
            {
                *this=H10(-1);
                return;
            }
            H10 X=x;
            for((*this)=H10(1);y;y>>=1)
            {
                if(y&1) (*this)=(*this)*X;
                if(y>1)
                    X=X*X;
            }
        }
        H10 operator*(const H10 &other)
        {
            H10 ans;
            int T=0;
            ans.v[0]=v[0]+other.v[0]-1;
            for(int i=1;i<=v[0];i++)
                for(int j=1;j<=other.v[0];j++)
                    ans.v[i+j-1]+=v[i]*other.v[j];
            for(int i=1;i<=ans.v[0];i++)
            {
                ans.v[i]+=T;
                T=ans.v[i]/10;
                ans.v[i]%=10;
            }
            if(T) ans.v[++ans.v[0]]=T;
            return ans;
        }
        void read()
        {
            char s[500];
            gets(s);v[0]=strlen(s);
            for(int i=0;s[i];i++)
                v[v[0]-i]=s[i]-'0';
        }
        inline bool operator<=(const H10 &other) const
        {
            if(v[0]<other.v[0]) return true;
            if(v[0]>other.v[0]) return false;
            for(int i=v[0];i;i--)
                if(v[i]>other.v[i])
                    return false;
                else if(v[i]<other.v[i])
                    return true;
            return true;
        }
        void operator+=(const H10 &other)
        {
            v[0]=max(v[0],other.v[0]);
            int t=0;
            for(int i=1;i<=v[0];i++)
            {
                v[i]=v[i]+other.v[i]+t;
                t=v[i]/10;
                v[i]%=10;
            }
            if(t) v[++v[0]]=t;
        }
        H10 operator+(const H10 &other)
        {
            H10 aux=(*this);
            aux+=other;
            return aux;
        }
        void operator/=(const int &x)
        {
            int R=0;
            for(int i=v[0];i;i--)
            {
                R=R*10+v[i];
                v[i]=R/x;
                R%=x;
            }
            while(!v[v[0]] && v[0]>1) v[0]--;
        }
        void operator*=(const int &x)
        {
            int t=0;
            for(int i=1;i<=v[0];i++)
            {
                v[i]=v[i]*x+t;
                t=v[i]/10;
                v[i]%=10;
            }
            while(t)
            {
                v[++v[0]]=t%10;
                t/=10;
            }
        }
        H10 solve(const int &ex)
        {
            ///cautam cel mai mare A, pt care A^ex<=*this
            H10 ans,pas;
            for(pas=H10(1);pas<=*this;pas*=2);
            for(pas/=2;pas!=H10();pas/=2)
                if(H10(ans+pas,ex)<=*this)
                    ans+=pas;
            if(H10(ans,ex)!=*this)
                return H10();
            return ans;
        }
        inline bool operator!=(const H10 &other)
        {
            if(v[0]!=other.v[0]) return true;
            return memcmp(v,other.v,(v[0]+1)*sizeof(int));
        }
        void print()
        {
            for(int i=v[0];i;i--)
                printf("%d",v[i]);
            printf("\n");
        }
} P;
int main()
{
    freopen("numere2.in","r",stdin);
    freopen("numere2.out","w",stdout);
    P.read();
    int lg;
    for(lg=0;H10(2,lg)<=P;lg++);lg--;
    for(int i=33;i>=1;i--)
        if(P.solve(i)!=H10())
        {
            P.solve(i).print();
            printf("%d\n",i);
            return 0;
        }
    return 0;
}