zoukankan      html  css  js  c++  java
  • ACM模板——大数

    class HP
    {
        public : int len,s[maxn]; HP() {(*this) = 0;};
        HP(int inte) {(*this)=inte;}; HP(const char*str) {(*this)=str;};
        friend ostream& operator << (ostream &cout,const HP &x);
        HP operator = (int inte); HP operator = (const char*str);
        HP operator * (const HP &b);HP operator + (const HP &b);
        HP operator - (const HP &b);HP operator / (const HP &b);
        HP operator % (const HP &b);int Compare(const HP &b);
        bool operator < (const HP &b);
    };
    
    ostream& operator << (ostream &cout,const HP &x)
    {for(int i = x.len;i >= 1;i --) cout<<x.s[i];return cout;}
    
    HP HP::operator = (const char *str)
    {
        len = strlen(str);
        for(int i = 1;i <= len;i ++) s[i] = str[len-i]-'0';
        return (*this);
    }
    
    HP HP::operator = (int inte)
    {
        if(inte==0) {len = 1;s[1] = 0;return (*this);};
        for(len = 0;inte > 0;) {s[++len] = inte%10;inte /= 10;};
        return (*this);
    }
    
    HP HP::operator * (const HP&b)
    {
        int i,j;HP c;c.len = len+b.len;
        for(i = 1;i <= c.len;i ++) c.s[i] = 0;
        for(i = 1;i <=len;i ++) for(j = 1;j <=b.len;j ++) c.s[i+j-1]+=s[i]*b.s[j];
        for(i = 1;i < c.len;i ++) {c.s[i+1]+=c.s[i]/10;c.s[i]%=10;}
        while(c.s[i]) {c.s[i+1]=c.s[i]/10;c.s[i]%=10;i ++;}
        while(i>1&&!c.s[i]) i--;c.len = i;
        return c;
    }
    
    HP HP::operator+(const HP &b)
    {
        int i;HP c;c.s[1] = 0;
        for(i = 1;i <=len || i<=b.len || c.s[i];i ++)
        {
            if(i<=len) c.s[i]+=s[i];
            if(i<=b.len) c.s[i]+=b.s[i];
            c.s[i+1]=c.s[i]/10;c.s[i]%=10;
        }
        c.len = i-1;if(c.len==0) c.len = 1;
        return c;
    }
    
    HP HP::operator-(const HP&b)
    {
        int i, j;HP c;
        for(i = 1,j = 0;i <= len;i ++)
        {
            c.s[i] = s[i]-j;if(i<=b.len) c.s[i]-=b.s[i];
            if(c.s[i]<0){j = 1;c.s[i]+=10;}else j = 0;
        }
        c.len = len;while(c.len>1&&!c.s[c.len]) c.len--;
        return c;
    }
    
    int HP::Compare(const HP &y)
    {
        if(len>y.len) return 1;
        if(len<y.len) return -1;
        int i = len;
        while((i>1)&&(s[i]==y.s[i])) i--;
        return s[i]-y.s[i];
    }
    
    bool HP::operator < (const HP &y)
    {
        if(this->Compare(y)>=0)    return false;
        return true;
    }
    
    HP HP::operator / (const HP&b)
    {
        int i,j;HP d(0),c;
        for(i = len;i > 0;i --)
        {
            if(!(d.len==1 && d.s[1]==0))
                {for(j = d.len;j > 0;j --) d.s[j+1]=d.s[j];++d.len;}
            d.s[1] = s[i]; c.s[i] = 0;
            while((j = d.Compare(b))>=0)
                {d=d-b;c.s[i]++;if(j==0) break;}
        }
        c.len = len;while((c.len>1)&&(c.s[c.len]==0)) c.len--;
        return c;
    }
    
    HP HP::operator%(const HP&b)
    {
        int i,j;HP d(0);
        for(i = len;i > 0;i --)
        {
            if(!(d.len==1 && d.s[1]==0))
            {for(j = d.len;j > 0;j --) d.s[j+1]=d.s[j];++d.len;}
        d.s[1] = s[i];
        while((j = d.Compare(b))>=0) {d = d-b;if(j==0)break;}
        }
        return d;
    }
    
    int main()
    {
        HP a[6] {234324234,6,9,11,2,99};
        sort(a,a+6);
        _for(i,0,6)
             cout << a[i] << " ";
        cout << endl;
        return 0;
    }
    整数
    #define MAXL 50000 
    typedef long long ll;
    const int bit=1000;
    struct bign
    {
        int s[MAXL],len;
        bign()
        {
            memset(s,0,sizeof s);
            len=0;
        }
        void clean()
        {
            while(len>1&&!s[len-1]) len--;
        }
        void read()
        {
            char num[MAXL];
            scanf("%s",num);
            *this=num;
        }
        void print()
        {
            printf("%d",s[len-1]);
            for(int i=len-2; i>=0; i--)
                printf("%03d",s[i]);
            putchar(10);
        }
        bign operator=(const int &num)
        {
            int tmp=num;
            len=0;
            while(tmp)
            {
                s[len++]=tmp%bit;
                tmp/=bit;
            }
            return *this;
        }
        bign operator=(const char *num)
        {
            int up=strlen(num),g=1;
            len=0;
            for(int i=up-1; i>=0; i--)
            {
                s[len]+=(num[i]-'0')*g;
                if(g==bit/10) len++;
                g=((g!=bit/10)?g*10:1);
            }
            len++;
            clean();
            return *this;
        }
        bign operator+(const bign &b)const
        {
            bign c;
            int up=max(len,b.len);
            for(int i=0,g=0; i<up||g; i++)
            {
                if(i<len) g+=s[i];
                if(i<b.len) g+=b.s[i];
                c.s[c.len++]=g%bit;
                g/=bit;
            }
            c.clean();
            return c;
        }
        bign operator-(const bign &b)const
        {
            bign c;
            bool g=0;
            for(int i=0; i<len; i++)
            {
                int x=s[i]-g;
                if(i<b.len) x-=b.s[i];
                if(x>=0) g=0;
                else x+=bit,g=1;
                c.s[c.len++]=x;
            }
            c.clean();
            return c;
        }
        bign operator*(const bign &b)const
        {
            bign c;
            c.len=len+b.len;
            for(int i=0; i<len; i++)
                for(int j=0; j<b.len; j++)
                    c.s[i+j]+=s[i]*b.s[j];
            for(int i=0; i<c.len; i++)
                c.s[i+1]+=c.s[i]/bit,c.s[i]%=bit;
            c.clean();
            return c;
        }
        bign operator*(const int &b)const
        {
            bign c;
            c.len=len+32;
            for(int i=0; i<c.len; i++)
            {
                c.s[i]+=s[i]*b;
                c.s[i+1]+=c.s[i]/bit;
                c.s[i]%=bit;
            }
            c.clean();
            return c;
        }
        bign operator/(const bign &b)const
        {
            bign c,f;
            c.len=len;
            for(int i=len-1; i>=0; i--)
            {
                f=f*bit;
                f.s[0]=s[i];
                while(f>=b)
                    f=f-b,c.s[i]++;
            }
            c.clean();
            return c;
        }
        bign operator/(const int b)const
        {
            bign c;
            c.len=len;
            int f=0;
            for(int i=len-1; i>=0; i--)
            {
                f=f*bit+s[i];
                if(f>=b)
                    c.s[i]=f/b,f%=b;
            }
            c.clean();
            return c;
        }
        bign operator%(const bign &b)const
        {
            bign f;
            for(int i=len-1; i>=0; i--)
            {
                f=f*bit;
                f.s[0]=s[i];
                while(f>=b) f=f-b;
            }
            return f;
        }
        int operator%(const int &b)const
        {
            int f=0;
            for(int i=len-1; i>=0; i--)
            {
                f=f*bit+s[i];
                if(f>=b) f%=b;
            }
            return f;
        }
        bool operator<(const bign &b)const
        {
            if(len!=b.len) return len<b.len;
            for(int i=len-1; i>=0; i--)
                if(s[i]!=b.s[i]) return s[i]<b.s[i];
            return false;
        }
        bool operator>=(const bign &b)const
        {
            return !(*this<b);
        }
        bool operator>(const bign &b)const
        {
            if(len!=b.len) return len>b.len;
            for(int i=len-1; i>=0; i--)
                if(s[i]!=b.s[i]) return s[i]>b.s[i];
            return false;
        }
        bool operator<=(const bign &b)const
        {
            return !(*this>b);
        }
        bool operator==(const bign &b)const
        {
            if(len!=b.len) return false;
            for(int i=0; i<len; i++)
                if(s[i]!=b.s[i]) return false;
            return true;
        }
    };
    bign bign_sqrt(bign n)
    {
        bign l,r,mid,one,ans;
        r=n;
        l.s[l.len++]=1;
        one.s[one.len++]=1;
        ans=(l+r)/2;
        while(l<r)
        {
            mid=(l+r)/2;
            if(mid*mid>n) r=mid;
            else l=mid+one,ans=mid;
        }
        return ans;
    }
    整数2
    const int fprec = 100;
    
    HP zero = 0;
    class FS
    {
        public:
        FS(); void SetZero();
        FS(int inte) {(*this) = inte;}
        FS(char *s) {(*this)=s;}
        FS operator=(char *s); FS operator=(int inte);
        FS operator+(FS b); FS operator-(FS b);
        FS operator*(FS b);FS operator/(FS b);
        friend ostream& operator << (ostream &cout,FS x);
        int sign,prec;
        HP num;
    };
    
    void FS::SetZero() {sign=1;num=0;prec=0;}
    FS::FS() {SetZero();}
    
    ostream& operator<<(ostream &cout,FS x)
    {
        if(x.sign<0) cout << "-";
        int i,k,low=1;
        for(i = x.num.len;i > x.prec;i --) cout << x.num.s[i];
        if(x.num.len<=x.prec) cout << "0";
        if(x.num.Compare(zero)==0) {cout << ".0";return cout;}
        k = i;
        while(k>0 && x.num.s[k]==0) k--;
        if(k==0) {cout << ".0";return cout;}
        cout << ".";
        if(x.num.len<x.prec)for(int j = 0;j < x.prec-x.num.len;j ++)cout << "0";
        while(i>=low) cout << x.num.s[i--];
        return cout;
    }
    
    FS FS::operator=(int inte)
    {
        prec = 0;
        if(inte>=0) {sign = 1;num = inte;}
        else {sign = -1;num = -inte;}
        return (*this);
    }
    
    FS FS::operator=(char *s)
    {
        int p,i,j,l;
        SetZero();
        if(s[0]=='-') {sign = -1;s ++;};
        if(s[0]=='+') {sign = 1;s ++;};
        l = strlen(s);
        for(p = 0;p < l;p ++) if(s[p]=='.') break;
        if(p==l) prec = 0;else prec = l-1-p;
        for(i = l-1,j = 0;i >= 0;i --) if(s[i]!='.') num.s[++j] = s[i]-'0';
        while(j>1 && num.s[j]==0) --j; num.len = j;
        return (*this); 
    }
    
    void LShift(FS &a,int sl)
    {
        a.prec += sl; a.num.len+=sl; int i;
        for(i = a.num.len;i > sl;i --) a.num.s[i] = a.num.s[i-sl];
        while(i>0) a.num.s[i--] = 0;
    }
    
    void RShift(FS &a,int sl)
    {
        a.prec-=sl; a.num.len -= sl;int i;
        for(i = 1;i <= a.num.len;i ++) a.num.s[i] = a.num.s[i+sl];
    }
    
    FS FS::operator+(FS b)
    {
        FS c;
        if(prec > b.prec) LShift(b,prec-b.prec);else
        if(prec < b.prec) LShift((*this),b.prec-prec);
        if(sign==b.sign)
        {
            c.sign = sign;c.prec = prec;c.num = num+b.num;
            if(c.num.Compare(zero)==0) c.SetZero();
        }
        else
        {
            c.prec = prec;
            if(num.Compare(b.num)==0) c.SetZero();
            else if(num.Compare(b.num)>0) {c.sign = sign;c.num = num-b.num;}
            else if(num.Compare(b.num)<0) {c.sign = b.sign;c.num = b.num-num;}
            if(c.num.Compare(zero)==0) c.SetZero();
        }
        if(c.prec > fprec) RShift(c,c.prec-fprec);
        return c;
    }
    
    FS FS::operator-(FS b)
    {
        b.sign = -b.sign;
        FS c = (*this) + b;
        b.sign = -b.sign;
        return c;
    }
    
    FS FS::operator*(FS b)
    {
        FS c;
        c.sign = sign*b.sign;
        c.prec = prec+b.prec;
        c.num = num*b.num;
        if(c.num.Compare(zero)==0) c.SetZero();
        if(c.prec > fprec) RShift(c,c.prec-fprec);
        return c;
    }
    
    FS FS::operator/(FS b)
    {
        FS c,d;
        d = (*this);LShift(d,fprec);
        c.sign = d.sign*b.sign;
        c.prec = d.prec;
        LShift(d,b.prec);
        c.num = d.num/b.num;
        if(c.prec > fprec) RShift(c,c.prec-fprec);
        return c;
    }
    浮点数
    # define FOR(i, a, b) for (int i = a; i <= b; ++i)
    # define _FOR(i, a, b) for (int i = a; i >= b; --i)
    
    using namespace std;
    
    const int NR = 1100;
    
    struct BigInt {
        static const int M = 1100, Dight = 4, Base = 10000;
        // M表示数组最大长度,P表示压几位,Base = 10 ^ P.
        static char str[M << 2];
        int num[M];
        BigInt()
        {
            memset(num, 0, sizeof(num));
            num[0] = 1;
        }
    
        void read()
        {
            scanf("%s", str);
            num[0] = 0;
            int sz = strlen(str);
            for (int i = sz - 1; i >= 0; i -= Dight) {
                num[++num[0]] = 0;
                FOR(j, max(0, i - Dight + 1), i)
                    num[num[0]] = (num[num[0]] << 3) + (num[num[0]] << 1) + (str[j] ^ 48);
            }
        }
    
        void print()
        {
            printf("%d", num[num[0]]);
            _FOR(i, num[0] - 1, 1) printf("%0*d", Dight, num[i]);
            puts("");
        }
    
        bool operator < (const BigInt &cmp) const
        {
            if (num[0] != cmp.num[0]) return num[0] < cmp.num[0];
            _FOR(i, num[0], 1)
                if (num[i] != cmp.num[i]) return num[i] < cmp.num[i];
            return false;
        }
        bool operator > (const BigInt &cmp) const {return cmp < *this;}
        bool operator <= (const BigInt &cmp) const {return !(cmp < *this);}
        // bool operator != (const BigInt &cmp) const {return cmp < *this || *this < cmp;}
        // bool operator == (const BigInt &cmp) const {return !(cmp < *this || *this < cmp);}
    
        BigInt operator + (const BigInt &A) const
        {
            BigInt B;
            B.num[0] = max(A.num[0], num[0]);
            FOR(i, 1, B.num[0]){
                B.num[i] += num[i] + A.num[i];
                if (B.num[i] >= Base){
                    B.num[i] -= Base;
                    B.num[i + 1]++;
                }
            }
            while (B.num[B.num[0] + 1]) ++B.num[0];
            return B;
        }
    
        BigInt operator - (const BigInt &A) const
        {
            BigInt B;
            B.num[0] = max(A.num[0], num[0]);
            FOR(i, 1, B.num[0]){
                B.num[i] += num[i] - A.num[i];
                if (B.num[i] < 0){
                    B.num[i] += Base;
                    B.num[i + 1]--;
                }
            }
            while (B.num[0] > 1 && !B.num[B.num[0]]) --B.num[0];
            return B;
        }
    
        BigInt operator * (const int &x) const
        {
            BigInt B;
            B.num[0] = num[0];
            FOR(i, 1, num[0]){
                B.num[i] += num[i] * x;
                B.num[i + 1] += B.num[i] / Base;
                B.num[i] %= Base;
            }
            while (B.num[B.num[0] + 1]) ++B.num[0];
            return B;
        }
    
        BigInt operator * (const BigInt &A) const
        {
            BigInt B;
            B.num[0] = A.num[0] + num[0] - 1;
            FOR(i, 1, num[0])
                FOR(j, 1, A.num[0]){
                    B.num[i + j - 1] += num[i] * A.num[j];
                    B.num[i + j] += B.num[i + j - 1] / Base;
                    B.num[i + j - 1] %= Base;
                }
            while (B.num[B.num[0] + 1]) ++B.num[0];
            return B;
        }
    
        BigInt operator / (const int &x) const
        {
            BigInt B = *this;
            _FOR(i, num[0], 2){
                B.num[i - 1] += B.num[i] % x * Base;
                B.num[i] /= x;
            }
            B.num[1] /= x;
            while (B.num[0] > 1 && !B.num[B.num[0]]) --B.num[0];
            return B;
        }
    
        BigInt operator / (const BigInt &A) const
        {
            stack<BigInt> s1, s2;
            BigInt Power = A, K;
            K.num[0] = 1; K.num[1] = 1;
            while (Power <= *this) {
                s1.push(Power); s2.push(K);
                Power = Power + Power;  K = K + K;
            }
            BigInt C = *this, B;
            while (!s1.empty()) {
                if (s1.top() <= C) {
                    C = C - s1.top();
                    B = B + s2.top();
                }
                s1.pop(); s2.pop();
            }
            return B;
        }
    };
    char BigInt::str[M << 2];
    整数3
  • 相关阅读:
    SQL 获取本年第几周
    SQL Server 数据类型
    (转)经典SQL查询语句大全
    mssql查询某个值存在某个表里的哪个字段的值里面
    SQL Server 数据库还原
    SQL Server 数据库备份
    【转】T-SQL 教程
    【原】C# decimal字符串转成整数
    Django基础篇(二)与mysql配合使用
    Django基础篇(一)
  • 原文地址:https://www.cnblogs.com/Asurudo/p/10456022.html
Copyright © 2011-2022 走看看