BZOJ-1002 (高精度)

我的高精度挺挫的……

#include 
#include 
#include  
using namespace std;

struct big_number{
    int s[20000],cnt;
    void upd(big_number &x){    //去掉前导零 
        while (x.s[x.cnt]==0 && x.cnt>1)
            x.cnt--;
    }
    big_number operator + (big_number b){
        big_number ret={0};
        for (int i=1,k=max(cnt,b.cnt); i<=k; i++){
            ret.s[i]+=s[i]+b.s[i];
            ret.s[i+1]+=ret.s[i]/10;
            ret.s[i]%=10;
            if (ret.s[i]<0) ret.s[i]+=10,ret.s[i+1]--;
            ret.cnt=max(ret.cnt,i);
        }
        if (ret.s[ret.cnt+1]<0)
            for (int i=1; i<=ret.cnt; i++)
                ret.s[i]-=10,ret.s[i+1]++,ret.s[i+1]+=ret.s[i]/10,ret.s[i]%=10;
        ret.cnt+=(ret.s[ret.cnt+1]!=0);
        upd(ret);
        return ret;
    }
    big_number operator * (big_number b){
        big_number ret={0};
        for (int i=1; i<=cnt; i++){
            for (int j=1; j<=b.cnt; j++){
                ret.s[i+j-1]+=s[i]*b.s[j];
                ret.s[i+j]+=ret.s[i+j-1]/10;
                ret.s[i+j-1]%=10;
                ret.cnt=max(i+j-1,ret.cnt);
            }
        }
        ret.cnt+=(ret.s[ret.cnt+1]!=0);
        upd(ret);
        return ret;
    }
    void rd(){
        char b[20000],fu=1;
        scanf("%s",b); cnt=strlen(b);
        if (b[0]=='-') fu=-1;
        for (int i=1; i<=cnt; i++) s[i]=(b[cnt-i]-'0')*fu;
        if (b[0]=='-') cnt--;
    }
    void pt(){
        int fu=1;
        if (s[cnt]<0) printf("-"),fu=-1;
        for (int i=cnt; i; i--) printf("%d",s[i]*fu);
        puts("");
    }
} x,x1,x2,y,z,k1,k2,k3;

int main(){
    int n=(scanf("%d",&n),n);
    if (n==1){puts("1"); return 0;}
    if (n==2){puts("5"); return 0;}
    k1.s[1]=3,k1.cnt=1;
    k2.s[1]=-1,k2.cnt=1;
    k3.s[1]=2,k3.cnt=1;
    //f[i]=(f[i-1]*3-f[i-2]+2)
    z.s[1]=1;y.s[1]=5; z.cnt=y.cnt=1;
    for(int i=3;i<=n;i++){
        x=y*k1+z*k2+k3;
        z=y,y=x;
    }
    y.pt();
}

你可能感兴趣的:(题目)