高精度模板

来源:互联网 发布:风景线打印软件最新版 编辑:程序博客网 时间:2024/05/16 18:38

by spark:

#include<iostream>    #include<cstdio>    #include<vector>    #include<cstring>    #define LL long long     using namespace std;    struct sparkint{        static const int BASE=1000000000;        static const int width=9;        vector<LL> s;                sparkint (LL num=0){*this = num;}        sparkint operator = (LL num){            s.clear();            do{                s.push_back(num%BASE);                num/=BASE;            }while(num>0);            return *this;        }        sparkint operator = (const string& str){            s.clear();            int x,len=(str.length()-1)/width+1;            for(int i=0;i<len;i++){                int end=str.length()-i*width;                int start=max(0,end-width);                sscanf(str.substr(start,end-start).c_str(),"%d",&x);                s.push_back(x);            }            return *this;        }        //四则运算         sparkint operator + (const sparkint &b)const {            sparkint c;            c.s.clear();            for(int i=0,g=0;;i++){                if(i>=s.size()&&i>=b.s.size()&&g==0) break;                int x=g;                if(i<s.size())x+=s[i];                if(i<b.s.size()) x+=b.s[i];                c.s.push_back(x%BASE);                g=x/BASE;            }            return c;        }        sparkint operator - (const sparkint &b)const {            sparkint c;            c.s.clear();            for(int i=0,g=0;;i++){                if(i>=s.size()&&i>=b.s.size()&&g==0) break;                int x=g;                if(i<s.size()) x+=s[i];                if(i<b.s.size()) x-=b.s[i];                if(x<0) g=-1,x+=BASE;              else g=0;                c.s.push_back(x);            }            return c;        }                sparkint operator * (const sparkint &b)const {            LL i,j,g=0,cur,size=s.size()+b.s.size();LL buf[size+5];            sparkint c;            c.s.clear();          memset(buf,0,sizeof(buf));            if((*this)==0||b==0) return c;            for(i=s.size()-1;i>=0;i--)                for(j=b.s.size()-1;j>=0;j--)                buf[i+j]+=b.s[j]*s[i];        for(i=0;i<size;i++){                buf[i+1]+=buf[i]/BASE;                buf[i]%=BASE;            }            if(buf[size-1]==0)size--;          for(i=0;i<size;i++)c.s.push_back(buf[i]);           return c;        }        sparkint operator / (const sparkint &b)const {                    }        sparkint operator % (const sparkint &b)const {        }        //比较运算符         bool operator < (const sparkint &b)const {            if(s.size()!=b.s.size()) return s.size()<b.s.size();            for(int i=s.size()-1;i>=0;i--)                if(s[i]!=b.s[i]) return s[i]<b.s[i];            return false;           }        bool operator > (const sparkint &b)const {            return b<(*this);        }        bool operator == (const sparkint &b)const {            return !(*this<b) && !(b< *this);        }        bool operator <= (const sparkint &b)const {            return *this<b || *this == b;        }        bool operator >= (const sparkint &b)const {            return *this>b || *this == b;        }        bool operator != (const sparkint &b)const {            return *this<b || *this > b;        }        sparkint operator *= (const sparkint &b){    *this=(*this)*b; return *this;}   sparkint operator -= (const sparkint &b){    *this=(*this)-b; return *this;}sparkint operator += (const sparkint &b){    *this=(*this)+b; return *this;} sparkint operator ++ (int) {     *this=*this+1; return *this;} sparkint& operator ++ () {     *this=*this+1; return *this;}sparkint operator -- (int) {     *this=*this-1; return *this;} sparkint& operator -- () {     *this=*this-1; return *this;}};        ostream& operator <<(ostream &out,const sparkint& x){        out<<x.s.back();        for(int i=x.s.size()-2;i>=0;i--){            char buf[20];            sprintf(buf,"%09d",x.s[i]);            for(int j=0;j<strlen(buf);j++)out<<buf[j];        }        return out;    }    istream&  operator >> (istream &in,sparkint& x){        string s;        if(!(in>>s)) return in;        x=s;        return in;    }        int main(){    sparkint ans=1,i,n;cin>>n;for(i=1;i<=n;i++)ans=ans*i;cout<<ans<<endl;}    



0 0
原创粉丝点击