• + 0 comments

    C++ Solution:

    void coutVector(vector<int> *a) {
        int len = (*a).size();
        for(int i = 0; i < len; i++) {
            cout << (*a)[i];
        }
    }
    
    /**
     * This function bring every character of
     * string `a` to vector `b` under int type
     * Input: a is a string with number characters
     * Input: b is a blank vector
     * Effect: b is changed with full of number got from
     *  string a's number characters, but in reverse way
     */
    void translateStringToVector(string a, vector<int> *b) {
        for(int i = a.size() - 1; i >= 0; i--) {
            (*b).push_back(a[i] - 48);
        }
    }
    
    vector<int> surplusVaN(vector<int> a, int n) {
        vector<int> result{};
        vector<int> b{};
        translateStringToVector(to_string(n), &b);
          
        vector<int> *maxV {};
        if(a.size() >= b.size()) {
            maxV = &a;
        } else {
            maxV = &b;
        }
        int len = (*maxV).size();
        int minSize = min(a.size(), b.size());
        int remain = 0;
        for(int i = 0; i < len; i++) {
            if(i >= minSize) {
                int sum = (*maxV)[i] + 0 + remain;
                remain = sum/10;
                result.push_back((sum - remain*10));
                continue;
            }
            int sum = a[i] + b[i] + remain;
            remain = sum/10;
            result.push_back(sum - remain*10);
        }
        if(remain > 0) {
            result.push_back(remain);
        }
        return result;
    }
    
    /**
     * This function get
     * Input: `result` destination, vector `a` and
     * number `b` for calculation. Vector `a` is reverse
     * It returns
     * Output: `result` have new value that each 
     * number in vector is changed
     */
    void multiplyVaN(vector<int> *result, vector<int> a, int b) {
        int len = a.size();
        vector<int> remain {0};
        for(int i = 0; i < len; i++ ) {
            vector<int> product = surplusVaN(remain,a[i] * b);
            
            remain.clear();
            if(product.size() > 1) {
                remain.insert(remain.begin(), product.begin() + 1, product.end());// Bring `remain` to next index
            }
            (*result)[i] = product[0];   // Get the last position number of product;
        }
        if(remain.size() == 1 && remain[0] != 0) {
            (*result).push_back(remain[0]);
        }
        if(remain.size() > 1) {
            (*result).insert((*result).end(), remain.begin(), remain.end());
        }
    }
    
    void extraLongFactorials(int n) {
        vector<int> result{1};
    
        for(int i = 1; i <= n; i++) {
            multiplyVaN(&result, result, i);
        }
        reverse(result.begin(), result.end());
        coutVector(&result);
    }