Cod sursa(job #2769982)

Utilizator maria_neagoieMaria Neagoie maria_neagoie Data 18 august 2021 17:18:28
Problema Next Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.41 kb
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAX_DIGITS=1000005,BASE=10;
char s[MAX_DIGITS];
class HUGE
{
    private: int x[MAX_DIGITS];
    public:
        //Constructori
        HUGE()
        {
            memset(x,0,sizeof(x));
            x[0]=1;
        }
        HUGE(long long n)
        {
            memset(x,0,sizeof(x));
            x[0]=0;
            do
            {
                x[++x[0]]=n%10;
                n=n/10;
            }
            while(n>0);
        }
        HUGE(char s[MAX_DIGITS],int l)
        {
            memset(x,0,sizeof(x));
            int i;
            for(i=l-1;i>=0;i--)
                x[i+1]=s[i]-'0';
            x[0]=l;
        }
        void print()
        {
            int i;
            for(i=x[0];i>=1;i--)
                printf("%d",x[i]);
            printf("\n");
        }
        //Supraincarcarea operatorilor
        HUGE operator+=(const HUGE &other);
        HUGE operator-=(const HUGE &other);
        HUGE operator%(long long k);
};
HUGE HUGE::operator+=(const HUGE &other)
{
    //x=x+other.x
    int i,tr,aux;
    x[0]=max(x[0],other.x[0]);
    for(i=1,tr=0;i<=x[0];i++)
    {
        aux=x[i]+other.x[i]+tr;
        x[i]=aux%BASE;
        tr=aux/BASE;
    }
    if(tr)
    {
        x[0]++;
        x[x[0]]=tr;
    }
    return (*this);
}
HUGE HUGE::operator-=(const HUGE &other)
{
    //x=x-other.x
    int i,impr,aux;
    impr=0;
    for(i=1;i<=x[0];i++)
    {
        aux=x[i]-other.x[i]-impr;
        if(aux<0)
        {
            x[i]=aux+BASE;
            impr=1;
        }
        else
        {
            x[i]=aux;
            impr=0;
        }
    }
    while(x[x[0]]==0 && x[0]>1)
        x[0]--;
    return (*this);
}
HUGE HUGE::operator%(long long k)
{
    //c.x=x%k
    long long r=0;
    int i;
    for(i=x[0];i>=1;i--)
        r=(r*BASE+x[i])%k;
    HUGE c;
    memset(c.x,0,sizeof(c.x));
    c.x[0]=0;
    do
    {
        ++c.x[0];
        c.x[c.x[0]]=r%10;
        r=r/10;
    }
    while(r>0);
    return c;
}
int main()
{
    freopen("next.in","r",stdin);
    freopen("next.out","w",stdout);
    long long d;
    int l;
    fgets(s,MAX_DIGITS,stdin);
    l=strlen(s)-1;
    s[l]=NULL;
    scanf("%lld",&d);
    HUGE N(s,l),D(d),REST;
    REST=N%d;
    N+=D;
    N-=REST;
    N.print();
    return 0;
}