Cod sursa(job #2440141)

Utilizator Vlad_1Alexan Vlad Vlad_1 Data 17 iulie 2019 17:03:16
Problema Next Scor 30
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 5.1 kb
#include <iostream>
#include <fstream>
#include <cstring>
#include <iostream>

using namespace std;

const int MAX_DIGITS=1024;
const int BASE=10;

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

class HugeN
{
private:
    int x[MAX_DIGITS];
public:
    HugeN()
    {
        memset(x,0,sizeof(x));
        x[0]=1;
    }
    HugeN(int n)
    {
        memset(x,0,sizeof(x));
        do
        {
            ++x[0];
            x[x[0]]=n%10;
            n=n/10;
        }while(n>0);
    }
    HugeN(const HugeN & other)
    {
        memcpy(x,other.x,sizeof(other.x));
    }
    void print()
    {
        int i;
        for(i=x[0]; i>=1; i--)
            fout<<x[i];
        fout<<"\n";
    }
    int cmp(const HugeN & other)
    {
        if(x[0]<other.x[0]) return -1;
        else
            if(x[0]>other.x[0]) return 1;
            else
            {
                for(int i=x[0]; i>=1; i--)
                    if(x[i]<other.x[i]) return -1;
                    else
                        if(x[i]>other.x[i]) return 1;
                    return 0;
            }
    }
    HugeN(char s[1000001])
    {
        int n,i;
        memset(x,0,sizeof(x));
        n=strlen(s);
        x[0]=n;
        for(i=n-1; i>=0; i--)
            x[n-i]=s[i]-'0';
    }

    bool operator < (const HugeN & other);
    bool operator <= (const HugeN & other);
    bool operator > (const HugeN & other);
    bool operator >= (const HugeN & other);
    bool operator == (const HugeN & other);
    bool operator != (const HugeN & other);
    HugeN operator + (const HugeN & other);
    HugeN operator - (const HugeN & other);
    HugeN operator * (const HugeN & other);
    HugeN operator / (const HugeN & other);
    HugeN operator += (const HugeN & other);
    HugeN operator -= (const HugeN & other);
    HugeN operator *= (int k);
    HugeN operator /= (int k);
    HugeN operator += (long long k);
    long long operator % (long long k);
};

bool HugeN::operator < (const HugeN & other)
    {
        // x < other.x  -1
        if((*this).cmp(other)==-1) return 1;
        return 0;
    }
bool HugeN::operator <= (const HugeN & other)
{
    if((*this).cmp(other)<=0) return 1;
    return 0;
}

HugeN HugeN::operator + (const HugeN & other)
{
    //c=a+b     c=x+other.x
    int tr,i,k;
    HugeN c;
    c.x[0]=max(x[0],other.x[0]);
    tr=0;
    for(i=1; i<=c.x[0]; i++)
    {
        k=x[i]+other.x[i]+tr;
        c.x[i]=k%10;
        tr=k/10;
    }
    if(tr>0)
    {
        c.x[0]++;  k=c.x[0];
        c.x[k]=tr; }
    return c;
}

HugeN HugeN::operator +=(const HugeN & other)
{
    // a+=b    x+=other.x
    int i,t,k;
    x[0]=max(x[0],other.x[0]);
    for(t=0,i=1; i<=x[0]; i++)
    {
        k=x[i]+other.x[i]+t;
        x[i]=k%10;
        t=k/10;
    }
    if(t)
    {
        x[0]++; x[x[0]]=t;
    }
    return *this;
}

HugeN HugeN::operator *=(int k)
{
    int tr=0,i,aux;
    for(i=1; i<=x[0]; i++)
        x[i]=x[i]*k;
    for(i=1; i<=x[0]; i++)
    {
        aux=x[i]+tr;
        x[i]=aux%10;
        tr=aux/10;
    }
    aux=x[0];
    while(tr)
    {
        aux++;
        x[aux]=tr%10;
        tr=tr/10;
    }
    x[0]=aux;
    return *this;
}

HugeN HugeN::operator * (const HugeN & other)
{
    // c=x*other.x
    HugeN c;
    int i,tr,j,k;
    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[i];
    tr=0;
    for(i=1; i<=c.x[0]; i++)
    {
        k=c.x[i]+tr;
        c.x[i]=k%10;
        tr=k/10;
    }
    k=c.x[0];
    while(tr)
    {
        k++; c.x[k]=tr%10; tr=tr/10;
    }
    c.x[0]=k;
    return c;
}

HugeN HugeN::operator - (const HugeN & other)
{
    //c=a-b  c=x-other.x
    int impr,i,k;
    HugeN c;
    c.x[0]=max(x[0],other.x[0]);
    impr=0;
    for(i=1; i<=c.x[0]; i++)
    {
        k=x[i]-other.x[i]-impr;
        if(k<0)
        {
            c.x[i]=k+10;
            impr=1;
        }
        else{c.x[i]=k; impr=0;}
    }
    while(c.x[x[0]]==0 && c.x[0]>1)
        c.x[0]--;
    return c;
}

long long HugeN::operator % (long long k)
{
    int i;
    long long r;
    r=0;
    for(i=x[0]; i>=1; i--)
    {
        r=r*10+x[i];
        r=r%k;
    }
    return r;
}

HugeN HugeN::operator /=(int k)
{
    int i,r;
    r=0;
    for(i=x[0]; i>=1; i--)
    {
        r=r*10+x[i];
        x[i]=r/k;
        r=r%k;
    }
    while(x[0]>1 && x[x[0]]==0)
        x[0]--;
    return *this;
}

HugeN HugeN::operator +=(long long k)
{
    int i;
    long long tr=k;
    for(i=1; i<=x[0]; i++)
    {
        tr=x[i]+tr;
        x[i]=tr%10;
        tr=tr/10;
    }
    while(tr)
    {
        x[0]++;
        x[x[0]]=tr%10;
        tr=tr/10;
    }
    return *this;
}

int main()
{
    char s[MAX_DIGITS];
    fin.getline(s,MAX_DIGITS);
    HugeN n(s);
    long long d,r;
    fin>>d;
    r=n%d;
    if(r==0)
        n.print();
    else
        {
            d=d-r;
            n+=d;
            n.print();
        }
    return 0;
}