1 条题解

  • 0
    @ 2024-9-24 20:41:11

    题解:递推

    先考虑每一层元素个数,再考虑整体。

    假设第 kk 层数量为 aka_k,于是有 ak=ak1+ka_k = a_{k-1}+k,前 kk 层数量和为 bk=i=1kai=bk1+akb_k = \sum_{i=1}^{k} a_i=b_{k-1}+a_k

    由于只需要求出前 nn 层和,对于 bib_i 无需使用数组,直接用 ansans 来维护即可。

    #include <bits/stdc++.h>
    using namespace std;
    const int N = 101;
    int n,a[N],ans=1;
    
    int main() {
        cin >> n;
        a[1] = 1;  // a[i] 第 i 层数量
        for (int i = 2; i <= n; i++) {
            a[i] = a[i - 1] + i;
            ans += a[i];
        }
        cout << ans;
        return 0;
    }
    

    大数运算

    如果数据很大,需要使用大数计算。

    #include <bits/stdc++.h>
    using namespace std;
    const int N = 201;
    vector<int> f[N], a[N];
    
    vector<int> add(vector<int>& a, vector<int>& b) {
        int la = a.size(), lb = b.size();
        vector<int> c;
        for (int i = 0, t = 0; i < max(la, lb) || t; i++) {
            if (i < la) t += a[i];
            if (i < lb) t += b[i];
            c.push_back(t % 10), t /= 10;
        }
        return c;
    }
    
    vector<int> add(vector<int>& a, int b) {
        vector<int> c;
        while (b) c.push_back(b % 10), b /= 10;
        return add(a, c);
    }
    
    int main() {
        int n; cin >> n;
        a[1].push_back(1);
        f[1].push_back(1);
        for (int i = 2; i <= n; i++) {
            a[i] = add(a[i - 1], i);
            f[i] = add(f[i - 1], a[i]);
        }
        vector<int>::reverse_iterator it = f[n].rbegin();
        for (; it != f[n].rend(); it++)
            cout << *it;
        cout << endl;
        return 0;
    }
    
    • 1

    信息

    ID
    220
    时间
    1000ms
    内存
    16MiB
    难度
    1
    标签
    递交数
    149
    已通过
    103
    上传者