Cod sursa(job #1427656)

Utilizator paunmatei7FMI Paun Matei paunmatei7 Data 2 mai 2015 19:17:57
Problema Prod Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.24 kb
#include <cstdio>
#include <algorithm>

#define NMAX 10007

using namespace std;

int Ap[17], a[NMAX], b[NMAX], c[NMAX];

inline int comp(){
    if (a[0] > b[0])
        return 1;
    else
        if (a[0] < b[0])
            return -1;
    for(int i = 1; i <= a[0]; ++i)
        if (a[i] > b[i])
            return 1;
        else
            if (a[i] < b[i])
                return -1;
    return 0;
}

void Prod(){
    int t = 0;
    c[0] = a[0] + b[0] - 1;
    for(int i = 1; i <= a[0]; ++i)
        for(int j = 1; j <= b[0]; ++j)
            c[i + j - 1] += a[i] * b[j];
    for(int i = 1; i <= c[0]; ++i){
        t = (c[i] += t) / 10;
        c[i] %= 10;
    }
    if (t)
        c[++c[0]] = t;
}

int main(){
    freopen("prod.in", "r", stdin);
    freopen("prod.out", "w", stdout);
    for(int i = 1; i <= 9; ++i)
        scanf("%d", &Ap[i]);
    for(int i = 9; i >= 1; i--){
        while (Ap[i]){
            if (comp() < 0)
                a[++a[0]] = i;
            else
                b[++b[0]] = i;
            Ap[i]--;
        }
    }
    reverse(a + 1, a + 1 + a[0]);
    reverse(b + 1, b + 1 + b[0]);
    Prod();
    for (int i = c[0]; i >= 1; --i)
        printf("%d", c[i]);
    return 0;
}