自测无问题,但30分,大佬帮忙看看,感激不尽!

P1591 阶乘数码

XSean @ 2022-08-21 15:56:42


#include<bits/stdc++.h>
using namespace std;
int t;
string jc(string s1,string s2){
    int a[10001]={},b[10001]={},c[10001]={};
    string s3;
    int len1=s1.length();int len2=s2.length();
    for(int i=len1-1,j=1;i>=0,j<=len1;i--,j++){
        a[j]=s1[i]-'0';
    }
    for(int i=len2-1,j=1;i>=0,j<=len2;i--,j++){
        b[j]=s2[i]-'0';
    }
    int maxl=len1+len2;
    for(int i=1;i<=len2;i++){
        for(int j=1;j<=len1;j++){
            c[i+j-1]+=a[j]*b[i];
            c[i+j]+=c[i+j-1]/10;
            c[i+j-1]%=10;
        }
    }
    while(c[maxl]==0&&maxl>1) maxl--;
    for(int i=maxl;i>=1;i--){
        s3+=char(c[i]+'0');
    }
    return s3;
}
int main(){
/*
freopen (".in","r",stdin);
freopen (".out","w",stdout);
*/
cin>>t;
int n,cnt=0;
string n1[11];
for(int i=1;i<=10;i++){
    n1[i]+="1";
}
char a1;
for(int i=1;i<=t;i++){
    cnt=0;
    cin>>n>>a1;
    for(int j=2;j<=n;j++){
        stringstream j1;
        j1<<j;
        string j2=j1.str();
//      cout<<"j2:"<<j2<<endl;
//      cout<<"n1i:"<<n1[i]<<endl;
        n1[i]=jc(n1[i],j2);
    }
    int len=n1[i].length();
//  cout<<"n1:"<<n1[i]<<endl;
    for(int k=1;k<=len;k++){
        if(n1[i][k]==a1) cnt++;
    }
    cout<<cnt<<endl;
} 

/*
fclose(stdin);
fclose(stdout);
*/
    return 0;
}

|