Cod sursa(job #1296947)

Utilizator victormarinMarin Victor victormarin Data 21 decembrie 2014 16:31:47
Problema Next Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 7.23 kb
#include <cstdio>
#include <cstring>
#define filein "next.in"
#define fileout "next.out"
#define NMAX 1000002
#define Base 10
using namespace std;

class Huge
{
    private:
        char x[NMAX];
    public:
        /// Constructors
        Huge ()
        {
            memset(x,0,sizeof(x));
            x[0]=1;
        }
        Huge (int k)
        {
            memset(x,0,sizeof(x));
            do
            {
                x[++x[0]]=k%Base;
                k/=Base;
            } while(k);
        }
        Huge (Huge &other)
        {
            memcpy(x,other.x,sizeof(other.x));
        }
        Huge (char str[])
        {
            memset(x,0,sizeof(x));
            x[0]=strlen(str);
            for (int i=1; i<=x[0]; i++)
                x[i]=str[x[0]-i]-'0';
        }

        /// Show it to your friends
        void Print()
        {
            for (int i=x[0]; i>=1; i--)
                printf("%d",x[i]);
        }

        /// A little help for overloading logical operators functions
        int Compare (const Huge &other)
        {
            if (x[0]>other.x[0]) return 1;
            if (x[0]<other.x[0]) return -1;
            for (int i=x[0]; i>=1; i--)
            {
                if (x[i]>other.x[i]) return 1;
                if (x[i]<other.x[i]) return -1;
            }
            return 0;
        }

        /// Overloading logical operators
        bool operator < (const Huge &other);
        bool operator < (int k);
        bool operator <= (const Huge &other);
        bool operator <= (int k);
        bool operator > (const Huge &other);
        bool operator > (int k);
        bool operator >= (const Huge &other);
        bool operator >= (int k);
        bool operator == (const Huge &other);
        bool operator == (int k);
        bool operator != (const Huge &other);
        bool operator != (int k);

        /// Overloading arithmetical operators
        Huge operator + (const Huge &other);
        Huge operator + (int k);
        Huge operator += (const Huge &other);
        Huge operator += (int k);
        Huge operator - (const Huge &other);
        Huge operator - (int k);
        Huge operator -= (const Huge &other);
        Huge operator -= (int k);
        Huge operator * (const Huge &other);
        Huge operator * (int k);
        Huge operator *= (const Huge &other);
        Huge operator *= (int k);
        Huge operator / (const Huge &other);
        Huge operator / (int k);
        Huge operator /= (const Huge &other);
        Huge operator /= (int k);
        Huge operator % (const Huge &other);
        Huge operator % (int k);
        Huge operator %= (const Huge &other);
        Huge operator %= (int k);
};

int main()
{
    freopen(filein,"r",stdin);
    freopen(fileout,"w",stdout);
    int aux;
    char str[NMAX];
    scanf("%s",str);
    scanf("%d",&aux);
    Huge N(str),D(aux);
    Huge M;
    M=N+D-N%D;
    M.Print();
    return 0;
}

bool Huge :: operator < (const Huge &other)
{
    if ((*this).Compare(other)==-1) return true;
    return false;
}

bool Huge :: operator < (int k)
{
    Huge n(k);
    return (*this)<n;
}

bool Huge :: operator <= (const Huge &other)
{
    if ((*this).Compare(other)==1) return false;
    return true;
}

bool Huge :: operator <= (int k)
{
    Huge n(k);
    return (*this)<=n;
}

bool Huge :: operator > (const Huge &other)
{
    if ((*this).Compare(other)==1) return true;
    return false;
}

bool Huge :: operator > (int k)
{
    Huge n(k);
    return (*this)>n;
}

bool Huge :: operator >= (const Huge &other)
{
    if ((*this).Compare(other)==-1) return false;
    return true;
}

bool Huge :: operator >= (int k)
{
    Huge n(k);
    return (*this)>=n;
}

bool Huge :: operator == (const Huge &other)
{
    if ((*this).Compare(other)==0) return true;
    return false;
}

bool Huge :: operator == (int k)
{
    Huge n(k);
    return (*this)==n;
}

bool Huge :: operator != (const Huge &other)
{
    if ((*this).Compare(other)==0) return false;
    return true;
}

bool Huge :: operator != (int k)
{
    Huge n(k);
    return (*this)!=n;
}

Huge Huge :: operator + (const Huge &other)
{
    int i,t;
    Huge c;
    c.x[0]=x[0]>other.x[0]?x[0]:other.x[0];
    for (i=1, t=0; i<=c.x[0]; i++)
    {
        c.x[i]=x[i]+other.x[i]+t;
        t=0;
        if (c.x[i]>=Base)
        {
            t=1;
            c.x[i]%=Base;
        }
    }
    if (t)
        c.x[++c.x[0]]=t;
    return c;
}

Huge Huge :: operator + (int k)
{
    Huge n(k);
    Huge a;
    a=(*this)+n;
    return a;
}

Huge Huge :: operator += (const Huge &other)
{
    (*this)=(*this)+other;
    return *this;
}

Huge Huge :: operator += (int k)
{
    Huge n(k);
    (*this)=(*this)+n;
    return (*this);
}

Huge Huge :: operator - (const Huge &other)
{
    int i,t;
    Huge c;
    c.x[0]=x[0];
    for (t=0, i=1; i<=c.x[0]; i++)
    {
        c.x[i]=x[i]-other.x[i]-t;
        if (c.x[i]<0)
        {
            c.x[i]+=Base;
            t=1;
        }
        else
            t=0;
    }
    for (i=c.x[0]; c.x[i]==0; i--)
        c.x[0]--;
    return c;
}

Huge Huge :: operator - (int k)
{
    Huge n(k);
    Huge a;
    a=(*this)-n;
    return a;
}

Huge Huge :: operator -= (const Huge &other)
{
    (*this)=(*this)-other;
    return *this;
}

Huge Huge :: operator -= (int k)
{
    Huge n(k);
    (*this)=(*this)-n;
    return *this;
}

Huge Huge :: operator * (const Huge &other)
{
    Huge c;
    int i,j;
    int t=0;
    c.x[0]=x[0]+other.x[0]-1;
    for (i=1; i<=x[0]; i++)
        for (j=1; j<=other.x[0]; j++)
            c.x[i+j-1]+=x[i]*other.x[j];
    for (i=1; i<=c.x[0]; i++)
    {
        t=(c.x[i]+=t)/Base;
        c.x[i]%=Base;
    }
    if (t) c.x[++c.x[0]]=t;
    return c;
}

Huge Huge :: operator * (int k)
{
    Huge n(k),a;
    a=(*this)*n;
    return a;
}

Huge Huge :: operator *= (const Huge &other)
{
    (*this)=(*this)*other;
    return *this;
}

Huge Huge :: operator *= (int k)
{
    Huge n(k);
    (*this)=(*this)*n;
    return (*this);
}

Huge Huge :: operator / (const Huge &other)
{
    int i;
    Huge c,r;
    c.x[0]=x[0];
    r.x[0]=0;
    for (i=x[0]; i; i--)
    {
        r=r*Base+x[i];
        c.x[i]=0;
        while (r>=other)
        {
            c.x[i]++;
            r-=other;
        }
    }
    while (!c.x[c.x[0]] && c.x[0]>1) c.x[0]--;
    return c;
}

Huge Huge :: operator / (int k)
{
    Huge n(k);
    Huge a;
    a=(*this)/n;
    return a;
}

Huge Huge :: operator /= (const Huge &other)
{
    (*this)=(*this)/other;
    return *this;
}

Huge Huge :: operator /= (int k)
{
    Huge n(k);
    (*this)=(*this)/n;
    return *this;
}

Huge Huge :: operator % (const Huge &other)
{
    Huge c,r;
    c=(*this)/other;
    r=(*this)-(c*other);
    return r;
}

Huge Huge :: operator % (int k)
{
    Huge a;
    Huge n(k);
    a=(*this)%n;
    return a;
}

Huge Huge :: operator %= (const Huge &other)
{
    (*this)=(*this)%other;
    return (*this);
}

Huge Huge :: operator %= (int k)
{
    Huge n(k);
    (*this)=(*this)%n;
    return (*this);
}