Cod sursa(job #1783448)

Utilizator CiurezAndreiCiurez Marius-Andrei CiurezAndrei Data 18 octombrie 2016 23:57:27
Problema Next Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.57 kb
#include <bits/stdc++.h>

using namespace std;

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

typedef long long ll;

const int dim = 1000000 / 2 + 5;

#define A (*this)
class Huge : protected vector < int > {

protected:

    static const ll base = 100, nBase = 2;

public:

    Huge() {
        this->resize(dim);
    }

    Huge(ll x) {
        this->resize(dim);
        A = x;
    }

    Huge(char* s) {
        this->resize(dim);
        A = s;
    }

    void operator = (ll x) {
        for (A[0] = 0; x; x /= base)
            A[++A[0]] = x % base;
    }

    void operator = (char *s) {
        A[0] = 0;
        for (ll i = strlen(s); i > 0; i -= nBase) {
            ++A[0];
            for (ll j = max(0ll, i - nBase); j < i; ++j)
                A[A[0]] = A[A[0]] * 10 + s[j] - '0';
        }
    }

    void print(void) {
        if (A[0] == 0) {
            fout << 0;
            return;
        }
        fout << A[A[0]];
        for (ll i = A[0] - 1; i > 0; --i) {
            ll p = base / 10;
            while (p > A[i] && p > 1) {
                fout << 0;
                p /= 10;
            }
            fout << A[i];
        }
    }

    bool operator < (const Huge &B) {
        if (A[0] != B[0])
            return A[0] < B[0];
        for (ll i = A[0]; i; --i) {
            if (A[i] < B[i]) return true;
            if (B[i] < A[i]) return false;
        }
        return false;
    }

    Huge operator + (const Huge &B) {
        ll i, t = 0;
        Huge C;
        for (i = 1; i <= A[0] || i <= B[0] || t; ++i, t /= base) {
            t += (i <= A[0] ? A[i] : 0);
            t += (i <= B[0] ? B[i] : 0);
            C[i] = t % base;
        }
        C[0] = i - 1;
        return C;
    }

    Huge operator - (const Huge &B) {
        Huge C = A;
        ll i, t = 0;
        for (i = 1; i <= A[0]; ++i) {
            C[i] -= (i <= B[0] ? B[i] : 0) + t;
            t = 0;
            if (C[i] < 0) C[i] += base, t = 1;
        }
        while (C[0] > 1 && C[C[0]] == 0)
            --C[0];
        return C;
    }

    Huge operator * (ll x) {
        Huge C = A;
        ll t = 0;
        for (ll i = 1; i <= C[0]; ++i) {
            t = 1LL * C[i] * x + t;
            C[i] = t % base;
            t /= base;
        }
        while (t) {
            C[++C[0]] = t % base;
            t /= base;
        }
        return C;
    }

    Huge operator * (const Huge &B) {
        Huge C;
        for (ll i = 1; i <= A[0]; ++i) {
            ll t = 0; ll j;
            for (j = 1; j <= B[0] || t; ++j, t /= base) {
                t += C[i + j - 1] + (j <= B[0] ? 1LL * A[i] * B[j] : 0);
                C[i + j - 1] = t % base;
            }
            if (i + j - 2 > C[0])
                C[0] = i + j - 2;
        }
        return C;
    }

    Huge operator / (ll x) {
        Huge C;
        C = A;
        ll R = 0;
        for (ll i = A[0]; i; --i) {
            R = R * base + A[i];
            C[i] = ll(R / x);
            R %= x;
        }
        while (C[0] > 1 && C[C[0]] == 0)
            --C[0];
        return C;
    }

    ll operator % (ll x) {
        ll R = 0;
        for (ll i = A[0]; i; --i) {
            R = R * base + A[i];
            R %= x;
        }
        return (ll)R;
    }

};

char s[1000005];
ll d;

int main() {

    fin >> s >> d;

    Huge n(s);

    if (n % d == 0) {
        n.print();
        return 0;
    }

    n = n / d;
    n = n * d;
    n = n + d;

    n.print();

    return 0;

}