高精度怎么做

P1009 [NOIP1998 普及组] 阶乘之和

Pearls @ 2024-04-09 16:31:32

谁能帮帮我,我就是个小学五年级的同学,高精度看不懂啊


by Weizhuo_Zhao @ 2024-04-09 16:39:15

@wwyzzf 字符串


by Weizhuo_Zhao @ 2024-04-09 16:40:34

打表AC:

#include <iostream>
using namespace std;
int main(){
    string a[]={"1","3","9","33","153","873","5913","46233","409113","4037913","43954713","522956313","6749977113","93928268313","1401602636313","22324392524313","378011820620313","6780385526348313","128425485935180313","2561327494111820313","53652269665821260313","1177652997443428940313","27029669736328405580313","647478071469567844940313","16158688114800553828940313","419450149241406189412940313","11308319599659758350180940313","316196664211373618851684940313","9157958657951075573395300940313","274410818470142134209703780940313","8497249472648064951935266660940313","271628086406341595119153278820940313","8954945705218228090637347680100940313","304187744744822368938255957323620940313","10637335711130967298604907294846820940313","382630662501032184766604355445682020940313","14146383753727377231082583937026584420940313","537169001220328488991089808037100875620940313","20935051082417771847631371547939998232420940313","836850334330315506193242641144055892504420940313","34289376947494122614363304694584807557656420940313","1439295494700374021157505910939096377494040420940313","61854558558074209658512637979453093884758552420940313","2720126133346522977702138448994068984204397080420940313","122342346998826717539665299944651784048588130840420940313","5624964506810915667389970728744906677010239883800420940313","264248206017979096310354325882356886646207872272920420940313","12678163798554051767172643373255731925167694226950680420940313","620960027832821612639424806694551108812720525606160920420940313","31035053229546199656252032972759319953190362094566672920420940313"};
    int b;
    cin >> b;
    cout<<a[b-1]<<'\n';
    return 0;
}

by kevinZ99 @ 2024-04-09 17:05:54

@wwyzzf 最简单的就是把语言换成 python py 自带高精


by Diode123 @ 2024-04-09 17:10:22

简单的高精度其实像是对竖式乘法,竖式加法的模拟,只是因为是对字符串的操作才要复杂些,具体的可以看这里


by canwen @ 2024-04-11 21:47:44

@wwyzzf 我初一,反正看不懂大佬那些精简的代码,我的思路如下: 枚举1~n,然后求n的阶乘,再用sum存起来。重点在于n的阶乘如何求:可以先用两个数组存起a和b,a是上次阶乘的结果(初始1),b是短的因数(1~50),然后模拟高精度。记得每次循环用memset(0,shuzu,sizeof(shuzu))初始化b就行了,阶乘结果用ans【】存起来方便下次使用 我的代码比较笨比较也是萌新,能看就凑合着看(doge 打了一个小时。。

#include<iostream>
#include<cstring>
using namespace std;
long long ans[100000],sum[100000],small[3],lasttemp=0,linshi[100000],maxtemp=0;
int main(){
    int num;cin>>num;int numb=1;
//lasttmp乘数的位数  
for(numb=1;numb<=num;numb++){
    for(int i=1;i<=numb;i++){
        if(i==1){
            ans[1]=1;lasttemp=1;continue;
        }
        memset(small,0,sizeof(small));
        int j=i,tmp=0;
        while(j!=0){
            small[++tmp]=j%10;
            j/=10;
        }
        memset(linshi,0,sizeof(linshi));
        for(int j=1;j<=tmp;j++){
            for(int i=1;i<=lasttemp;i++){
                linshi[i+j-1]+=ans[i]*small[j];
            }
        }
        int most=tmp+lasttemp;
        for(int i=1;i<most;i++){
            if(linshi[i]>=10){
                linshi[i+1]+=linshi[i]/10;
                linshi[i]%=10;      
            }
        }
        while(linshi[most]==0&&most>1) most--;
        lasttemp=most;
        memset(ans,0,sizeof(ans));
        for(int i=1;i<=most;i++){
           ans[i]=linshi[i];
        }
    }
    //和的计算
    if(lasttemp>maxtemp) maxtemp=lasttemp; 
    for(int i=1;i<=lasttemp;i++){
        sum[i]+=ans[i];
    } 
    for(int i=1;i<=maxtemp;i++){
        if(sum[i]>=10){
            sum[i+1]+=sum[i]/10;
            sum[i]=sum[i]%10;
        }
    }
    while(sum[maxtemp]==0&&maxtemp>1) maxtemp--;
for(int i=maxtemp;i>=1;i--) cout<<sum[i]; 
cout<<endl;
}
maxtemp++;
while(sum[maxtemp]==0&&maxtemp>1) maxtemp--;
for(int i=maxtemp;i>=1;i--) cout<<sum[i]; 
}

by canwen @ 2024-04-11 21:48:58

@canwen2 倒数第六到第八行是为了测试用的,提交时删掉就行


by cmz9690 @ 2024-04-11 22:46:35

我小学五年级拿了普及组省一(去年


by cmz9690 @ 2024-04-11 22:47:16

@wwyzzf


|