高精度模板(结构体封装,重载运算符)
ps:模板可以根据个人习惯风格重构。
1 #include<bits/stdc++.h> 2 #define re register 3 #define max(x,y) ((x)>(y)?(x):(y)) 4 using namespace std; 5 inline int read(){ 6 re int a=0,b=1; re char ch=getchar(); 7 while(ch<'0'||ch>'9') 8 b=(ch=='-')?-1:1,ch=getchar(); 9 while(ch>='0'&&ch<='9') 10 a=(a<<3)+(a<<1)+(ch^48),ch=getchar(); 11 return a*b; 12 } 13 struct Big{ 14 int len,s[810]; 15 Big(){memset(s,0,sizeof(s)); len=1;} 16 Big(int val) {*this=val;} 17 Big(const char *val) {*this=val;} 18 Big operator = (const int &val){ 19 re char s[810]; 20 sprintf(s,"%d",val); 21 *this=s;return *this; 22 } 23 Big operator = (const char *val){ 24 len=strlen(val); 25 while(len>1&&val[0]=='0') ++val,len--; 26 for(re int i=0;i<len;++i) s[i]=val[len-i-1]-'0'; 27 return *this; 28 } 29 inline void deal(){ 30 while(len>1&&!s[len-1]) len--; 31 } 32 Big operator + (const Big &a) const { 33 Big res; res.len=0; 34 re int top=max(len,a.len),add=0; 35 for(re int i=0;add||i<top;++i){ 36 re int now=add; 37 if(i<len) now+=s[i]; 38 if(i<a.len) now+=a.s[i]; 39 res.s[res.len++]=now%10; 40 add=now/10; 41 } 42 return res; 43 } 44 Big operator - (const Big &a) const { 45 Big res; res.len=0;re int del=0; 46 for(re int i=0;i<len;++i){ 47 re int now=s[i]-del; 48 if(i<a.len) now-=a.s[i]; 49 if(now>=0) del=0; 50 else del=1,now+=10; 51 res.s[res.len++]=now; 52 } 53 res.deal(); return res; 54 } 55 Big operator * (const Big &a) const { 56 Big res; res.len=len+a.len; 57 for(re int i=0;i<len;++i) 58 for(re int j=0;j<a.len;++j) 59 res.s[i+j]+=s[i]*a.s[j]; 60 for(re int i=0;i<res.len;++i) 61 res.s[i+1]+=res.s[i]/10,res.s[i]%=10; 62 res.deal(); return res; 63 } 64 Big operator / (const Big &a) const { 65 Big res,cur=0;res.len=len; 66 for(re int i=len-1;~i;--i){ 67 cur=cur*10,cur.s[0]=s[i]; 68 while(cur>=a) 69 cur-=a,res.s[i]++; 70 } 71 res.deal(); return res; 72 } 73 Big operator % (const Big &a) const { 74 Big res=*this/a; 75 return *this-res*a; 76 } 77 Big operator += (const Big &a) {*this=*this+a; return *this;} 78 Big operator -= (const Big &a) {*this=*this-a; return *this;} 79 Big operator *= (const Big &a) {*this=*this*a; return *this;} 80 Big operator /= (const Big &a) {*this=*this/a; return *this;} 81 Big operator %= (const Big &a) {*this=*this%a; return *this;} 82 bool operator < (const Big &a) const { 83 if(len!=a.len) return len<a.len; 84 for(re int i=len-1;~i;i--) 85 if(s[i]!=a.s[i]) return s[i]<a.s[i]; 86 return false; 87 } 88 bool operator > (const Big &a) const {return a<*this;} 89 bool operator <= (const Big &a) const {return !(*this>a);} 90 bool operator >= (const Big &a) const {return !(*this<a);} 91 bool operator == (const Big &a) const {return !(*this>a||*this<a);} 92 bool operator != (const Big &a) const {return *this>a||*this<a;} 93 }; 94 inline Big Sqrt(const Big &x){ 95 re int a[510],top=0; 96 for(re int i=0;i<x.len;i+=2){ 97 if(i==x.len-1) a[top++]=x.s[i]; 98 else a[top++]=x.s[i]+x.s[i+1]*10; 99 } 100 Big res=(int)sqrt((double)a[top-1]); 101 re int dat=(int)sqrt((double)a[top-1]); 102 Big pre=a[top-1]-dat*dat,val; 103 for(re int i=top-2;~i;--i){ 104 pre=pre*100+a[i],val=res*20; 105 for(re int j=9;~j;--j){ 106 Big now=(dat+j)*j; 107 if(now>pre) continue; 108 res=res*10+j;pre-=now; break; 109 } 110 } 111 return res; 112 } 113 inline void print(const Big &a){ 114 for(re int i=a.len-1;~i;--i) 115 printf("%d",a.s[i]); puts(""); 116 } 117 signed main(){ 118 return 0; 119 }