25pts,和测试点答案一模一样,但是就是WA

P1009 [NOIP1998 普及组] 阶乘之和

sun_qy @ 2024-01-03 16:18:34

#include <bits/stdc++.h>
using namespace std;
struct node {
    int a[22500];
    node() {
        memset(a, 0, sizeof(a));
    }
    node(unsigned long long x) {
        int i = 1;
        while(x) {
            a[i ++] = x % 10;
            x /= 10;
        }
        a[0] = -- i;
    }
    void input() {
        string s;
        cin >> s;
        a[0] = s.size();
        for(int i = 1; i <= a[0]; i ++)
            a[i] = s[a[0] - i] - '0';
    }
    bool zero() {
        return a[0] == 1 && a[1] == 0;
    }
    void print() {
        if(zero()) {
            printf("0\n");
            return;
        }
        int tmp = 0;
        for(int i = a[0]; i >= 1; i --) {
            if(a[i] || tmp) {
                printf("%d", a[i]);
                tmp = 1;
            }
        }
        printf("\n");
    }
};
inline node operator + (node x, node y) {
    node res;
    res.a[0] = max(x.a[0], y.a[0]);
    for(int i = 1; i <= res.a[0]; i ++) {
        res.a[i] += x.a[i] + y.a[i];
        if(res.a[i] >= 10) res.a[i] -= 10, res.a[i + 1] ++;
    }
    if(res.a[res.a[0] + 1]) res.a[0] ++;
    return res;
}
inline node operator * (node x, node y) {
    int lx = x.a[0], ly = y.a[0];
    node res;
    if(x.zero()) return x;
    if(y.zero()) return y;
    res.a[0] = lx + ly;
    for(int i = 1; i <= lx; i ++) {
        for(int j = 1; j <= ly; j ++) {
            res.a[i + j - 1] += ((x.a[i] * y.a[j]) % 10);
            res.a[i + j] += ((x.a[i] * y.a[j]) / 10);
            res.a[i + j] += res.a[i + j - 1] / 10;
            res.a[i + j - 1] %= 10;
        }
    }
    if(res.a[lx + ly]) res.a[0] ++;
    return res;
}
inline node operator - (node x, node y) {
    node res;
    if(y.zero()) return x;
    res.a[0] = max(x.a[0], y.a[0]);
    for(int i = 1; i <= res.a[0]; i ++) {
        res.a[i] += x.a[i] - y.a[i];
        if(res.a[i] < 0) x.a[i + 1] --, res.a[i] += 10;
    }
    if(res.a[res.a[0]] == 0) res.a[0] --;
    if(res.a[0] == 0) res.a[0] = 1, res.a[1] = 0;
    return res;
}
node fac(node x) {
    node res = 1;
    for(; !x.zero(); x = x - 1) {
        res = res * x;
    }
    return res;
}
int main() {
    node t, res;
    t.input();
    while(!t.zero()) {
        res = res + fac(t);
        t = t - 1;
    }
    res.print();
    return 0;
}

注:其它ojAC了,数据范围比这个还大,对拍没有任何问题


by yuqixin2008 @ 2024-01-03 19:24:59

其实这个不用重载运算符就能过的其坤


by sun_qy @ 2024-01-04 20:05:49

@myxsb 其实我知道这个,但是我喜欢这个写法


by swuster27 @ 2024-01-30 22:38:37

高精度加法+高精度乘法


by swuster27 @ 2024-01-30 22:50:47


#include<iostream>
using namespace std;
static const int N = 100;
int a[N], b[N];
int main()
{
    int n, len;
    cin >> n;
    a[0] = 1; b[0] = 1;
    for (int i = 2; i <= n; i++)
    {
      //高精度乘法
        for (int j = 0; j <N ; j++)
        {
            a[j] *= i;
        }
        for (int j = 0; j <N; j++)
        {
            a[j + 1] += a[j] / 10;
            a[j] %= 10;
        }
      //高精度加法
        for (len = 0; len <N; len++)
        {
            b[len] += a[len];
            b[len + 1] += b[len] / 10;
            b[len] %= 10;
        }
    }
  //删除多余前导0
    for (len=N; b[len] == 0; len--);
    for (int i = len; i >= 0; i--)
    {
        cout << b[i];
    }
    cout << endl;
    return 0;
}

|