75pts求调

P1009 [NOIP1998 普及组] 阶乘之和

yzbzz147268 @ 2024-11-22 13:37:13

#include<bits/stdc++.h>
using namespace std;
int a[500000],b[500000],n,i,j;
int main(){
    cin>>n;
    a[1]=1;
    b[1]=1;
    for(i=2;i<=n;i++){
        for(j=1;j<=n;j++)
            a[j]=a[j]*i;    
        for(j=1;j<=n;j++)
            if(a[j]>9){
                a[j+1]+=a[j]/10;
                a[j]=a[j]%10;
            }
        for(j=1;j<=n;j++){
            b[j]+=a[j];
            if(b[j]>9){
                b[j+1]+=b[j]/10;
                b[j]%=10;
            }
        } 
    }
    for(i=n;i>=1;i--)
        if(a[i]!=0)
            break;
    for(j=i;j>=1;j--) cout<<b[j];
    return 0;
}

by __jwh2024__ @ 2024-11-22 13:42:00

@yzbzz147268 参考一下,求关

#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10, INF=1<<30;
int A[N],B[N],C[N],la,lb,lc;
// 初始化,同时反转数组
void init(string a,string b) {
    la = a.size(), lb = b.size();
    memset(A, 0, sizeof(A));
    memset(B, 0, sizeof(B));
    memset(C, 0, sizeof(C));
    for(int i=0; i<la; i++) A[i]=a[i]-'0';
    for(int i=0; i<lb; i++) B[i]=b[i]-'0';
    reverse(A, A+la), reverse(B, B+lb);
}
// 高精加高精
string add(string a,string b) {
    init(a,b);
    lc = max(la, lb);
    for(int i=0; i<lc; i++) {
        C[i] = A[i]+B[i]+C[i];
        if(C[i]>9) C[i+1] = C[i]/10, C[i] %= 10;
    }
    while(C[lc]) lc++; //进位
    string c;
    for(int i=lc-1; i>=0; i--) c.append(1, C[i]+'0');
    return c;
}
//高精乘以低精
string mul(string a,int b) {
    memset(A,0,sizeof(A)), la=a.size();
    for(int i=0; i<la; i++) A[i]=a[la-1-i]-'0';
    for(int i=0; i<la; i++) A[i]=A[i]*b;
    for(int i=0; i<la; i++) A[i+1]+=A[i]/10, A[i]%=10;
    while(A[la]) A[la+1]=A[la]/10, A[la]%=10, la++; // 进位
    string c;
    for(int i=la-1; i>=0; i--) c.append(1, A[i]+'0');
    return c;
}
int main() {
    int n; cin>>n;
    string base="1", s="1";
    for(int i=2; i<=n; i++){
        base = mul(base, i);
        s = add(s, base);
    }
    cout<<s<<endl;
    return 0;
}

by yzbzz147268 @ 2024-11-22 13:49:58

@jwh2024...有点看不懂啊.....不过关注了


by __jwh2024__ @ 2024-11-22 14:31:46

@yzbzz147268 理解意思即可哈


|