大佬帮我看看为什么只有50分

P1009 [NOIP1998 普及组] 阶乘之和

wlq484906073 @ 2022-10-23 18:52:02

#include<stdio.h>
int main()
{
    int n,t=1,s=0;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        t=t*i;
        s=s+t;
    }
    printf("%d\n",s);
    return 0;
}

by _djc_ @ 2022-10-23 18:53:21

因为要用高精……


by RichardCgy @ 2022-10-23 19:00:03

拿出来了压箱底的高精度模板

#include <cstdio>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
class BigInt//所有数字都需要是正整数 
{
    public:
        int value[1000];//1为个位,2为十位,... 
        int size=0;
        void clear()
        {
            for(int i=0;i<=999;i++)
                value[i]=0;
            size=1;
        }
        BigInt()//创建时自动清空 
        {
            for(int i=1;i<=999;i++)
                value[i]=0;
            size=1;
        }
        BigInt(BigInt* a)
        {
            for(int i=1;i<=a->size;i++)
                value[i]=a->value[i];
            size=a->size;
        }
        BigInt(long long a)
        {
            clear();
            size=1;
            while(a>0)
            {
                value[size]=a%10;
                size++;
                a/=10;
            }
            size--;
        }
        void print()//输出 
        {
            if(size==0)
            {
                std::printf("%d",0);
                return;
            }
            for(int i=size;i>=1;i--)
                std::printf("%d",value[i]);
        }
        void read()
        {
            string a;
            cin>>a;
            for(int i=a.length()-1;i>=0;i--)
            {
                this->value[a.length()-i]=a[i]-'0';
            }
            this->size=a.length();
        }
        BigInt add(BigInt a,BigInt b)
        {
            int cnt=a.size;
            if(cnt<b.size)
                cnt=b.size;
            BigInt ans;
            for(int i=1;i<=cnt;i++)
            {
                ans.value[i]+=a.value[i]+b.value[i];
                ans.value[i+1]+=ans.value[i]/10;
                ans.value[i]%=10; 
            }
            ans.size=cnt;
            if(ans.value[cnt+1]!=0)
                ans.size++;
            return ans;
        }
        int big_number_cmp(BigInt a,BigInt b)//0相等,1左大于右,2右大于左 
        {
            if(b.value[b.size]>=0&&a.value[a.size]<0)
                return 2;
            if(a.size!=b.size)
            {
                if(a.size>b.size)
                    return 1;
                return 2;
            }
            for(int i=a.size;i>=1;i--)
            {
                if(a.value[i]!=b.value[i])
                {
                    if(a.value[i]>b.value[i])
                        return 1;
                    return 2;
                }
            }
            return 0;
        }
        int operator =(int other)//赋值给高精度数据 
        {
            int a=other;
            size=1;
            while(other>0)
            {
                value[size]=other%10;
                other/=10;
                size++;
            }
            size--;
            return a;
        }
        BigInt operator +(BigInt b)//高精加! 
        {
            int cnt=this->size;
            if(cnt<b.size)
                cnt=b.size;
            BigInt ans;
            for(int i=1;i<=cnt;i++)
            {
                ans.value[i]+=this->value[i]+b.value[i];
                ans.value[i+1]+=ans.value[i]/10;
                ans.value[i]%=10; 
            }
            ans.size=cnt;
            if(ans.value[cnt+1]!=0)
                ans.size++;
            return ans;
        }
        BigInt operator -(BigInt b)//高精减! 
        {
            int cnt=this->size;
            if(big_number_cmp(this,b)==2)
            {
                cerr<<"在减法中结果出现负数\n";
                exit(1001);
            }
            else if(big_number_cmp(this,b)==0)
            {
                BigInt ans;
                return ans;
            }
            BigInt ans;
            for(int i=1;i<=cnt;i++)
            {
                ans.value[i]=this->value[i]-b.value[i];
            }
            for(int i=1;i<=cnt;i++)
            {
                if(ans.value[i]<0)
                {
                    ans.value[i+1]--;
                    ans.value[i]+=10;
                }
            }
            for(int i=cnt;i>=1;i--)
            {
                if(ans.value[i]!=0)
                {
                    ans.size=i;
                    break;
                }
            }
            return ans;
        }
        BigInt multiply_(BigInt a,int b)//b=0~10,单位高精乘 
        {
            BigInt ans;
            if(b==0)
            {
                return ans;
            }
            if(b==10)
            {
                for(int i=1;i<=a.size;i++)
                    ans.value[i+1]=a.value[i];
                ans.size=a.size+1;
                return ans;
            }
            for(int i=1;i<=a.size;i++)
                ans.value[i]=a.value[i]*b;
            for(int i=1;i<=a.size;i++)
            {
                if(ans.value[i]>=10)
                {
                    ans.value[i+1]+=ans.value[i]/10;
                    ans.value[i]%=10;
                }
            }
            for(int i=999;i>=1;i--)
            {
                if(ans.value[i]!=0)
                {
                    ans.size=i;
                    break;
                }
            }
            if(ans.size==0)
                ans.size=1;
            return ans;
        }
        BigInt multiply(BigInt a,BigInt b)
        {
            BigInt ans;
            for(int i=1;i<=a.size;i++)
            {
                BigInt temp;
                temp=multiply_(b,a.value[i]);//this因为一些奇妙的特性不是正常变量,而是指针,指针入参太难懂不易调试了 
                for(int j=i;j<=i+temp.size-1;j++)
                    ans.value[j]+=temp.value[j-i+1];
            }
            for(int i=1;i<=999;i++)
            {
                if(ans.value[i]>=10)
                {
                    ans.value[i+1]+=ans.value[i]/10;
                    ans.value[i]%=10; 
                }
            }
            for(int i=999;i>=1;i--)
            {
                if(ans.value[i]!=0)
                {
                    ans.size=i;
                    break;
                }
            }
            if(ans.size==0)
                ans.size=1;
            return ans;
        }
        BigInt divicion(BigInt a,BigInt b)
        {
            BigInt ans;
            BigInt del;
            int cnt=1;
            del=b;
            while(big_number_cmp(a,del)==1)
            {
                del=multiply_(del,10);
                cnt++;
            }
            cnt--;
            for(int i=1;i<=del.size;i++)
            {
                del.value[i]=del.value[i+1];
            }
            del.size--;
            ans.size=cnt;
            while(big_number_cmp(a,b)!=2)
            {
                if(big_number_cmp(a,del)!=2)
                {
                    //进行高精减...
                    for(int i=1;i<=a.size;i++)
                        a.value[i]-=del.value[i];
                    for(int i=1;i<=a.size;i++)
                        if(a.value[i]<0)
                        {
                            a.value[i]+=10;
                            a.value[i+1]--;
                        }
                    for(int i=999;i>=1;i--)
                    {
                        if(a.value[i]!=0)
                        {
                            a.size=i;
                            break;
                        }
                    }
                    ans.value[cnt]++;
                }
                else
                {
                    cnt--;
                    for(int i=1;i<=del.size;i++)
                    {
                        del.value[i]=del.value[i+1];
                    }
                    del.size--;
                }
            }
            return ans;
        }
        BigInt minus(BigInt a,BigInt b)
        {
            int cnt=a.size;
            if(big_number_cmp(a,b)==2)
            {
                cerr<<"在减法中结果出现负数\n";
                exit(1001);
            }
            else if(big_number_cmp(a,b)==0)
            {
                BigInt ans;
                return ans;
            }
            BigInt ans;
            for(int i=1;i<=cnt;i++)
            {
                ans.value[i]=a.value[i]-b.value[i];
            }
            for(int i=1;i<=cnt;i++)
            {
                if(ans.value[i]<0)
                {
                    ans.value[i+1]--;
                    ans.value[i]+=10;
                }
            }
            for(int i=cnt;i>=1;i--)
            {
                if(ans.value[i]!=0)
                {
                    ans.size=i;
                    break;
                }
            }
            return ans;
        }
        BigInt mod(BigInt this_,BigInt other)
        {
            BigInt quotient/*商*/=divicion(this_,other);
            BigInt ans;
            ans=minus(this_,multiply(quotient,other));
            return ans;
        }
        BigInt operator *(BigInt b)//高精乘! 
        {
            BigInt ans;
            for(int i=1;i<=this->size;i++)
            {
                BigInt temp;
                temp=multiply_(b,this->value[i]);//this因为一些奇妙的特性不是正常变量,而是指针,指针入参太难懂不易调试了 
                for(int j=i;j<=i+temp.size-1;j++)
                    ans.value[j]+=temp.value[j-i+1];
            }
            for(int i=1;i<=999;i++)
            {
                if(ans.value[i]>=10)
                {
                    ans.value[i+1]+=ans.value[i]/10;
                    ans.value[i]%=10; 
                }
            }
            for(int i=999;i>=1;i--)
            {
                if(ans.value[i]!=0)
                {
                    ans.size=i;
                    break;
                }
            }
            if(ans.size==0)
                ans.size=1;
            return ans;
        }
        BigInt operator /(BigInt other)//高精除!除为整除不保留余数
        {
            BigInt ans;
            BigInt del;
            int cnt=1;
            del=other;
            while(big_number_cmp(this,del)==1)
            {
                del=multiply_(del,10);
                cnt++;
            }
            cnt--;
            for(int i=1;i<=del.size;i++)
            {
                del.value[i]=del.value[i+1];
            }
            del.size--;
            ans.size=cnt;
            while(big_number_cmp(this,other)!=2)
            {
                if(big_number_cmp(this,del)!=2)
                {
                    //进行高精减...
                    for(int i=1;i<=this->size;i++)
                        this->value[i]-=del.value[i];
                    for(int i=1;i<=this->size;i++)
                        if(this->value[i]<0)
                        {
                            this->value[i]+=10;
                            this->value[i+1]--;
                        }
                    for(int i=999;i>=1;i--)
                    {
                        if(this->value[i]!=0)
                        {
                            this->size=i;
                            break;
                        }
                    }
                    ans.value[cnt]++;
                }
                else
                {
                    cnt--;
                    for(int i=1;i<=del.size;i++)
                    {
                        del.value[i]=del.value[i+1];
                    }
                    del.size--;
                }
            }
            return ans;
        }
        BigInt operator %(BigInt other)
        {
            BigInt quotient/*商*/=divicion(this,other);
            BigInt ans;
            ans=minus(this,multiply(quotient,other));
            return ans;
        }
        BigInt operator +=(BigInt other)
        {
            for(int i=1;i<=max(this->size,other.size);i++)
            {
                this->value[i]+=other.value[i];
            }
            for(int i=1;i<=max(this->size,other.size);i++)
            {
                if(this->value[i]>=10)
                {
                    this->value[i]-=10;
                    this->value[i+1]++;
                }
            }
            return this;
        }
        BigInt operator -=(BigInt other)
        {
            for(int i=1;i<=max(this->size,other.size);i++)
            {
                this->value[i]-=other.value[i];
            }
            for(int i=1;i<=max(this->size,other.size);i++)
            {
                if(this->value[i]<0)
                {
                    this->value[i]+=10;
                    this->value[i+1]--;
                }
            }
            return this;
        }
        BigInt operator *=(BigInt other)
        {
            BigInt ans;
            for(int i=1;i<=this->size;i++)
            {
                BigInt temp;
                temp=multiply_(other,this->value[i]);//this因为一些奇妙的特性不是正常变量,而是指针,指针入参太难懂不易调试了 
                for(int j=i;j<=i+temp.size-1;j++)
                    ans.value[j]+=temp.value[j-i+1];
            }
            for(int i=1;i<=999;i++)
            {
                if(ans.value[i]>=10)
                {
                    ans.value[i+1]+=ans.value[i]/10;
                    ans.value[i]%=10; 
                }
            }
            for(int i=999;i>=1;i--)
            {
                if(ans.value[i]!=0)
                {
                    ans.size=i;
                    break;
                }
            }
            if(ans.size==0)
                ans.size=1;
            return ans;
        }
        BigInt operator /=(BigInt other)
        {
            BigInt ans;
            BigInt del;
            int cnt=1;
            del=other;
            while(big_number_cmp(this,del)==1)
            {
                del=multiply_(del,10);
                cnt++;
            }
            cnt--;
            for(int i=1;i<=del.size;i++)
            {
                del.value[i]=del.value[i+1];
            }
            del.size--;
            ans.size=cnt;
            while(big_number_cmp(this,other)!=2)
            {
                if(big_number_cmp(this,del)!=2)
                {
                    //进行高精减...
                    for(int i=1;i<=this->size;i++)
                        this->value[i]-=del.value[i];
                    for(int i=1;i<=this->size;i++)
                        if(this->value[i]<0)
                        {
                            this->value[i]+=10;
                            this->value[i+1]--;
                        }
                    for(int i=999;i>=1;i--)
                    {
                        if(this->value[i]!=0)
                        {
                            this->size=i;
                            break;
                        }
                    }
                    ans.value[cnt]++;
                }
                else
                {
                    cnt--;
                    for(int i=1;i<=del.size;i++)
                    {
                        del.value[i]=del.value[i+1];
                    }
                    del.size--;
                }
            }
            return ans;
        }
};

BigInt创建高精度类,存储上限10^1000,有完整的计算符号重载,能直接和默认类型数据参与计算(然而这么搞容易wa).read()读入,.print()输出


by _wjr_ @ 2022-10-23 19:04:01

楼上正解


|