求助!不知道咋改高精度

P1009 [NOIP1998 普及组] 阶乘之和

easonireading @ 2023-08-11 11:17:24

#include <iostream>
#include <stdio.h>
using namespace std;

int main(){
    int n;
    cin>>n;
    long long m=1,s=0;
    for (int i=1;i<=n;i++){
        for
    }
    cout<<s;
    return 0;
}

by easonireading @ 2023-08-11 11:19:00

只有50分QwQ


by KidzzZip @ 2023-08-11 11:22:44

用数组模拟乘法竖式


by Weizhuo_Zhao @ 2023-08-11 11:25:21

@easonireading 不知道怎么改高精可以打表!

#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 t;
    cin >> t;
    cout<<a[t-1]<<endl;
    return 0;
}

by tZEROちゃん @ 2023-08-11 11:27:24

@easonireading 草你这代码都没写完为啥会有 50 pts 啊


by easonireading @ 2023-08-11 16:09:44

@tZEROちゃん

SORRY,我不小心把代码删成这样了 ,请原谅

代码应该是:

#include <iostream>
using namespace std;

int main(){
    int n;
    cin>>n;
    long long m=1,s=0;
    for (int i=1;i<=n;i++){
        m=m*i;
        s=s+m;
    }
    cout<<s;
    return 0;
}

by easonireading @ 2023-08-11 16:11:01

@Weizhuo_Zhao

谢谢


by ThEskY_B1ackeR @ 2023-08-17 20:03:41

@easonireading


#include<bits/stdc++.h>
using namespace std;
int num[105],ans[105],n;
//num数组用于存储当前阶乘的大小,ans数组用于存储答案
void mcl(int x){//乘以x
    int a=0;
    for(int i=1;i<=100;i++){
        num[i]=num[i]*x+a;
        a=num[i]/10;
        num[i]%=10; 
    }
}
void add(){//加上新的阶乘
    for(int i=1;i<=100;i++){
        ans[i]+=num[i];
        ans[i+1]+=ans[i]/10;
        ans[i]%=10;
    }
}
int main(){
    cin>>n;
    ans[1]=num[1]=1;//初始化
    for(int i=2;i<=n;i++){
        mcl(i);
        add();
    }
    bool flag=1;//默认没有出现数字,不输出
    for(int i=100;i>=1;i--){//倒序输出
        if(flag&&ans[i]==0)continue;//如果没有出现数字就跳过
        else{
            flag=0;//数字出现
            cout<<ans[i];
        }
    }
    return 0;
} 

by forest1145141919810 @ 2023-08-22 20:44:01

@easonireading 你可以像我这样写,这样不容易错,而且很美观

#include<bits/stdc++.h>
using namespace std;
struct Num{
    #define LEN 105
    int num[LEN],cnt,f;
    inline int getnum(int x[],int cnt,int f){
        int num=0;
        for(int i=cnt;i>=1;i--) 
            num=num*10+x[i];
        return num;
    }
    inline void copy(int x1[], int cnt1, int f1, int x2[], int& cnt2, int& f2) {//把高精度数x1完全复制给高精度数ans
        for (int i = 1; i <= cnt1; i++) x2[i] = x1[i];
        cnt2 = cnt1; f2 = f1;
    }template<typename T>
    inline void copynum(int x[],int &cnt,int &f,T num){
        if(num==0) {x[1]=0,cnt=1,f=1;return;}
        if(num<0) f=-1,num=-num;else f=1;cnt=0;
        while(num>0) x[++cnt]=num%10,num/=10;
        while(x[cnt]==0&&cnt>0) cnt--;
        if(cnt==0) x[++cnt]=0;
    }
    inline bool ismax(int x1[], int cnt1, int f1, int x2[], int cnt2, int f2) {//判断数x1是不是更大
        if (f1 > f2) return true;
        else if (cnt1 > cnt2) return true;
        else if (f1 < f2) return false;
        else if (cnt1 < cnt2) return false;
        for (int i = cnt1; i >= 1; i--){
            if (x1[i] < x2[i]) return false;
            else if(x1[i] > x2[i]) return true;
        }
        return true;
    }
    inline bool ismin(int x1[], int cnt1, int f1, int x2[], int cnt2, int f2) {//判断数x1是不是更小
        if (f1 > f2) return false;
        else if (cnt1 > cnt2) return false;
        else if (f1 < f2) return true;
        else if (cnt1 < cnt2) return true;
        for (int i = cnt1; i >= 1; i--){
            if (x1[i] > x2[i]) return false;
            else if(x1[i] < x2[i]) return true;
        } 
        return true;
    }
    inline void add(int x1[],int cnt1,int x2[],int cnt2,int ans[],int &cnt){
        int anscnt=cnt;int anst[LEN];int x=0;
        for(int i=1;i<=cnt;i++) anst[i]=ans[i];
        int len=max(cnt1,cnt2);anscnt=0;
        if(len==cnt1) for(int i=cnt2+1;i<=len;i++) x2[i]=0;
        else for(int i=cnt1+1;i<=len;i++) x1[i]=0;
        for(int i=1;i<=len;i++){
            anst[++anscnt]=(x1[i]+x2[i]+x)%10;
            x=(x1[i]+x2[i]+x)/10;
        }
        if(x>0) anst[++anscnt]=x;
        while(!anst[anscnt]&&anscnt>0) anscnt--;
        if(anscnt==0) anscnt=1;cnt=anscnt;
        for(int i=1;i<=anscnt;i++) ans[i]=anst[i];
    }
    inline void minus(int x1[],int cnt1,int x2[],int cnt2,int anst[],int &cnt,int &ansf){
        int cntans=cnt;int ans[LEN];int f=ansf;
        for(int i=1;i<=cnt;i++) ans[i]=anst[i];
        if (ismin(x1, cnt1, 1, x2, cnt2, 1)) {
            copy(x2, cnt2, 1, ans, cntans, f); f = -f;
            for (int i = cnt1; i >= 1; i--) {
                if (ans[i] < x1[i]) {
                    int cnt = i + 1; ans[i] = ans[i] + 10 - x1[i];
                    while (ans[cnt] == 0 && cnt <= cntans) ans[cnt] = 9, cnt++;
                    ans[cnt]--; while (ans[cntans] == 0) cntans--;
                }
                else ans[i] -= x1[i];
            }while (cntans > 0 && ans[cntans] == 0) cntans--; if (cntans == 0) cntans++;
        }
        else {
            copy(x1, cnt1, 1, ans, cntans, f);
            for (int i = cnt2; i >= 1; i--) {
                if (ans[i] < x2[i]) {
                    int cnt = i + 1; ans[i] = ans[i] + 10 - x2[i];
                    while (ans[cnt] == 0 && cnt <= cntans) ans[cnt] = 9, cnt++;
                    ans[cnt]--; while (ans[cntans] == 0) cntans--;
                }
                else ans[i] -= x2[i];
            }while (ans[cntans] == 0) cntans--; if (cntans == 0) cntans++;
        }
        cnt=cntans;ansf=f;if(cnt==1&&ans[1]==0) ansf=1;
        for(int i=1;i<=cntans;i++) anst[i]=ans[i];
    }
    inline void multiply(int x1[],int cnt1,int f1,int x2[],int cnt2,int f2,int ans[],int &cnt,int &f){
        f=f1*f2;cnt=cnt1+cnt2+1;int anst[LEN],ansf=f,cntans=cnt;
        for(int i=1;i<=cntans;i++) anst[i]=0;
        for(int i=1;i<=cnt1;i++){
            int temp=0;
            for(int j=1;j<=cnt2;j++){
                anst[i+j-1]=(x1[i]*x2[j]+temp+anst[i+j-1]);
                temp=anst[i+j-1]/10;
                anst[i+j-1]%=10;
            }
            anst[i+cnt2]=temp;
        }
        while(anst[cntans]==0&&cntans>0) cntans--;
        if(cntans==0) cntans=1,ansf=1;
        for(int i=1;i<=cntans;i++) ans[i]=anst[i];
        cnt=cntans;f=ansf;return;
    }
    inline void operator += (Num b){
        if (f < 0 && b.f>0) minus(b.num, b.cnt, num, cnt, num, cnt, f);
        else if (f > 0 && b.f < 0) minus(num, cnt, b.num, b.cnt, num, cnt, f);
        else {add(num, cnt, b.num, b.cnt, num, cnt); }
    }
    inline void operator -= (Num b){
        if (b.f < 0 && f>0) { f = 1; add(num, cnt, b.num, b.cnt, num, cnt); }
        else if (f < 0 && b.f>0) { f = -1; add(num, cnt, b.num, b.cnt, num, cnt); }
        else if (f > 0 && b.f > 0) minus(num, cnt, b.num, b.cnt, num, cnt, f);
        else if (f < 0 && b.f < 0) minus(b.num, b.cnt, num, cnt, num, cnt, f);
    }
    inline void operator *= (Num b){multiply(num,cnt,f,b.num,b.cnt,b.f,num,cnt,f);}
    inline Num operator + (Num b){
        Num c;if (f < 0 && b.f>0) minus(b.num, b.cnt, num, cnt, c.num, c.cnt, c.f);
        else if (f > 0 && b.f < 0) minus(num, cnt, b.num, b.cnt, c.num, c.cnt, c.f);
        else {add(num, cnt, b.num, b.cnt, c.num, c.cnt); }return c;
    }
    inline Num operator - (Num b){
        Num c;if (b.f < 0 && f>0) { f = 1; add(num, cnt, b.num, b.cnt, c.num, c.cnt); }
        else if (f < 0 && b.f>0) { f = -1; add(num, cnt, b.num, b.cnt, c.num, c.cnt); }
        else if (f > 0 && b.f > 0) minus(num, cnt, b.num, b.cnt, c.num, c.cnt, c.f);
        else if (f < 0 && b.f < 0) minus(b.num, b.cnt, num, cnt, c.num, c.cnt, c.f);
        return c;
    }
    inline Num operator * (Num b){
        Num c;
        multiply(num,cnt,f,b.num,b.cnt,b.f,c.num,c.cnt,c.f);
        return c;
    }
    inline void operator = (Num b){copy(b.num,b.cnt,b.f,num,cnt,f);}
    template<typename T>inline void operator = (T b){copynum(num,cnt,f,b);}
    inline void operator ++ (){
        Num c;c=1;
        if (f < 0 && c.f>0) minus(c.num, c.cnt, num, cnt, num, cnt, f);
        else if (f > 0 && c.f < 0) minus(num, cnt, c.num, c.cnt, num, cnt, f);
        else {add(num, cnt, c.num, c.cnt, num, cnt); }
    }
    inline void operator -- (){
        Num c;c=-1;
        if (f < 0 && c.f>0) minus(c.num, c.cnt, num, cnt, num, cnt, f);
        else if (f > 0 && c.f < 0) minus(num, cnt, c.num, c.cnt, num, cnt, f);
        else {add(num, cnt, c.num, c.cnt, num, cnt); }
    }
    inline bool operator >= (Num b){return ismax(num,cnt,f,b.num,b.cnt,b.f);}
    template<typename T>inline bool operator >= (T b){
        Num c;c=b;return ismax(num,cnt,f,c.num,c.cnt,c.f);
    }
    inline bool operator <= (Num b){return ismin(num,cnt,f,b.num,b.cnt,b.f);}
    template<typename T>inline bool operator <= (T b){
        Num c;c=b;return ismin(num,cnt,f,c.num,c.cnt,c.f);
    }
    inline bool operator == (Num b){
        return ismax(num,cnt,f,b.num,b.cnt,b.f)&&ismin(num,cnt,f,b.num,b.cnt,b.f);
    }
    template<typename T>inline bool operator == (T b){
        Num c;c=b;
        return ismin(num,cnt,f,c.num,c.cnt,c.f)&&ismax(num,cnt,f,c.num,c.cnt,c.f);
    }
    inline bool operator > (Num b){return !ismin(num,cnt,f,b.num,b.cnt,b.f);}
    inline bool operator < (Num b){return !ismax(num,cnt,f,b.num,b.cnt,b.f);}
    template<typename T>inline bool operator > (T b){
        Num c;c=b;return !ismin(num,cnt,f,c.num,c.cnt,c.f);
    }
    template<typename T>inline bool operator < (T b){
        Num c;c=b;return !ismax(num,cnt,f,c.num,c.cnt,c.f);
    }
    inline bool operator ! (){
        Num c;copy(num,cnt,f,c.num,c.cnt,c.f);
        if(c==0) return true;
        return false;
    }
};
inline int GetNum(Num a){return a.getnum(a.num,a.cnt,a.f);}
inline void init(Num &a){a.cnt=1;a.f=1;}
inline void read(Num &a){
    a.cnt=0;a.f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-') a.f=-a.f;ch=getchar();}
    while(ch>='0'&&ch<='9'){a.num[++a.cnt]=ch-'0';ch=getchar();}
    for(int i=1;i<=a.cnt/2;i++) swap(a.num[i],a.num[a.cnt-i+1]);
}
inline void write(Num x){
    if(x.f<0) putchar('-');
    for(int i=x.cnt;i>=1;i--) 
        putchar(x.num[i]+'0');
    putchar('\n');return;
}
#define int Num
#define long long Num
#define __int128 Num
#define ui Num
#define ull Num
int s,n;
signed main(){
    read(n);s=0;int i;
    for(i=1;i<=n;++i){
        int jie,j;jie=1;
        for(j=1;j<=i;++j) jie*=j;
        s+=jie;
    }write(s);
    return 0;
}

by forest1145141919810 @ 2023-08-22 20:44:13

@easonireading


|