Cod sursa(job #2499376)

Utilizator memecoinMeme Coin memecoin Data 25 noiembrie 2019 23:55:23
Problema Prod Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.87 kb
#include <fstream>
#include <string>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <math.h>
#include <set>
#include <map>

using namespace std;

#ifdef DEBUG
string name = "data";
#else
string name = "prod";
#endif

ifstream fin(name + ".in");
ofstream fout(name + ".out");

using namespace std;

struct BigInt {
    char digits[5005];
    int size = 0;
    
    void add(BigInt &other) {
        int c = 0;
        size = max(size, other.size);
        for (int i = 0; i < size; ++i) {
            
            int sum = digits[i] + other.digits[i] + c;
            
            digits[i] = sum % 10;
            c = sum / 10;
        }
        if (c > 0) {
            digits[size] = c;
            size++;
        }
    }
    
    void sub(BigInt &other) {
        int c = 0;
        
        int bound = min(size, other.size);
        
        for (int i = 0; i < bound; ++i) {
            
            int sum = digits[i] - (other.digits[i] + c);
            
            if (sum < 0) {
                sum += 10;
                c = 1;
            }
            else {
                c = 0;
            }
            
            digits[i] = sum % 10;
        }
        while (c > 0) {
            digits[bound]--;
            if (digits[bound] < 0) {
                digits[bound] = 9;
                c = 1;
            }
            else {
                c = 0;
            }
            bound++;
        }
    }
    
    void multiply(int s) {
        int c = 0;
        for (int i = 0; i < size; ++i) {
            
            int sum = digits[i] * s + c;
            
            digits[i] = sum % 10;
            c = sum / 10;
        }
        while (c > 0) {
            digits[size] = c % 10;
            c /= 10;
            size++;
        }
    }
    
    void multiply(BigInt& other) {
        
        BigInt result = BigInt(0);
        
        for (int i = 0; i < other.size; ++i) {
            int ndx = i;
            int c = 0;
            for (int j = 0; j < size; ++j) {
                int product = other.digits[i] * digits[j] + result.digits[ndx] + c;
                result.digits[ndx] = product % 10;
                result.size = max(result.size, ndx + 1);
                c = product / 10;
                ndx++;
            }
            if (c > 0) {
                result.digits[ndx] = c;
                result.size = max(result.size, ndx + 1);
            }
        }
        
        size = result.size;
        for (int i = 0; i < size; ++i) {
            digits[i] = result.digits[i];
        }
    }
    
    BigInt() {
        BigInt(0);
    }
    
    BigInt(int n) {
        memset(digits, 0, sizeof(digits));
        while (n > 0) {
            digits[size] = n % 10;
            n /= 10;
            size++;
        }
        size = max(size, 1);
    }
    
    BigInt(string s) {
        size = s.size();
        int j = 0;
        for (int i = size - 1; i >= 0; --i) {
            digits[j] = s[i] - '0';
            j++;
        }
    }
    
    void print() {
        for (int i = size - 1; i >= 0; --i) {
            fout << (int)digits[i];
        }
    }
};

int main() {
    
    int d[10];
    
    for (int i = 1; i <= 9; ++i) {
        fin >> d[i];
    }
    
    string a = "";
    string b = "";
    
    bool addToA = true;
    
    for (int i = 9; i >= 1; --i) {
        while (d[i] > 0) {
            if (addToA) {
                a = a + to_string(i);
                addToA = false;
            } else {
                b = b + to_string(i);
                addToA = true;
            }
            d[i]--;
        }
        
        if (b.size() < a.size()) {
            addToA = false;
        } else if (a.size() > b.size()) {
            addToA = true;
        } else {
            addToA = a[a.size() - 1] <= b[b.size() - 1];
        }
    }
    
    BigInt ba = BigInt(a);
    BigInt bb = BigInt(b);
    
    ba.multiply(bb);
    
    ba.print();
    
    return 0;
}