扣50分是哪里的问题啊?

P1009 [NOIP1998 普及组] 阶乘之和

kaistar2004 @ 2023-03-07 20:19:47

#include<stdio.h>
int m(int m)
{
int x,y;
for(x=1,y=1;x<=m;x++)
{
y=y*x;
}
return y;
}
int main()
{
int a,n;
long b;
scanf("%d",&n);
for(a=1,b=0;a<=n;a++)
{
b=b+m(a);
}
printf("%ld",b);
return 0;
}

by InversionShadow @ 2023-03-07 20:21:23

高精度


by No21 @ 2023-03-07 20:35:28

50的阶乘大约是3*10^64,暴力已经炸到姥姥家了,这个题是用高精度算法的,不会的话可以去B站搜一下,有些UP讲的很不错,学完高精度就能秒了


by liyincan @ 2023-03-07 20:40:56

long long


by liuzekai123 @ 2023-03-19 16:27:59

首先用高精度,其次用long long int,因为输入量很大


by xxc123 @ 2023-04-10 22:21:01

@lumos1 long long int ???


by xxc123 @ 2023-04-10 22:21:24

@kaistar2004 参考 求关注


#include<iostream>
using namespace std;
int a[1001],b[1001];
void add()
{
    int t=0;
    for(int i=1000;i>=1;i--)
    {
        b[i]=b[i]+a[i]+t;
        t=b[i]/10;
        b[i]%=10;
    }
}
void multiplication(int x)
{
    int t=0;
    for(int i=1000;i>=1;i--)
    {
        a[i]=a[i]*x+t;
        t=a[i]/10;
        a[i]%=10;
    }
}
void print()
{
    int flag=0;
    for(int i=1;i<=1000;i++)
    {
        if(b[i]!=0) flag=1;
        if(flag) cout<<b[i];
    }
}
int main()
{
    a[1000]=b[1000]=1;
    int n;
    cin>>n;
    for(int i=2;i<=n;i++)
    {
        multiplication(i);
        add();
    }
    print();
    return 0;
}

by liuzekai123 @ 2023-04-15 18:53:52

@xxc123 这个人习惯,longlong,longlongint都可以……^v^


|