Cod sursa(job #1236742)

Utilizator HotSteelBeteag Ion Andrei HotSteel Data 2 octombrie 2014 15:59:21
Problema Next Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 4.34 kb
#include <cstdio>
#include <cstring>

using namespace std;

#define MAX_DIGITS 1<<10
#define BASE 10

class HugeN
{
    private:
        int x[MAX_DIGITS];

        int cmp(const HugeN& other)
        {
            if(x[0] < other.x[0])
                return -1;
            else
                if(x[0] > other.x[0])
                    return 1;

            for(int 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;
        }

    public:
    HugeN()
    {
        memset(x, 0, sizeof(x));
        x[0] = 1;
    }
    HugeN(char * otherx)
    {
        int size = strlen(otherx);
        x[0] = size;
        for(int i = 1 ; i <= size ; ++i)
            x[x[0] - i + 1] = otherx[i-1] - '0';
    }
    HugeN(int N)
    {
        memset(x, 0, sizeof(x));
        do
        {
            x[++x[0]] = N % BASE;
            N /= BASE;
        }while(N);
    }
    HugeN(const HugeN& other)
    {
        memcpy(x,other.x,sizeof(x));
    }

    inline bool operator < (const HugeN& other)
    {
        if(cmp(other) == -1)
            return true;

        return false;
    }
    inline bool operator <= (const HugeN& other)
    {
        if(cmp(other) <= 0)
            return true;

        return false;
    }
    inline bool operator > (const HugeN& other)
    {
        if(cmp(other) == 1)
            return true;

        return false;
    }
    inline bool operator >= (const HugeN & other)
    {
        if(cmp(other) >= 0)
            return true;

        return false;
    }
    inline bool operator == (const HugeN& other)
    {
        if(cmp(other) == 0)
            return true;

        return false;
    }
    inline bool operator != (const HugeN& other)
    {
        if(cmp(other) != 0)
            return true;

        return false;
    }
    HugeN operator + (const HugeN& other)
    {
        int i,t;
        HugeN c;
        c.x[0] = x[0] > other.x[0] ? x[0] : other.x[0];

        for(t = 0, i = 1 ; i<= c.x[0] ; ++i)
        {
            t = x[i] + other.x[i] + t;
            c.x[i] = t % BASE;
            t /= BASE;
        }

        if(t)
        {
            c.x[0]++;
            c.x[c.x[0]] = t;
        }

        return c;
    }
    HugeN& operator += (const HugeN& other)
    {
        int i,t;
        x[0] = x[0] > other.x[0] ? x[0] : other.x[0];

        for(t = 0, i = 1 ; i <= x[0] ; ++i)
        {
            t = x[i] + other.x[i] + t;
            x[i] = t % BASE;
            t /= BASE;
        }

        if(t)
        {
            x[0]++;
            x[x[0]] = t;
        }

        return *this;
    }
    HugeN& operator /= (int k)
    {
        int r = 0, i;

        for(i = x[0]; i > 0 ; --i)
        {

            r = r * BASE + x[i];
            x[i] = r / k;
            r %= k;
        }

        while(x[0] > 1 && !x[x[0]])
            --x[0];

        return *this;
    }
    long long int operator % (long long int k)
    {
        long long int r = 0;
        int i;

        for(i = x[0]; i > 0; --i)
        {
            r = r * BASE + x[i];
            r %= k;
        }

        return r;
    }
    HugeN operator* (const HugeN& other)
    {

        HugeN c;

        c.x[0] = x[0] + other.x[0] - 1;

        int i, j, t;

        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(t = 0, i = 1; i <= c.x[0]; ++i)
        {
            t += c.x[i];
            c.x[i] = t % BASE;
            t /= BASE;
        }

        int l = c.x[0];
        while(t)
        {
            l++;
            c.x[l] = t % BASE;
            t /= BASE;
        }

        c.x[0] = l;

        return c;
    }

    void print ()
    {
        for(int i = x[0] ; i > 0 ; --i)
            printf("%d",x[i]);
    }
};

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

    char tmp[1000005];

    gets(tmp);

    long long int d,r;

    scanf("%lld",&d);

    HugeN n(tmp);

    r = n % d;
    if(r)
    {
        r = d - r;
        n += r;
    }

    n.print();

    return 0;
}