标签:blog ar sp for div on log bs amp
这是写给自己看的模板……当然有需要的人也可以直接拿走
1、封装好的高精度模板
注意:算法的主要耗时在于高精度乘法、除法和乘方。其中高精度乘方有快速幂优化,常数大致是乘法的5~10倍。除法是用二分写的效率较低,大概是乘法的20~30倍,能不用尽量不用
#define mx 300 struct gaojing{ int len; int a[mx+10]; }zero,one; inline void set0(gaojing &s)//高精清零 { s.len=1; for (int i=1;i<=mx+5;i++)s.a[i]=0; } inline void inputn(gaojing &a)//高精输入 { set0(a); char ch=getchar(); while (ch<‘0‘||ch>‘9‘)ch=getchar(); while (ch>=‘0‘&&ch<=‘9‘) { a.a[a.len++]=ch-‘0‘; ch=getchar(); } a.len--; int change[mx+15]; for (int i=1;i<=a.len;i++) change[i]=a.a[i]; for (int i=1;i<=a.len;i++) a.a[i]=change[a.len-i+1]; while (a.a[a.len]==0)a.len--; } inline void put(gaojing a)//高精输出 { for (int i=a.len;i>=1;i--)printf("%d",a.a[i]); printf("\n"); } inline bool operator < (const gaojing &a,const gaojing &b)//高精< { if (a.len<b.len)return 1; if (a.len>b.len)return 0; for (int i=a.len;i>=1;i--) { if (a.a[i]<b.a[i])return 1; if (a.a[i]>b.a[i])return 0; } return 0; } inline bool operator == (const gaojing &a,const gaojing &b)//高精== { if (a.len!=b.len)return 0; for (int i=a.len;i>=1;i--) { if (a.a[i]!=b.a[i])return 0; } return 1; } inline gaojing max(const gaojing &a,const gaojing &b)//高精max { if (a<b)return b; else return a; } inline gaojing min(const gaojing &a,const gaojing &b)//高精min { if (a<b)return a; else return b; } inline gaojing operator + (const gaojing &a,const gaojing &b)//高精+ { gaojing c;set0(c); int maxlen=max(a.len,b.len); for (int i=1;i<=maxlen;i++) { c.a[i]=c.a[i]+a.a[i]+b.a[i]; if (c.a[i]>=10) { c.a[i+1]+=c.a[i]/10; c.a[i]%=10; } } c.len=maxlen+4; while (!c.a[c.len]&&c.len>1) c.len--; return c; } inline gaojing operator - (const gaojing &a,const gaojing &b)//高精- { gaojing c;set0(c); gaojing d;d=a; for (int i=1;i<=b.len;i++) { c.a[i]=d.a[i]-b.a[i]; if (c.a[i]<0) { c.a[i]+=10; int now=i+1; while (!d.a[now]) { d.a[now]=9; now++; } d.a[now]--; } } for (int i=b.len+1;i<=d.len;i++)c.a[i]=d.a[i]; c.len=d.len; while (c.a[c.len]==0&&c.len>1)c.len--; return c; } inline gaojing operator * (const gaojing &a,const gaojing &b)//高精* { gaojing c;set0(c); for(int i=1;i<=a.len;i++) for (int j=1;j<=b.len;j++) c.a[i+j-1]+=a.a[i]*b.a[j]; c.len=a.len+b.len+5; for (int i=1;i<=c.len;i++) { c.a[i+1]+=c.a[i]/10; c.a[i]%=10; } while (!c.a[c.len]&&c.len>1)c.len--; return c; } inline void div_by_2(gaojing &a) { for (int i=a.len;i>=1;i--) { if (a.a[i]&1 && i!=1)a.a[i-1]+=10; a.a[i]/=2; } while (!a.a[a.len]&&a.len>1)a.len--; } inline gaojing operator / (gaojing a,const gaojing &b)//高精/ { gaojing l,r,ans; set0(l);l.len=1; set0(r);r=a; set0(ans);ans.len=1; while (l<r||l==r) { gaojing mid=l+r; div_by_2(mid); if(mid*b==a)return mid; if(mid*b<a){ans=mid;l=mid+one;} if(a<mid*b)r=mid-one; } return ans; } inline gaojing operator ^(const gaojing &a,int p)//高精^ { gaojing ans=one,mult=a; while (p) { if (p&1)ans=ans*mult; mult=mult*mult; p>>=1; } return ans; } inline void chushihua()//初始化,对0、1高精度常数赋值 { set0(zero); zero.len=1; set0(one);one.len=1;one.a[1]=1; } int main() { chushihua(); }
……恩先这样吧还有的再加
标签:blog ar sp for div on log bs amp
原文地址:http://www.cnblogs.com/zhber/p/4098385.html