50分求调

P1009 [NOIP1998 普及组] 阶乘之和

emyutd @ 2024-11-29 20:57:03

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> mul(vector<int> A,vector<int> B)
{

    if(A.size()<B.size())
    {
        return mul(B,A);
    }
    int t=0;
    vector<int> C(A.size()+B.size(),0);
    for(int i=0;i<B.size();i++)
    {
        for(int j=0;j<A.size();j++)
        {
            C[i+j]=A[j]*B[i];
        }
    }
    for(int i=0;i<C.size();i++)
    {
        C[i+1]+=C[i]/10;
        C[i]%=10;
    }
    while(C.size()>1&&C.back()==0)
    {
        C.pop_back();
    }
    return C;
}
vector<int> add(vector<int> A,vector<int> B)
{
    if(A.size()<B.size())
    {
        return add(B,A);
    }
    int t=0;
    vector<int> C;
    for(int i=0;i<A.size();i++)
    {
        t+=A[i];
        if(i<B.size())
        {
            t+=B[i];
        }
        C.push_back(t%10);
        t/=10;
    }
    if(t) C.push_back(t);
    return C;
}
vector<int> fact(int a)
{
    vector<int> result(1,1);
    for(int i=2;i<=a;i++)
    {
        vector<int> multi;
        int temp=i;
        while(temp>0)
        {
            multi.push_back(temp%10);
            temp/=10;
        }
        result=mul(result,multi);
    }
    return result;
}
vector<int> sum_fact(int n)
{
    vector<int> sum(1,0);
    for(int i=1;i<=n;i++)
    {
        vector<int> fac=fact(i);
        sum=add(sum,fac);
    }
    return sum;
}
int main()
{
    int n;
    cin>>n;
    vector<int> result=sum_fact(n);
    for(int i=result.size()-1;i>=0;i--)
    {
        cout<<result[i];
    }
    return 0;
}

by Arefa @ 2024-11-29 21:41:29

代码第 23 行: C[i+j]=A[j]*B[i];

应将=改为+=

例如 12 * 34,乘积的十位,应该等于 1*4+2*3 ,而不是其中的一项。


by Dave123 @ 2024-11-30 17:13:51

AC代码

#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <iostream>
using namespace std;
#include <string>

struct BigInteger {
    int S[200] = { 0 };
    int len = 200;

    int getFirst() {
        for (int i = 0; i < len; i++) {
            if (S[i] != 0) return i;
        }
        return 199;
    }

    void clean() {
        for (int i = len - 1; i >= 1; i--) {
            if (S[i] >= 10) {
                int prefix = S[i] / 10;
                S[i] -= prefix * 10;
                S[i - 1] += prefix;
            }
        }
    }

    void add(int n) {
        string s = std::to_string(n);
        int next = 200;
        for (int i = s.length() - 1; i >= 0; i--) {
            S[--next] += s[i] - '0';
        }
        clean();
    }

    void multi(int n) {
        for (int i = len - 1; i >= 0; i--) {
            S[i] *= n;
        }
        clean();
    }

    void add(int S[]) {
        for (int i = len - 1; i >= 0; i--) {
            this->S[i] += S[i];
        }
        clean();
    }

    string to_string() {
        string str = "";
        for (int i = getFirst(); i < len; i++) {
            str += std::to_string(S[i]);
        }
        return str;
    }
};

int main() {
    int n;
    scanf("%d", &n);
    BigInteger sum;
    for (int i = 1; i <= n; i++) {
        BigInteger bi;
        bi.add(1);
        for (int j = i; j >= 1; j--) {
            bi.multi(j);
        }
        sum.add(bi.S);
    }
    cout << sum.to_string() << endl;

    return 0;
}

by YizhiYinbao @ 2024-11-30 19:54:02

@Dave123 膜拜


|