Cod sursa(job #420755)

Utilizator bogdan2412Bogdan-Cristian Tataroiu bogdan2412 Data 20 martie 2010 14:42:19
Problema Prod Scor Ascuns
Compilator cpp Status done
Runda Marime 3.14 kb
#include <cstdio>
#include <cassert>
#include <cstring>

using namespace std;

#define MAXC 256
#define BASE 10000
#define OUTPUT_FORMAT "%04d"

class Huge {
protected:
    int a[MAXC];

    int& operator [] (int poz) {
        return a[poz];
    }
    int operator [] (int poz) const {
        return a[poz];
    }
public:
    Huge() {
        memset(a, 0, sizeof(a));
    }

    Huge(int N) {
        memset(a, 0, sizeof(a));
        if (N == 0) {
            a[0] = 1;
        }

        for (; N; N /= BASE) {
            a[++a[0]] = N % BASE;
        }
    }

    Huge operator + (const Huge &B) const {
        int i, t = 0;
        Huge C;

        for (i = 1; i <= a[0] || i <= B[0] || t; i++, t /= BASE)
            C[i] = (t += a[i] + B[i]) % BASE;
        C[0] = i - 1;
        return C;
    }

    Huge& operator += (const Huge &B) {
        *this = *this + B;
        return *this;
    }

    Huge& operator -= (const Huge &B) {
        int i, t = 0;

        for (i = 1; i <= a[0]; i++) {
            a[i] -= B[i] + t;
            t = (a[i] < 0);
            if (t)
                a[i] += BASE;
        }
        for (; a[0] > 1 && !a[a[0]]; a[0]--);

        return *this;
    }

    Huge operator * (const Huge &B) const {
        int i, j, t = 0;
        Huge C;

        for (i = 1; i <= a[0] || t; i++) {
            for (j = 1, t = 0; j <= B[0] || t; j++, t /= BASE) {
                C[i + j - 1] = (t += C[i + j - 1] + a[i] * B[j]) % BASE;
            }
            if (i + j - 2 > C[0]) {
                C[0] = i + j - 2;
            }
        }
        return C;
    }

    int operator < (const Huge &B) const {
        if (a[0] < B[0]) {
            return 1;
        }
        if (a[0] > B[0]) {
            return 0;
        }
        for (int i = a[0]; i > 0; i--) {
            if (a[i] != B[i]) {
                return a[i] < B[i];
            }
        }
        return 0;
    }

    int operator > (const Huge &B) const {
        return B < *this;
    }

    Huge& read() {
        char tmp[128];
        scanf(" %s", tmp);

        memset(a, 0, sizeof(a));
        int pow = 1, val = 0, p;
        for (p = 0; tmp[p]; p++);
        for (p--; p >= 0; p--, pow *= 10) {
            if (pow == BASE) {
                a[++a[0]] = val;
                val = 0; pow = 1;
            }
            val = val + pow * (tmp[p] - '0');
        }
        a[++a[0]] = val;
        return *this;
    }

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

int cnt[10];

int main() {
    freopen("prod.in", "rt", stdin);
#ifndef DEBUG
    freopen("prod.out", "wt", stdout);
#endif

    int SUM = 0;
    for (int i = 1; i <= 9; i++) {
        assert(scanf("%d", cnt + i) == 1);
        assert(0 <= cnt[i]);
        SUM += cnt[i];
    }
    assert(SUM <= 1000);

    Huge A = 0, B = 0; int type = 0;
    for (int i = 9; i >= 0; i--) {
        for (; cnt[i]; cnt[i] -= 1) {
            if (type == 0) {
                A = A * 10 + i;
            } else {
                B = B * 10 + i;
            }
            type ^= 1;
        }
    }

    (A * B).print(); printf("\n");

    return 0;
}