Cod sursa(job #1236759)

Utilizator stefan.friptuPetru Stefan Friptu stefan.friptu Data 2 octombrie 2014 16:13:42
Problema Next Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.08 kb
#include <cstdio>
#include <cstring>

#define MAX_DIGITS 1<<10
#define BASE 10

using namespace std;

class huge {
    private:
        long x[MAX_DIGITS];
    public:
        huge(){
            memset(x,0,sizeof(x));
            x[0]=1;
        }

        huge(long n){
            memset(x,0,sizeof(x));
            x[0]=0;
            do{
                x[++x[0]]=n%10;
                n/=10;
            }while(n);
        }
        huge(const huge& other)
        {
            memcpy(x,other.x,sizeof(other.x));
        }

        huge(char *s)
        {
            memset(x,0,sizeof(x));
            long n,i;
            n=strlen(s);
            x[0]=n;
            for(i=n-1;i>=0;i--)
                x[n-i]=s[i]-'0';
        }

        void print()
        {
            for(long i=x[0];i>=1;i--)
                printf("%ld",x[i]);
            printf("\n");
        }

        long cmp (const huge& other)
        {
            if(x[0]<other.x[0])
                return -1;
            else if (x[0]>other.x[0]) return -1;
            for(long i=x[0];i>0;i--)
                if(x[i]<other.x[i]) return -1;
                else if(x[i]>other.x[i]) return 1;
            return 0;
        }


        long long operator % (long long k);
        huge& operator + (long long k);

};

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

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

long long n,d;

int main () {
    freopen("next.in","r",stdin);
    freopen("next.out","w",stdout);

    char s[1000005];
    scanf("%s",&s);
    huge n(s);
    long long d,r;
    scanf("%lld",&d);
    r=n%d;
    if(r)
    {

        r=d-r;
        n=n+r;
    }
    n.print();

    return 0;
}