老老实实写高精,直接重载vector容器,求大佬轻喷
查看原帖
老老实实写高精,直接重载vector容器,求大佬轻喷
133246
zhaite楼主2020/11/16 23:08
#include <bits/stdc++.h>
using namespace std;

template <typename _Tp, typename = typename std::enable_if_t<std::is_integral_v<_Tp>>>
_Tp getlength() { return static_cast<_Tp>(floor(log10(sqrt(pow(2, sizeof(_Tp) * 8))))); }

template <typename _Tp, typename = typename std::enable_if_t<std::is_integral_v<_Tp>>>
_Tp getmod() { return static_cast<_Tp>(pow(10, getlength<_Tp>())); }

template <typename _Tp, typename _Alloc, typename = typename std::enable_if_t<std::is_integral_v<_Tp>>>
vector<_Tp, _Alloc> operator+(const vector<_Tp, _Alloc> &__x, const vector<_Tp, _Alloc> &__y)
{
    _Tp mod = getmod<_Tp>();
    vector<_Tp, _Alloc> __result;
    _Tp __tmp = 0;
    size_t __i = 0, __j = 0;
    while (__i < __x.size() && __j < __y.size())
    {
        __tmp += __x.at(__i++) + __y.at(__j++);
        __result.push_back(__tmp % mod);
        __tmp /= mod;
    }
    auto f = [&__tmp, &__result, &mod](size_t __i, const vector<_Tp, _Alloc> &__x) {
        while (__i < __x.size())
        {
            __tmp += __x.at(__i++);
            __result.push_back(__tmp % mod);
            __tmp /= mod;
        }
    };
    f(__i, __x);
    f(__j, __y);
    if (__tmp != 0)
        __result.push_back(__tmp);
    return __result;
}

template <typename _Tp, typename _Tp2, typename _Alloc,
          typename = typename std::enable_if_t<std::is_integral_v<_Tp>>,
          typename = typename std::enable_if_t<std::is_integral_v<_Tp2>>>
vector<_Tp, _Alloc> operator+(const vector<_Tp, _Alloc> &__x, const _Tp2 __y)
{
    vector<_Tp, _Alloc> __z;
    size_t mod = static_cast<size_t>(getmod<_Tp>()), __y2 = static_cast<size_t>(__y);
    while (__y2)
    {
        __z.push_back(static_cast<_Tp>(__y2 % mod));
        __y2 /= mod;
    }
    return __x + __z;
}

template <typename _Tp, typename _Alloc, typename = typename std::enable_if_t<std::is_integral_v<_Tp>>>
vector<_Tp, _Alloc> operator*(const vector<_Tp, _Alloc> &__x, const vector<_Tp, _Alloc> &__y)
{
    vector<_Tp, _Alloc> __result;
    const _Tp __mod = getmod<_Tp>();
    for (size_t i = 0; i < __y.size(); ++i)
    {
        vector<_Tp, _Alloc> __tmp(i);
        _Tp __overflow = 0;
        for (size_t j = 0; j < __x.size(); j++)
        {
            __overflow += __y[i] * __x[j];
            __tmp.push_back(__overflow % __mod);
            __overflow /= __mod;
        }
        if (__overflow != 0)
            __tmp.push_back(__overflow);
        __result = __result + __tmp;
    }
    return __result;
}

template <typename _Tp, typename _Tp2, typename _Alloc,
          typename = typename std::enable_if_t<std::is_integral_v<_Tp>>,
          typename = typename std::enable_if_t<std::is_integral_v<_Tp2>>>
vector<_Tp, _Alloc> operator*(const vector<_Tp, _Alloc> &__x, const _Tp2 __y)
{
    vector<_Tp, _Alloc> __z;
    size_t mod = static_cast<size_t>(getmod<_Tp>()), __y2 = static_cast<size_t>(__y);
    while (__y2)
    {
        __z.push_back(static_cast<_Tp>(__y2 % mod));
        __y2 /= mod;
    }
    return __x * __z;
}

template <typename _Tp, typename _Alloc, typename = typename std::enable_if_t<std::is_integral_v<_Tp>>>
ostream &operator<<(ostream &out, const vector<_Tp, _Alloc> &__v)
{
    if (__v.size() > 0)
        out << *__v.rbegin();
    if (__v.size() > 1)
        for_each(++__v.rbegin(), __v.rend(), [&](const _Tp &_a) { out << setw(static_cast<int>(getlength<_Tp>())) << setfill('0') << _a; });
    return out;
}

int main(void)
{
    vector<size_t> a{1}, total = a;
    size_t n;
    cin >> n;
    for (size_t i = 2; i <= n; ++i)
    {
        a = a * i;
        total = total + a;
    }
    cout << total << endl;
    return 0;
}

重载vector运算符写的,容器类型可以用各种整数类型,10000以内的阶乘和都可以秒出结果,写的不规范的,求大佬轻喷。逃……

2020/11/16 23:08
加载中...