JAVA只有前两个AC

P1009 [NOIP1998 普及组] 阶乘之和

ljt0203 @ 2023-06-03 17:16:41


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        long a= scanner.nextInt();
        long b=1,c=a,d=a;
        long sum=0;
        if (a==0){
            System.out.println(0);
        }else {
            for (int j=0;j<d;j++){
                for (int i=0;i<c;i++) {
                    b = b * a;
                    a--;
                }
                sum=sum+b;
                c--;
                a=c;
                b=1;
            }
            BigInteger e=BigInteger.valueOf(sum);
            System.out.println(e);
        }

    }
}

by NeNe_ @ 2023-06-03 20:41:01

这题要写高精,等教到高精再写罢(c++党懒得打所以直接python过了


by Qixin429429 @ 2023-06-22 20:42:06


string Mult(string as,string bs)
{
    string cs="";
    int al=as.size(),bl=bs.size();
    int cl=al+bl;
    for(int i=1;i<=al;i++)a[i]=as[al-i]-'0';
    for(int i=1;i<=bl;i++)b[i]=bs[bl-i]-'0';
    for(int i=1;i<=al;i++)
    {
        for(int j=1;j<=bl;j++)
        {
            c[i+j-1]+=a[i]*b[j];
            c[i+j]+=c[i+j-1]/10;
            c[i+j-1]%=10;
        }
    }
    while(c[cl]==0&&cl>1)cl--;
    for(int i=cl;i>=1;i--)cs+=c[i]+'0';
    return cs;
}

#include <bits/stdc++.h>
using namespace std;
const int N=1000;
int a[N],b[N],c[N+N];
string Add(string as,string bs)
{
    memset(a,0,sizeof(a));
    memset(b,0,sizeof(b));
    memset(c,0,sizeof(c));
    string cs="";
    int al=as.size(),bl=bs.size();
    int cl=max(al,bl)+1;
    for(int i=1;i<=al;i++)a[i]=as[al-i]-'0';
    for(int i=1;i<=bl;i++)b[i]=bs[bl-i]-'0';
    for(int i=1;i<=cl;i++)
    {
        c[i]+=a[i]+b[i];
        c[i+1]=c[i]/10;
        c[i]%=10;
    }
    while(c[cl]==0&&cl>1)
    {
        cl--;
    }
    for(int i=cl;i>=1;i--)cs+=c[i]+'0';
    return cs;
 }
string Mult(string as,int x)
{
    memset(a,0,sizeof(a));
    memset(c,0,sizeof(c));
    string cs="";
    int al=as.size();
    int cl=al;
    for(int i=1;i<=al;i++)a[i]=as[al-i]-'0';
    for(int i=1;i<=cl;i++)
    {
        c[i]+=a[i]*x;
        c[i+1]+=c[i]/10;
        c[i]%=10;
    }
    int n=c[cl+1];
    while(n)
    {
        c[++cl]=n%10;n/=10;
    }
    while(c[cl]==0&&cl>1)cl--;
    for(int i=cl;i>=1;i--)cs+=c[i]+'0';
    return cs;
}
int main(){
    int n;
    cin>>n;
    string sum="0",cum="1";
    for(int i=1;i<=n;i++)
    {
        cum=Mult(cum,i);
        sum=Add(sum,cum);
    }
    cout<<sum;
    return 0;
}

|