50分,求助(悬关)

P1009 [NOIP1998 普及组] 阶乘之和

Coffee_Moew @ 2023-08-01 11:52:06

WA了#3,#4,代码如下:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,s=0,cj;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cj=1;
        for(int j=1;j<=i;j++) cj=cj*j;
        s=s+cj;
    }
    cout<<s;
    return 0;
}

by yzm0325 @ 2023-08-01 11:54:20

@Coffee_Moew 要高精度


by Coffee_Moew @ 2023-08-01 11:54:54

@zym0325 谢啦


by diandian_ @ 2023-08-01 11:58:00

#include<bits/stdc++.h>
using namespace std;
long long a[1005], b, c[1005], n, ans[1005], t[1005]; 
void s2BIG(string s, long long a[]) {
    a[0] = s.size(); 
    for (long long i = 1; i <= a[0]; i++) a[i] = s[a[0] - i] - '0'; 
}
void addBIG(long long a[], long long b[], long long c[]) {
    c[0] = max(a[0], b[0]); 
    long long t = 0; 
    for (long long i = 1; i <= c[0]; i++) {
        c[i] = a[i] + b[i] + t; 
        t = c[i] / 10; 
        c[i] %= 10; 
    }
    if (t > 0) {
        c[0]++; 
        c[c[0]] = t; 
    }
}
void mulBIG(long long a[], long long b, long long c[]) {
    c[0] = a[0]; 
    long long t = 0; 
    for (long long i = 1; i <= c[0]; i++) {
        c[i] = a[i] * b + t; 
        t = c[i] / 10; 
        c[i] %= 10; 
    }
    while (t > 0) {
        c[0]++; 
        c[c[0]] = t % 10; 
        t /= 10; 
    }
}
void printBIG(long long a[]) {
    for (long long i = a[0]; i >= 1; i--) cout << a[i]; 
    cout << endl; 
}
int main() {
    cin >> n; 
    for (long long i = 1; i <= n; i++) {
        memset(t, 0, sizeof(t)); 
        s2BIG("1", t); 
        for (long long j = 1; j <= i; j++) {
            mulBIG(t, j, t); 
        }
        addBIG(ans, t, ans); 
    }
    printBIG(ans); 
    return 0;
}

by XLoffy @ 2023-08-01 12:14:46

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring> 
using namespace std;
int ans[1010],a[1010];
void printBIG(int* a)
{
    for (int i = a[0];i >= 1;i--)
        cout << a[i];
    cout << endl;
    return;
}
void mulBIG(int* x,int y,int* z)
{
    z[0] = x[0];
    for (int i = 1;i <= z[0];i++)
        z[i] = x[i] * y;
    for (int i = 1;i <= z[0];i++)
    {
        z[i + 1] += z[i] / 10;
        z[i] %= 10;
        if (z[z[0] + 1] > 0) z[0]++;
    }
}
void addBIG(int* x,int* y,int* z)
{
    z[0] = max(x[0],y[0]);
    for (int i = 1;i <= z[0];i++)
        z[i] = x[i] + y[i];
    for (int i = 1;i <= z[0];i++)
    {
        z[i + 1] += z[i] / 10;
        z[i] %= 10;
        if (z[z[0] + 1] > 0) z[0]++;
    }
}
int main()
{
    int n;
    cin >> n;
    for (int i = 1;i <= n;i++)
    {
        memset(a,0,sizeof(a));
        a[0] = a[1] = 1;
        for (int j = 1;j <= i;j++)
            mulBIG(a,j,a);
        addBIG(ans,a,ans);
    }
    printBIG(ans);
    return 0;
}

|