zhengzhisheng6 2022-02-02 22:16 采纳率: 91.7%
浏览 136
已结题

唉,TLE,提供一下方法吧

题目描述
有n个人,每个人都拥有一个喜悦值,接下来,任意三个人之间会进行一次交流,任意三个人交流所获得的贡献为三个人的喜悦值的和,求任意三个人交流所获得的贡献之和
输入格式:
第一行一个整数n
接下来n行,每行一个整数a,表示第i个人的喜悦值
输出格式:
按题目描述输出
样例输入1:
3
2
3
4
样例输出1:
9
约定:
n<=1000
1<=a<=10^100

#include <bits/stdc++.h>
class BigInt
{
public:
    BigInt(unsigned int n = 0)
    {
        if (n == 0)
            _data.push_back('0');
        else
        {
            std::string str;
            while (n)
            {
                str.push_back(n % 10 + '0');
                n /= 10;
            }
            _data.assign(str.rbegin(), str.rend());
        }
    }
  
    BigInt(const std::string &s) : _data(s) {}
  
private:
    std::string _data;
  
    friend BigInt operator+(const BigInt &lsh, const BigInt &rhs);
    friend std::istream &operator>>(std::istream &is, BigInt &a);
    friend std::ostream &operator<<(std::ostream &os, const BigInt &a);
};
  
BigInt operator+(const BigInt &lhs, const BigInt &rhs)
{
    std::string r;
    int carry = 0;
    std::string::const_reverse_iterator itr1 = lhs._data.rbegin();
    std::string::const_reverse_iterator itr2 = rhs._data.rbegin();
    while (itr1 != lhs._data.rend() && itr2 != rhs._data.rend())
    {
        int a = *itr1 - '0';
        int b = *itr2 - '0';
        int c = a + b + carry;
        if (c >= 10)
        {
            carry = c / 10;
            c %= 10;
        }
        else
        {
            carry = 0;
        }
        r.push_back(c + '0');
        ++itr1;
        ++itr2;
    }
    while (itr1 != lhs._data.rend())
    {
        int a = *itr1 - '0' + carry;
        if (a >= 10)
        {
            carry = a / 10;
            a %= 10;
        }
        else
        {
            carry = 0;
        }
        r.push_back(a + '0');
        ++itr1;
    }
    while (itr2 != rhs._data.rend())
    {
        int a = *itr2 - '0' + carry;
        if (a >= 10)
        {
            carry = a / 10;
            a %= 10;
        }
        else
        {
            carry = 0;
        }
        r.push_back(a + '0');
        ++itr2;
    }
    if (carry > 0)
        r.push_back(carry + '0');
    return BigInt(std::string(r.rbegin(), r.rend()));
}
  
std::istream &operator>>(std::istream &is, BigInt &a)
{
    is >> a._data;
    return is;
}
  
std::ostream &operator<<(std::ostream &os, const BigInt &a)
{
    os << a._data;
    return os;
}
  
int main()
{
    int n;
    std::cin >> n;
    std::vector<BigInt> a(n);
    for (int i = 0; i < n; i++)
        std::cin >> a[i];
    BigInt sum = 0;
    for (int i = 0; i < n - 2; i++)
        for (int j = i + 1; j < n - 1; j++)
            for (int k = j + 1; k < n; k++)
                sum = sum + a[i] + a[j] + a[k];
    std::cout << sum;
    return 0;
}//源自—GX—

想不出来如何优化

  • 写回答

3条回答 默认 最新

  • _GX_ 2022-02-04 06:42
    关注
    #include <iostream>
    #include <string>
    #include <iterator>
    #include <vector>
    
    class BigInt
    {
    public:
        BigInt(unsigned int n = 0)
        {
            if (n == 0)
                _data.push_back('0');
            else
            {
                while (n)
                {
                    _data.push_back(n % 10 + '0');
                    n /= 10;
                }
            }
        }
    
        BigInt(const std::string &s) : _data(s.rbegin(), s.rend()) {}
    
        BigInt &operator+=(const BigInt &other);
        BigInt &operator*=(const BigInt &other);
    
        BigInt operator+(const BigInt &other) const
        {
            BigInt t = *this;
            t += other;
            return t;
        }
    
        BigInt operator*(const BigInt &other) const
        {
            BigInt t = *this;
            t *= other;
            return t;
        }
    
    private:
        std::string _data;
    
        friend std::istream &operator>>(std::istream &is, BigInt &a);
        friend std::ostream &operator<<(std::ostream &os, const BigInt &a);
    };
    
    BigInt &BigInt::operator+=(const BigInt &other)
    {
        int carry = 0;
        std::string::iterator itr1 = _data.begin();
        std::string::const_iterator itr2 = other._data.begin();
        while (itr1 != _data.end() && itr2 != other._data.end())
        {
            int a = *itr1 - '0';
            int b = *itr2 - '0';
            int c = a + b + carry;
            carry = c / 10;
            c %= 10;
            *itr1 = c + '0';
            ++itr1;
            ++itr2;
        }
        while (itr1 != _data.end())
        {
            int a = *itr1 - '0' + carry;
            carry = a / 10;
            a %= 10;
            *itr1 = a + '0';
            ++itr1;
        }
        while (itr2 != other._data.end())
        {
            int a = *itr2 - '0' + carry;
            carry = a / 10;
            a %= 10;
            _data.push_back(a + '0');
            ++itr2;
        }
        if (carry > 0)
            _data.push_back(carry + '0');
        return *this;
    }
    
    BigInt &BigInt::operator*=(const BigInt &other)
    {
        BigInt r;
        for (std::size_t i = 0; i < other._data.size(); i++)
        {
            int x = other._data[i] - '0';
            int carry = 0;
            BigInt t = *this;
            for (std::string::iterator itr = t._data.begin(); itr != t._data.end(); ++itr)
            {
                int y = *itr - '0';
                int a = x * y + carry;
                *itr = a % 10 + '0';
                carry = a / 10;
            }
            if (carry > 0)
                t._data.push_back(carry + '0');
            if (i > 0)
            {
                std::string zeros(i, '0');
                t._data.insert(t._data.begin(), zeros.begin(), zeros.end());
            }
            r += t;
        }
        *this = r;
        return *this;
    }
    
    std::istream &operator>>(std::istream &is, BigInt &a)
    {
        std::string s;
        is >> s;
        a = s;
        return is;
    }
    
    std::ostream &operator<<(std::ostream &os, const BigInt &a)
    {
        std::copy(a._data.rbegin(), a._data.rend(), std::ostream_iterator<char>(os));
        return os;
    }
    
    int main()
    {
        int n;
        std::cin >> n;
        std::vector<BigInt> a(n);
        for (int i = 0; i < n; i++)
            std::cin >> a[i];
        BigInt sum = 0;
        BigInt num = (n - 1) * (n - 2) / 2;
        for (int i = 0; i < n; i++)
            sum += a[i] * num;
        std::cout << sum;
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 2月12日
  • 已采纳回答 2月4日
  • 创建了问题 2月2日

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么