BIG INT

/*
    Author:  No Name
    Date  :  2016/3/5
*/

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <string>
#inlcude "myalgo.h"

#define ull unsigned long long
#define INTe18 1000000000000000000
#define INT2_e18 2000000000000000000

using namespace std;

struct Big_u {
    ull *begin,*high,*end;
    inline unsigned size() {
        return high-begin;
    }
    inline unsigned lenth() {
        return end-begin;
    }
    inline unsigned space() {
        return end-high+1;
    }
    Big_u () {
        begin=new ull[16];
        end=begin+16;
        for(high=begin; high!=end; ++high) {
            *high=0;
        }
        high=begin+4;
    }
    Big_u (const ull &n) {
        begin=new ull[16];
        end=begin+16;
        for(high=begin+1; high!=end; ++high) {
            *high=0;
        }
        high=begin+4;
        *begin=n>>32;
    }
    Big_u (const Big_u& n) {
        begin=new ull[(n.end-n.begin)];
        end=begin+(n.end-n.begin);
        high=begin+(n.high-n.begin);
        ull* i1=begin,*i2=n.begin;
        while(i1!=end) {
            *(i1++) = *(i2++);
        }
    }
    ~Big_u() {
        delete[] begin;
    }
    inline ull &operator [](unsigned i){
        return begin[i];
    }
    void rehigh() {
        for(high=end; high>begin && *--high==0;);
    }
    void enlarge_size() {
        ull newone= new ull[size()*2];
        int a=0;
        while(a<lenth()) newone[a]=begin[a++];
        while(a<size()) newone[a++]=0;
        delete[] begin;
        high=newone+lenth();
        end=newone+2*size();
        begin=newone;
    }
    void reduce_size() {
        if(size()==1) return;
        ull newone= new ull[size()/2];
        for(int a=0; a<lenth(); ++a) {
            newone[a]=begin[a];
        }
        delete[] begin;
        high=newone+lenth();
        end=newone+size()/2;
        begin=newone;
    }
    void getsize(unsigned s){
        delete[] begin;
        begin=new ull[s];
        end=begin+s;
        high=begin;
    }
    Big_u& operator =(const Big_u& a){
        begin=a.begin;
        end=a.end;
        high=a.high;
        return *this;
    }
};
void rs_add(Big_u& a){
    if(*(a.end-1)==0xffffffffffffffff) a.enlarge_size();
}
void rs_add(Big_u& a,Big_u& b){
    while(a.end==a.high||a.end<=b.high) a.enlarge_size();
}
void rs(Big_u& a){
    if(a.lenth()<=a.size()/4) a.reduce_size();
}
void rs_mult(Big_u &a){
    if(a.size()==a.lenth()) a.reduce_size();
}


Big_u &operator +=(Big_u& a,Big_u &b){
    rs_add(a,b);
    bool up=false;
    int i;
    for(i=0;i<b.size();++i){
        if(up) up=(++a[i]==0);
        else up=false;
        if((a[i]+=b[i])<b[i]) up=true;
    }
    if(up) {
        a[i]=1;
        a.high++;
    }
    return a;
}
Big_u operator +(Big_u& a,Big_u& b){
    Big_u r=a;
    r+=b;
    return r;
}
Big_u &mult_ul(Big_u &x,unsigned long that) {
    ull a=0,b=0,c=0;
    ull* end=x.end;
    if(x.high=1)
    for(ull *i=begin; i!=end;) {
        (a=((*--i)>>32))*=that;
        ((*i)&=F_32)*=that;
        b=(a>>32);
        a<<=32;
        if(((*i+=c)+=a)<a) ++b;
        (a=((*--i)>>32))*=that;
        ((*i)&=F_32)*=that;
        c=(a>>32);
        a<<=32;
        if(((*i+=b)+=a)<a) ++c;
    }
    return *this;
}

Big_u &operator *=(Big_u& a,ull b){
    
}


istream& operator>>(istream& in,Big_u &x) {
    string str;
    in>>str;
    x=0;
    for(unsigned i=0; str[i]; ++i) {
        x*=10;
        x+=(str[i]-48);
    }
    return in;
}

 

posted @ 2016-08-05 18:14  141421356  阅读(185)  评论(0)    收藏  举报