写了2小时的50分代码

P1009 [NOIP1998 普及组] 阶乘之和

遇事不决就选C @ 2022-06-30 23:13:31

#include <bits/stdc++.h>
using namespace std;
long long a[55], n, cnt, f;
void w() {
    long long sum = 10;
    for (int i = 0; i < log10(cnt) + 1; i++) {
        a[i] = cnt / sum % (sum / 10);
        sum = sum * 10;
    }
    f = log10(cnt) + 1;
}
void l(int wel) {
    int sum = a[wel] / 10;
    a[wel] = a[wel] % 10;
    a[wel + 1] += sum;
    f++;
    if (a[wel + 1] >= 10) l(wel + 1);
}
int main() {
//  freopen("test.in", "r", stdin);
//  freopen("test.out", "w", stdout);
    cin >> n;
    if (n < 22) {
        int sum = 1; 
        for (int i = 1; i <= n; i++) {
            sum = sum * i;
            cnt = cnt + sum;
        }
        cout << cnt;
        return 0;
    }
    long long sum1 = 1;
    for (int i = 1; i <= n; i++) {
        if (i == 10) w();
        if (i >= 10) {
            long long sum = 10;
            for (int j = 0; j < log10(i) + 1; j++) {
                a[j] = a[j] * cnt / sum % (sum / 10);
                sum = sum * 10;
                if (a[j] >= 10) l(j);
            }
        }
        else {
            sum1 = sum1 * i;
            cnt = cnt + sum1; 
        }
    }
    for (int i = 0; i <= f; i++) {
        cout << a[f];
    }
    return 0;
}

by 遇事不决就选C @ 2022-06-30 23:21:28

沙发大佬给关·-·


by Sellaris @ 2022-07-01 00:05:34

高精度。longlong 过不了。注意数据范围


by 遇事不决就选C @ 2022-07-01 07:06:18

@Sellaris 我没学过高精,我自己写的,知道数据范围,你看我那个数组打的,哪里错了


by YCSluogu @ 2022-07-01 07:22:35

@遇事不决就选C ?这道题是高精,没有高精当然拿不了满分


by Keep_RAD @ 2022-07-01 07:36:45

@YCSluogu 用高精了啊,都不看代码的吗


by 遇事不决就选C @ 2022-07-01 07:41:58

@run_after_dream 误打误闯·-·


by 遇事不决就选C @ 2022-07-02 09:50:48

我服了,两天还没有人告诉我


by 我是人999 @ 2022-07-02 12:59:53

@遇事不决就选C

1.数组可能开小了,可能有六十五位左右(我的代码输出)

2.log10 函数的误差可以接受吗(我没用过抱歉)

还有真没看懂您的高精……


by 我是人999 @ 2022-07-02 13:08:51

@我是人999 还有当 n \geq 21 时就要用高精了


by 我是人999 @ 2022-07-02 13:35:37

错的地方有点多啊,首先阶乘之和要先乘再加,加法好像被漏掉了;

其次输出 a[f] 错了;

还有进位等处理也有点问题,建议去 OI Wiki 看看,抱歉我没耐心调了……


| 下一页