Cod sursa(job #1236712)

Utilizator Sanduleac_VladSanduleac Vllad Alexandru Sanduleac_Vlad Data 2 octombrie 2014 15:32:56
Problema Next Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.92 kb
#include <cstdio>
#include <cstring>
using namespace std;
#define MAX_DIGITS (1 << 6)
#define max(x,y) (x > y ? x : y)

class HugeN {
private:
    int x[MAX_DIGITS];
public:
    HugeN() {
        memset(x, 0, sizeof(x));
        x[0] = 1;
    }
    HugeN(int n) {
        memset(x, 0, sizeof(x));
        x[0] = 0;
        do {
            x[++x[0]] = n % 10;
            n = n / 10;
        } while(n);
    }
    HugeN(const HugeN &other) {
        memcpy(x, other.x, sizeof(other.x));
    }
    void print() {
        for(int i = x[0]; i >= 1; i--)
            printf("%d", x[i]);
        printf("\n");
    }
    HugeN operator+(HugeN other) {
        long i, t;
        HugeN r(0);
        r.x[0] = x[0] > other.x[0] ? x[0] : other.x[0];
        for(t = 0, i = 1; i <= r.x[0]; i++) {
            t = x[i] + other.x[i] + t;
            r.x[i] = t % 10;
            t /= 10;
        }
        if(t)
            r.x[0]++, r.x[r.x[0]] = t;
    }
    HugeN operator+=(const HugeN &other) {
        int i, t;
        x[0] = max(x[0], other.x[0]);
        for(t = 0, i = 1; i <= x[0]; i++) {
            t = x[i] + other.x[i] + t;
            x[i] = t % 10;
            t /= 10;
        }
        if(t)
            x[0]++, x[x[0]] = t;
    }
    int operator%(int other) {
        int r = 0, i;
        for(i = x[0]; i > 0; i--) {
            r = r * 10 + x[i];
            r %= other;
        }
        return r ;
    }
    void read() {
        char *s;
        s = new char[MAX_DIGITS + 1];
        gets(s);
        long i;
        i = strlen(s);
        x[0] = i;
        while(i > 0) {
            x[x[0] - i + 1] = s[i - 1] - '0';
            i--;
        }
    }
};

HugeN N, rez;
long D;

int main() {
    int i, j;
    freopen("next.in", "r", stdin);
    freopen("next.out", "w", stdout);
    N.read();
    scanf("%ld", &D);
    N += (D - N % D);
    N.print();
    return 0;
}