求助,00!

P1009 [NOIP1998 普及组] 阶乘之和

Ghy_boy @ 2024-01-28 09:50:43

#include<bits/stdc++.h>
using namespace std;
int a[1000000],s[100005],n,num;
int main(){
    cin>>n;
    s[1]=1;
    for(int k=2;k<=n;k++){
        num=1;
        int jin=0;
        a[1]=1;
        for(int i=2;i<=k;i++){
            jin=0;
            for(int j=1;j<=num;j++){
                a[j]=a[j]*i+jin;
                jin=a[j]/10;
                a[j]=a[j]%10;
            }
            while(jin){
                num++;
                a[num]=jin%10;
                jin/=10;
            }
        }
    }
        for(int i=1;i<=num;i++){
            s[i]+=a[i];
            s[i+1]=s[i+1]+s[i]/10;
            s[i]=s[i]%10;
        }
    if(s[num+1]) num++;
    for(int i=num;i>=1;i--){
        cout<<s[i];
    }    
    return 0;
}

样例都能过,提交全WA。再说,我抄的课堂笔记写的,为什么全WA?太太太太太太难受啦!!!!


by Doufu_killer @ 2024-01-28 10:01:31

#include <bits/stdc++.h>
using namespace std;
int c[100001], d[100001]; // c存放结果, d存放阶乘结果
int len0, len1;
void add()
{
    int len = max(len0, len1);
    for (int i = 0; i < len; i++)
    {
        c[i] += d[i];
        c[i + 1] += c[i] / 10;
        c[i] %= 10;
        if (c[i] && i >= len0)
            len0++;
    }
}
void jc_add(int a)
{

    for (int i = 0; i < len1; i++)
        d[i] *= a;
    for (int i = 0; i < len1; i++)
    {
        d[i + 1] += d[i] / 10;
        d[i] %= 10;
        if (d[i + 1] && i + 1 >= len1)
            len1++;
    }
}
int main()
{
    int n;
    cin >> n;
    memset(c, 0, sizeof(c));
    memset(d, 0, sizeof(d));
    c[0] = 0;
    len0 = 1;
    len1 = 1;
    d[0] = 1;
    for (int i = 1; i <= n; i++)
    {
        jc_add(i);
        add();
    }
    for (int i = len0 - 1; i >= 0; i--)
        cout << c[i];
    cout << endl;
}

by Doufu_killer @ 2024-01-28 10:03:37

上方AC代码,临时写的O(∩∩)OO(∩∩)O


by Dark_Monarch @ 2024-01-28 14:15:22

@1111111x @Doufu_killer

也可以封装一个高精度

本蒟蒻先上代码

我也是抄的课堂笔记

#include<bits/stdc++.h>
#define maxn 100
using namespace std;

struct Bigint{;
    int len,a[maxn];
    Bigint(int x = 0){
        memset(a,0,sizeof(a));
        for(len = 0;x;len++)
            a[len] = x % 10,x /= 10;
    }
    int &operator[](int i){ return a[i]; }
    void flatten(int L){
        len = L;
        for(int i = 0;i < len;i++)
            a[i + 1] += a[i] / 10,a[i] %= 10;
        for(; len >= 1 && !a[len - 1];)
        len--;
    }
    void print(){
        for(int i = max(len-1,0);i >= 0;i--)
            printf("%d",a[i]);
    }
};
Bigint operator+(Bigint &a,Bigint &b){
    Bigint c;
    int len = max(a.len,b.len);
    for(int i = 0;i < len;i++)
        c[i] += a[i] + b[i];
    c.flatten(len + 1);
    return c;
}
Bigint operator*(Bigint &a,int b){
    Bigint c;
    int len = a.len;
    for(int i = 0;i < len;i++)
        c[i] = a[i] * b;
    c.flatten(len + 11);
    return c;
}

int main(){
    Bigint ans(0),fac(1);
    int m;
    cin >> m;
    for(int i = 1;i <= m;i++){
    fac = fac * i;
    ans = ans + fac;
    }
    ans.print();
    return 0;
}

by Ghy_boy @ 2024-01-28 14:24:57

@ywz121014 啊!!!!!!!这么长!


by Ghy_boy @ 2024-01-28 14:25:28

@Doufu_killer 6!


by Doufu_killer @ 2024-01-28 15:03:21

@1111111x 就是大括号有点多,再加了点函数而已(^-^


|