高精度

巨佬学长 zyf 写的一份高精。

使用方法:使用 bign 定义高精度,maxlen 代表最大位数,好像最大 20000,不然 MLE+TLE+RE+CE。

读入高精要在快读时处理。

namespace zyf{ struct bign{ static const int maxlen=200,width=8; static const long long limit=100000000LL; long long len,bit[maxlen]; long long& operator[](int p){ return bit[p]; } void ClearBit(){ memset(bit,0,sizeof(bit)); } void Delete0(){ for(;!bit[len-1] && len>1;--len); } bign(int p=0){ *this=p; } bign& operator=(int p){ ClearBit(); len=p?0:1; for(;p;p/=limit) bit[len++]=p%limit; return *this; } bign(const char *p){ *this=p; } bign& operator+=(bign b){ len=max(len,b.len)+1; for(int i=0;i<len;++i) bit[i]+=b[i],bit[i+1]+=bit[i]/limit,bit[i]%=limit; Delete0(); return *this; } bign& operator-=(bign b){ for(int i=0;i<len;++i){ bit[i]-=b[i]; if(bit[i]<0) bit[i]+=limit,--bit[i+1]; } Delete0(); return *this; } bign& operator*=(bign b){ bign a=*this; ClearBit(); len=a.len+b.len; for(int i=0;i<a.len;++i) for(int j=0;j<b.len;++j) bit[i+j]+=a[i]*b[j],bit[i+j+1]+=bit[i+j]/limit,bit[i+j]%=limit; Delete0(); return *this; } bign& operator*=(int b){ ++len; long long t=0; for (int i=0;i<len;++i) bit[i]=t+bit[i]*b,t=bit[i]/limit,bit[i]%=limit; Delete0(); return *this; } bign& operator/=(int b){ for(int i=len-1;i>0;--i) bit[i-1]+=limit*(bit[i]%b),bit[i]/=b; bit[0]/=b; Delete0(); return *this; } bool operator<(bign b) const{ if(len>b.len) return false; if(len<b.len) return true; for(int i=len-1;i>=0;--i) if(bit[i]!=b[i]) return bit[i]<b[i]; return bit[0]<b[0]; } bool operator==(bign b) const{ return !(*this<b) && !(b<*this); } bool operator!=(bign b) const{ return !(*this==b); } bool operator>(bign b) const{ return !(*this<b) && !(*this==b); } bool operator<=(bign b) const{ return *this<b || *this==b; } bool operator>=(bign b) const{ return *this>b || *this==b; } bool odd(){ return bit[0]%2==1; } bool even(){ return bit[0]%2==0; } }; bign operator+(bign a,bign b){ return a+=b; } bign operator-(bign a,bign b){ return a-=b; } bign operator*(bign a,bign b){ return a*=b; } bign operator*(bign a,int b){ return a*=b; } bign operator/(bign a,int b){ return a/=b; } istream& operator>>(istream &is,bign &p){ string s; is>>s; p=s.c_str(); return is; } ostream& operator<<(ostream &os,bign p){ os.fill('0'); os<<p.bit[p.len-1]; for(int i=p.len-2;i>=0;--i){ os.width(bign::width); os<<p.bit[i]; } return os; } bign sqrt(bign x){ bign head=1,tail=x; while(head<=tail){ bign mid=(head+tail)/2; if(x<mid*mid) tail=mid-1; else head=mid+1; } return tail; } bign gcd(bign a,bign b){ bign gcd=1; while(a!=b){ if(a<b) swap(a,b); if(a.even() && b.even()) gcd*=2,a/=2,b/=2; else if(a.even() && b.odd()) a/=2; else if(a.odd() && b.even()) b/=2; else a=a-b; } return gcd*=a; } } using zyf::bign;

__EOF__

本文作者JMartin
本文链接https://www.cnblogs.com/LAK666/p/16438026.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   Epoch_L  阅读(142)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示