50分,高精度怎么搞

P1009 [NOIP1998 普及组] 阶乘之和

wang0728 @ 2022-12-11 12:55:01


#include <iostream>//22=1177652997443428940313
using namespace std;
long long c[100],sum;
int main()
{
    for(int i=0;i<100;i++)
    {
        c[i]=1;
    } 
    long long n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=i;j++)
        {
            c[i]=c[i]*j;
        }
    }
    for(int i=1;i<=n;i++)
    {
        sum=sum+c[i];
    }
    cout<<sum;
    return 0;
}

by Kevin_Mamba @ 2022-12-11 12:57:06

python


by c2209YR @ 2022-12-11 12:57:29

python了解一下? @wang0728

n=int(input())
sum=0
for i in range(1,n+1):
    num=1
    for j in range(1,i+1):
        num*=j
    sum+=num
print(sum)

by Kevin_Mamba @ 2022-12-11 12:57:52

n=int(input())
tot=0
s=1
for i in range(n) :
    s*=(i+1)
    tot+=s
print(tot)

by Nosta1enh @ 2022-12-11 13:09:36

@wang0728

https://baike.baidu.com/item/高精度算法/1024900?fr=aladdin

如果不知道某个算法,先去搜一搜!


by Alone_Moonking @ 2022-12-11 13:53:50

#include<iostream>
#include<cstring>
using namespace std;
int a[500],sum[500];
void mul(int x)//高精乘
{
    int i;
    for(i=1;i<=a[0];i++)
        a[i]*=x;
    for(i=1;i<=a[0];i++)
    {
        a[i+1]+=a[i]/10;
        a[i]%=10;
    }

    i=a[0];
    while(a[i+1]>0)
        i++;

    a[0]=i;
    i=a[0];
    while(a[i]>10)
    {
        a[i+1]+=a[i]/10;
        a[i]%=10;
        i++;
    }
    a[0]=i;
}
void add()//高精加
{
    int i;
    if(sum[0]>a[0])
        sum[0]=sum[0];
    else
        sum[0]=a[0];
    for(i=1;i<=sum[0];i++)
    {
        sum[i]+=a[i];
        sum[i+1]+=sum[i]/10;
        sum[i]%=10;
    }
    if(sum[sum[0]+1]>0)
        sum[0]+=1;
}
int main()
{
    int n;
    int i;

    cin>>n;
    a[0]=1;a[1]=1;sum[0]=1;sum[1]=0;
    for(i=1;i<=n;i++)
    {
        mul(i);//计算阶乘
        add();//计算阶乘和
    }
    for(i=sum[0];i>=1;i--)
        cout<<sum[i];
    cout<<endl;
    return 0;
}

这是我的AC代码… 不知道有没有帮助


by Katz @ 2022-12-11 14:14:16

@c2209YR 希望你在NOIP中也可以使用Python


by chengyixuan1308 @ 2022-12-20 11:09:46

namespace Bignum
{
#define bit 18/*You can change sth here.*/
#if bit<=4
    typedef int Type;
#else
    typedef long long Type;
#endif
    typedef long long ll;
    const ll bit_pow[20]={1,(ll)10,(ll)1e2,(ll)1e3,(ll)1e4,(ll)1e5,(ll)1e6,(ll)1e7,(ll)1e8,(ll)1e9,(ll)1e10,(ll)1e11,(ll)1e12,(ll)1e13,(ll)1e14,(ll)1e15,(ll)1e16,(ll)1e17,(ll)1e18};
    template <typename TP> inline void read(TP &n)
    {
        TP w=1;char x=getchar();n=0;
        while(x<'0'||x>'9'){if(x=='-')w=-1;x=getchar();}
        while(x>='0'&&x<='9'){n=(n<<1)+(n<<3)+(x^48);x=getchar();}
        n*=w;
    }
    template <typename TP> inline void write(TP x)
    {
        if(x<0) putchar('-'),x=-x;
        if(x>9) write(x/10);
        putchar(x%10+'0');
    }
    template <typename TP,typename...Etc>
    inline void read(TP &n,Etc &...etcs){read(n);read(etcs...);}
    /*BIGNUM*/
    class bignum;
    bignum abs(const bignum &);
    bignum operator - (bignum);
    bool operator ! (const bignum &);
    bool operator < (const bignum &,const bignum &);
    bool operator > (const bignum &,const bignum &);
    bool operator <= (const bignum &,const bignum &);
    bool operator >= (const bignum &,const bignum &);
    bool operator == (const bignum &,const bignum &);
    bool operator != (const bignum &,const bignum &);
    bignum operator + (const bignum &,const bignum &);
    bignum operator - (const bignum &,const bignum &);
    bignum operator * (const bignum &,const bignum &);
    bignum operator / (const bignum &,const ll &);
    bignum operator % (const bignum &,const ll &);
    bignum operator += (bignum &,const bignum &);
    bignum operator -= (bignum &,const bignum &);
    bignum operator *= (bignum &,const bignum &);
    bignum operator /= (bignum &,const ll &);
    bignum operator %= (bignum &,const ll &);
    struct bignum
    {
        vector <Type> s;
        bool negative;
        bignum()                {clear();s.push_back(0);}
        void clear()            {negative=false;s.clear();}
        void input(char k=' ')    {clear();int rest=0;do{k=getchar();if(k=='-')negative=true;}while(k<'0'||k>'9');while(k>='0'&&k<='9'){if(!rest)s.push_back(0);*(--s.end())=*(--s.end())*10+k-'0';if(++rest==bit)rest=0;k=getchar();}rest+=(!rest)*bit;for(int i=s.size()-1;i>=1;i--){s[i]+=(s[i-1]%bit_pow[bit-rest]*bit_pow[rest]);s[i-1]/=bit_pow[bit-rest];}reverse();del();}
        void output()           {if(negative) putchar('-');write(s[s.size()-1]);for(int i=s.size()-2,put;i>=0;i--){put=bit-get(s[i]);while(put-->0)putchar('0');write(s[i]);}}
        void reverse()          {for(int i=0;i<(s.size()>>1);i++){swap(s[i],s[s.size()-i-1]);}}
        void del()              {while(s.size()>1&&!(*(--s.end()))) s.pop_back();if(zero()) negative=false;}
        int get(Type x)         {int cnt=0;while(x){x/=10;++cnt;}return cnt+(!cnt);}
        int size()              {return s.size();}
        int length()            {return bit*(s.size()-1)+get(s[s.size()-1]);}
        bool odd()              {return s[0]&1;}
        bool zero()             {return (!s[0])&&(s.size()==1);}
        ll number()             {ll K=0;for(int i=s.size()-1;i>=0;i--) K=K*bit_pow[bit]+s[i];return negative?-K:K;}
        bignum(const char x)                    {*this=x;}
        bignum(const char *x)                   {*this=x;}
        bignum(const string x)                  {*this=x;}
        bignum(const unsigned long long x)      {*this=x;}
        bignum(const unsigned int x)            {*this=x;}
        bignum(const unsigned short x)          {*this=x;}
        bignum(const long long x)               {*this=x;}
        bignum(const int x)                     {*this=x;}
        bignum(const short x)                   {*this=x;}
        bignum operator = (const char x)        {clear();s.push_back(x-'0');return *this;}
        bignum operator = (const char *x)       {clear();int len=strlen(x);int rest=len%bit;s.resize((len/bit)+(len%bit!=0));rest+=(!rest)*bit;int CJB=s.size()-1;for(int i=0;i<len;i++){s[CJB]=(s[CJB]<<1)+(s[CJB]<<3)+(x[i]-'0');if(!(--rest))CJB--,rest=bit;}del();return *this;}
        bignum operator = (const string x)      {clear();int len=x.size( );int rest=len%bit;s.resize((len/bit)+(len%bit!=0));rest+=(!rest)*bit;int CJB=s.size()-1;for(int i=0;i<len;i++){s[CJB]=(s[CJB]<<1)+(s[CJB]<<3)+(x[i]-'0');if(!(--rest))CJB--,rest=bit;}del();return *this;}
        bignum operator = (unsigned long long x){clear();do{s.push_back(x%bit_pow[bit]);}while(x/=bit_pow[bit]);return *this;}
        bignum operator = (unsigned int x)      {clear();do{s.push_back(x%bit_pow[bit]);}while(x/=bit_pow[bit]);return *this;}
        bignum operator = (unsigned short x)    {clear();do{s.push_back(x%bit_pow[bit]);}while(x/=bit_pow[bit]);return *this;}
        bignum operator = (long long x)         {clear();negative=(x<0);x=std::abs(x);do{s.push_back(x%bit_pow[bit]);}while(x/=bit_pow[bit]);return *this;}
        bignum operator = (int x)               {clear();negative=(x<0);x=std::abs(x);do{s.push_back(x%bit_pow[bit]);}while(x/=bit_pow[bit]);return *this;}
        bignum operator = (short x)             {clear();negative=(x<0);x=std::abs(x);do{s.push_back(x%bit_pow[bit]);}while(x/=bit_pow[bit]);return *this;}
        bignum operator ++ ()                   {return *this+=1,*this;}
        bignum operator ++ (int)                {return *this+=1,*this-1;}
        bignum operator -- ()                   {return *this-=1,*this;}
        bignum operator -- (int)                {return *this-=1,*this+1;}
    };
    bignum operator - (bignum x)        {x.negative^=(!x.zero()); return x;}
    bool operator ! (const bignum &x)   {return x.s[0]==0&&x.s.size()==1;}
    bool operator < (const bignum &x,const bignum &y)    {if(x.negative!=y.negative)return x.negative;if(x.s.size()!=y.s.size()) return (x.s.size()<y.s.size())^x.negative;for(int i=x.s.size()-1;i>=0;i--)if(x.s[i]!=y.s[i])return (x.s[i]<y.s[i])^x.negative;return false;}
    bool operator > (const bignum &x,const bignum &y)    {return y<x;}
    bool operator <= (const bignum &x,const bignum &y)   {return !(y<x);}
    bool operator >= (const bignum &x,const bignum &y)   {return !(x<y);}
    bool operator == (const bignum &x,const bignum &y)  {if(x.negative!=y.negative||x.s.size()!=y.s.size())return false;for(int i=x.s.size()-1;i>=0;i--)if(x.s[i]!=y.s[i])return false;return true;}
    bool operator != (const bignum &x,const bignum &y)  {return !(x==y);}
    static bignum plus(const bignum &x,const bignum &y)
    {
        bignum ANS;
        ANS.s.resize(max(x.s.size(),y.s.size())+1);
        short tag=0;
        for(int i=0;i<ANS.s.size();i++)
        {
            ANS.s[i]=tag;
            if(i<x.s.size()) ANS.s[i]+=x.s[i];
            if(i<y.s.size()) ANS.s[i]+=y.s[i];
            tag=(ANS.s[i]>=bit_pow[bit]);
            ANS.s[i]-=bit_pow[bit]*tag;
        }
        ANS.del();
        return ANS;
    }
    static bignum minus(const bignum &x,const bignum &y)
    {
        bignum ANS;
        ANS.s.resize(max(x.s.size(),y.s.size()));
        short tag=0;
        for(int i=0;i<ANS.s.size();i++)
        {
            ANS.s[i]=tag;
            if(i<x.s.size()) ANS.s[i]+=x.s[i];
            if(i<y.s.size()) ANS.s[i]-=y.s[i];
            tag=-(ANS.s[i]<0);
            ANS.s[i]-=bit_pow[bit]*tag;
        }
        ANS.del();
        return ANS;
    }
    bignum operator + (const bignum &x,const bignum &y)
    {
        bignum ANS;
        if(x.negative==y.negative)
            ANS=plus(x,y),ANS.negative=x.negative;
        else
            ANS=minus(max(abs(x),abs(y)),min(abs(x),abs(y))),ANS.negative=(x.negative&&abs(x)>abs(y))||(!x.negative&&abs(y)>abs(x));
        return ANS;
    }
    bignum operator - (const bignum &x,const bignum &y)
    {
        bignum ANS;
        if(x.negative!=y.negative)
            ANS=plus(x,y),ANS.negative=x.negative;
        else
            ANS=minus(max(abs(x),abs(y)),min(abs(x),abs(y))),ANS.negative=(x.negative&&abs(x)>abs(y))||(!x.negative&&abs(y)>abs(x));
        return ANS;
    }
    bignum operator * (const bignum &x,const bignum &y)
    {
        bignum ANS;
        ANS.s.resize(x.s.size()+y.s.size());
        ANS.negative=x.negative^y.negative;
        for(int i=0;i<ANS.s.size();i++)
            ANS.s[i]=0;
        for(int i=0;i<x.s.size();i++)
        {
            for(int j=0;j<y.s.size();j++)
            {
                ANS.s[i+j]+=x.s[i]*y.s[j];
                ANS.s[i+j+1]+=ANS.s[i+j]/bit_pow[bit];
                ANS.s[i+j]%=bit_pow[bit];
            }
        }
        ANS.del();
        return ANS;
    }
    bignum operator / (const bignum &x,const ll &y)
    {
        bignum ANS;
        ANS.s.resize(x.s.size());
        ANS.negative=(x.negative)^(y<0);
        ll Klee=0;
        for(int i=x.s.size()-1;i>=0;i--)
        {
            Klee=Klee*bit_pow[bit]+x.s[i];
            ANS.s[i]=Klee/y;
            Klee%=y;
        }
        ANS.del();
        return ANS;
    }
    bignum operator % (const bignum &x,const ll &y)
    {
        ll Klee=0;
        for(int i=x.s.size()-1;i>=0;i--)
        {
            Klee=Klee*bit_pow[bit]+x.s[i];
            Klee%=y;
        }
        return x.negative?-Klee:Klee;
    }
    bignum operator += (bignum &x,const bignum &y)  {return x=x+y;}
    bignum operator -= (bignum &x,const bignum &y)  {return x=x-y;}
    bignum operator *= (bignum &x,const bignum &y)  {return x=x*y;}
    bignum operator /= (bignum &x,const ll &y)      {return x=x/y;}
    bignum operator %= (bignum &x,const ll &y)      {return x=x%y;}
#ifdef _GLIBCXX_IOSTREAM
    istream& operator >> (istream& in,bignum &x)  {x.input();return in;}
    ostream& operator << (ostream& out,bignum x)  {x.output();return out;}
#endif
    inline void read(bignum &x)                     {x.input();}
    inline void write(bignum x)                     {x.output();}
    bignum abs(const bignum &x)         {return x.negative?-x:x;}
}

|