Cod sursa(job #2770040)

Utilizator TeodoraMaria123Serban Teodora Maria TeodoraMaria123 Data 18 august 2021 23:00:07
Problema Next Scor 40
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 5.23 kb
#include <fstream>
#include <cstring>
#include <algorithm>
using namespace std;
ifstream in("next.in");
ofstream out("next.out");
const int max_digits=1e6+10;
const int base=10;
const int nmax=1e6+10;
char s[nmax];
class huge
{
public:
    short x[max_digits];
public:
    huge()
    {
        memset(x,0,sizeof(x));
        x[0]=1;
    }
    huge(long long n)
    {
        memset(x,0,sizeof(x));
        do
        {
            x[++x[0]]=n%base;
            n/=base;
        }
        while(n);
    }
    huge(const huge &other)
    {
        memcpy(x,other.x,sizeof(other.x));
    }
    void print()
    {
        for(int i=x[0]; i>=1; i--)
            out<<x[i];
        out<<"\n";
    }
    int cmp(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;
    }
    void set_huge(int n)
    {
        x[0]=0;
        do
        {
            x[++x[0]]=n%base;
            n/=base;
        }
        while(n);
    }
    int get_digits()
    {
        return x[0];
    }
    huge operator +(const huge &other);
    huge operator +=(const huge &other);
    huge operator -(const huge &other);
    huge operator -=(const huge &other);
    huge operator *(int k);
    huge operator *(const huge &other);
    huge operator /(long long k);
    huge operator *=(int k);
    huge operator *=(const huge &other);
    huge operator /=(long long k);
    long long operator %(long long k);
    bool operator ==(const huge &other);
    bool operator !=(const huge &other);
    bool operator <=(const huge &other);
    bool operator <(const huge &other);
    bool operator >=(const huge &other);
    bool operator >(const huge &other);
};
bool huge :: operator ==(const huge &other)
{
    return ((*this).cmp(other)==0);
}
bool huge :: operator !=(const huge &other)
{
    return ((*this).cmp(other)!=0);
}
bool huge :: operator <=(const huge &other)
{
    return ((*this).cmp(other)!=1);
}
bool huge :: operator <(const huge &other)
{
    return ((*this).cmp(other)==-1);
}
bool huge :: operator >=(const huge &other)
{
    return ((*this).cmp(other)!=-1);
}
bool huge :: operator >(const huge &other)
{
    return ((*this).cmp(other)==1);
}
huge huge :: operator +(const huge &other)
{
    int i,tr,aux;
    huge c;
    c.x[0]=max(x[0],other.x[0]);
    for(tr=0,i=1; i<=c.x[0]; i++)
    {
        aux=x[i]+other.x[i]+tr;
        c.x[i]=aux%base;
        tr=aux/base;
    }
    if(tr)
        c.x[++c.x[0]]=tr;
    return c;
}
huge huge :: operator += (const huge &other)
{
    (*this)=(*this)+other;
    return (*this);
}
huge huge :: operator -(const huge &other)
{
    huge c;
    c.x[0]=max(x[0],c.x[0]);
    int i,impr,aux;
    for(impr=0,i=1; i<=c.x[0]; i++)
    {
        aux=x[i]-other.x[i]-impr;
        if(aux<0)
        {
            c.x[i]=aux+base;
            impr=1;
        }
        else
        {
            c.x[i]=aux;
            impr=0;
        }
    }
    while(c.x[0]>1  &&  c.x[c.x[0]]==0)
        c.x[0]--;
    return c;
}
huge huge :: operator -=(const huge &other)
{
    (*this)=(*this)-other;
    return (*this);
}
huge huge :: operator *(int k)
{
    huge c;
    int i,aux,tr;
    c.x[0]=x[0];
    for(i=1; i<=c.x[0]; i++)
        c.x[i]=x[i]*k;
    for(tr=0,i=1; i<=c.x[0]; i++)
    {
        aux=c.x[i]+tr;
        c.x[i]=aux%base;
        tr=aux/base;
    }
    while(tr)
    {
        c.x[++c.x[0]]=tr%base;
        tr/=base;
    }
    return c;
}
huge huge :: operator *=(int k)
{
    (*this)=(*this)*k;
    return (*this);
}
huge huge :: operator *(const huge &other)
{
    huge c;
    int i,j,tr,aux;
    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(tr=0,i=1; i<=c.x[0]; i++)
    {
        aux=c.x[i]+tr;
        c.x[i]=aux%base;
        tr=aux/base;
    }
    while(tr)
    {
        c.x[++c.x[0]]=tr%base;
        tr/=base;
    }
    return c;
}
huge huge :: operator *=(const huge &other)
{
    (*this)=(*this)*other;
    return (*this);
}
huge huge :: operator /(long long k)
{
    huge c;
    int i;
    long long r;
    c.x[0]=x[0];
    for(r=0,i=c.x[0]; i>=1; i--)
    {
        r=1ll*(r*base+x[i]);
        c.x[i]=r/k;
        r=1ll*(r%k);
    }
    while(c.x[0]>1  &&  c.x[c.x[0]]==0)
        c.x[0]--;
    return c;
}
huge huge :: operator /=(long long k)
{
    (*this)=(*this)/k;
    return (*this);
}
long long huge :: operator %(long long k)
{
    int i;
    long long r;
    for(r=0,i=x[0]; i>=1; i--)
    {
        r=1ll*(r*base+x[i]);
        r=1ll*(r%k);
    }
    return r;
}
int main()
{
    in.getline(s,nmax);
    huge nr;
    int n,i;
    long long d;
    in>>d;
    n=strlen(s);
    nr.x[0]=n;
    for(i=0; i<n; i++)
        nr.x[n-i]=s[i]-'0';
    if(nr%d==0)
        nr.print();
    else
    {
        huge aux(d-nr%d);
        nr=nr+aux;
        nr.print();
    }
    return 0;
}