壓位高精度模板

2022-07-31 06:57:12 字數 3991 閱讀 6197

不走程式,直接上板子。

第乙個板子壓四位,支援帶符號的加減乘。

第二個板子壓九位,支援不帶符號的四則運算和取模。

都封裝了。

#include #include #include using namespace std;

struct intx

bool operator<(const intx &b) const

bool operator<=(const intx &b) const

bool operator>=(const intx &b) const

bool operator!=(const intx &b) const

bool operator==(const intx &b) const

intx operator+(const intx &b) const

if(c.xb[c.xb[0]+1]>0) c.xb[0]++;

return c;

} intx operator-(const intx &b) const

else if(d<0)

else if(e<0)

else if(e1) c.xb[0]--;

return c;

} intx& operator+=(const intx &b)

intx& operator-=(const intx &b)

intx operator*(const intx &b) const

intx operator*=(const intx &b)

void readx(intx &b)

void writex()

};int main()

#include#include#includeusing namespace std;

const int len=10000+1,bit=9;

struct intx

intx& operator=(const char *num)

return *this;

}intx& operator=(int num)

intx(int n)

intx(const char *n)

bool operator>(const intx &a) const

bool operator<(const intx &a) const

bool operator<=(const intx &a) const

bool operator>=(const intx &a) const

bool operator!=(const intx &a) const

bool operator==(const intx &a) const

intx operator+(const intx &a) const

intx& operator+=(const intx &a)

intx operator-(const intx &a) const

intx& operator-=(const intx &a)

intx operator*(const intx &a) const

while(c.s[c.s[0]]>0) c.s[0]++;

while(c.s[c.s[0]]==0 && c.s[0]>1) c.s[0]--;

return c;

}intx& operator*=(const intx &a)

intx operator<<(const int &num)

while(s[s[0]]==0 && s[0]>1) s[0]--;

return *this;

}intx operator>>(const int &num)

while(s[s[0]]==0 && s[0]>1) s[0]--;

return *this;

}intx operator/(const intx &k) const

c=lt;

while(c.s[c.s[0]]==0 && c.s[0]>1) c.s[0]--;

if(c.s[0]<1) c.s[c.s[0]=1]=0;

return c;

}intx& operator/=(const intx &a)

intx operator%(const intx &a) const

intx& operator%=(const intx &a)

};ostream& operator<<(ostream &out,const intx &a)

istream& operator>>(istream &in,intx &a)

upd:距離第一次寫就這篇文章已有5個月,在此期間遇到了很多需要高精的題,才發現之前的模板有很多的缺陷,壓位和多種運算固然不錯,但獨立性太差,碼量過多與基於自損相減的除法運算複雜度過高還是硬傷,於是這次更新了新的wint

新的寫法基於vector實現,碼量--,除法換成了高精除低精,並且各運算相對獨立,在實際應用中表現更好。

#include #include #include struct wint:vector

wint& check()

return *this;

}};bool operator!=(const wint &a,const wint &b)

bool operator==(const wint &a,const wint &b)

bool operator<(const wint &a,const wint &b)

bool operator>=(const wint &a,const wint &b)

return a.check();

}wint operator-(wint a,const wint &b)

wint operator*(const wint &a,const wint &b)

reverse(n.begin(),n.end());

return n;

}wint& operator/=(wint &a,const int &b)

void readx(wint &n)

void writex(wint n)

upd2:又經過了許久,我的高精度最終版終於誕生(怎麼這麼中二)。

新版在之前的wint基礎上再改良,變成了壓 9 位的高精,用vector也不怕被卡了!

為了與壓位相匹配,實現了新的賦值函式,支援大整數讀入。

我持續了半年的偉業終於完成(

typedef long long ll;

struct wint:vector

wint& operator=(const char* num)

return *this;

}wint& check()

return *this;

}};bool operator<(wint a,wint b)

bool operator>=(wint a,wint b)

return a.check();

}wint operator-(wint a,wint b)

wint operator*(wint a,wint b)

reverse(n.begin(),n.end());

return n;

}wint& operator/=(wint &a,int b)

void readx(wint &n)

void writex(wint n)

int len=n.size()-1; printf("%lld",n[len]);

for(int i=len-1;i>=0;--i) printf("%09lld",n[i]);

}

壓位高精度模板

原先是整型陣列每個元素存1個數字,壓位高精是每位存8個數字,這樣可以加速8唄,空間也減小了。可謂是對整形陣列的充分利用。include include include include include include include include include include include in...

壓位高精度的寫法

2020 06 10 修正 html 原始碼 2021 12 25 發現這篇隨筆閱讀量破千了,二次修正 html 原始碼,並做了相關補充 壓位高精度的寫法 對於單位高精度演算法,那麼有沒有可以加速 節省空間的做法呢?顯然是有的。以前,存數字陣列裡面只存著乙個數字,所以加減都是一位一位地加,進製也是一...

高精度模板 過載運算子 壓位

昨天做一道dp的題 矩陣取數遊戲 本來是很簡單的,但是要用高精度,又不想用 int128水過去 誰讓noip不讓 於是自己打了乙個小時,最後成功掛了。於是本蒟蒻痛定思痛,感覺高精度還是過載運算子好用啊,就花了幾個小時打了乙個自以為比較好記好用高精度模板 注意暫不支援負數,如果發現有bug歡迎指出。採...