高精度运算模板
作者:互联网
为了模板化,我们用struct或class来定义高精度。
1 template<int SIZE>//用来限制最大表示位数 2 class bign(){ 3 private://在class外不能调用 4 int a[SIZE],len;//高精度整数,用数组表示 5 public: 6 //一系列运算函数 7 }
输入输出:
1 bign(){len=1;memset(a,0,sizeof(a));}//初始化 2 void in(){ 3 char s[SIZE]; 4 scanf("%s",s); 5 len=strlen(s); 6 for(int i=0,j=len-1;i<len;i++,j--) a[i]=s[j]-48; 7 while(len>1&&a[len-1]==0) len--;//应对输入含前导0 8 } 9 void out(bool Entr=true){//默认输出换行 10 for(int i=len-1;i>=0;i--) putchar(a[i]+'0'); 11 if(Entr) putchar('\n'); 12 }
赋值:
1 void operator = (char *s){ 2 len=strlen(s); 3 for(int i=0,j=len-1;i<len;i++,j--) a[i]=s[j]-48; 4 while(len>1&&a[len-1]==0) len--; 5 } 6 void operator = (int x){ 7 len=0;if(x==0) len=1,a[0]=0; 8 while(x>0) a[len++]=x%10,x/=10; 9 } 10 void operator = (long long x){ 11 len=0;if(x==0) len=1,a[0]=0; 12 while(x>0) a[len++]=x%10,x/=10; 13 }
大小比较:
1 friend bool operator < (bign a,bign b){ 2 if(a.len!=b.len) return a.len<b.len; 3 for(int i=a.len-1;i>=0;i--) 4 if(a.a[i]!=b.a[i]) return a.a[i]<b.a[i]; 5 return false;//表示a,b相等 6 } 7 friend bool operator > (bign a,bign b){ 8 return b<a; 9 } 10 friend bool operator <= (bign a,bign b){ 11 return !(a>b); 12 } 13 friend bool operator >= (bign a,bign b){ 14 return !(a<b); 15 } 16 friend bool operator == (bign a,bign b){ 17 return !(a>b&&b<a); 18 } 19 friend bool operator != (bign a,bign b){ 20 return a<b||b<a; 21 }
加减乘除运算:
1 friend bign operator + (bign a,bign b){ 2 bign c; 3 int lenth=max(a.len,b.len)+1; 4 for(int i=0;i<lenth;i++) c.a[i]=a.a[i]+b.a[i]; 5 for(int i=0;i<lenth;i++){ 6 if(c.a[i]>9) c.a[i]-=10,c.a[i+1]++; 7 } 8 while(lenth>1&&c.a[lenth-1]==0) lenth--; 9 c.len=lenth; 10 return c; 11 } 12 friend bign operator - (bign a,bign b){//求a-b的绝对值 13 bign c; 14 if(a<b) swap(a,b); 15 int len=a.len; 16 for(int i=0;i<len;i++) 17 c.a[i]=a.a[i]-b.a[i]; 18 for(int i=0;i<len;i++) 19 if(c.a[i]<0) c.a[i+1]--, c.a[i]+=10; 20 while(len>1&&c.a[len-1]==0) len--; 21 c.len=len; 22 return c; 23 } 24 friend bign operator * (bign a,bign b){ 25 bign ans; 26 int len=a.len+b.len; 27 for(int i=0;i<a.len;i++) 28 for(int j=0;j<b.len;j++) 29 ans.a[i+j]+=a.a[i]*b.a[j];//这里要思考一下 30 for(int i=0;i<len;i++) 31 if(ans.a[i]>9) ans.a[i+1]+=ans.a[i]/10,ans.a[i]%=10; 32 while(len>1&&ans.a[len-1]==0) len--; 33 ans.len=len; 34 return ans; 35 } 36 friend bign operator * (bign a,int x){//防溢出,x<=200,000,000 37 bign ans; 38 int len=a.len+20; 39 for(int i=0;i<len;i++) ans.a[i]=a.a[i]*x; 40 for(int i=0;i<len;i++) 41 if(ans.a[i]>9) ans.a[i+1]+=ans.a[i]/10,ans.a[i]%=10; 42 while(ans.a[len-1]==0&&len>1) len--; 43 ans.len=len; 44 return ans; 45 } 46 friend bign operator / (bign a,int b){//防溢出 47 bign c; 48 int d=0,len=a.len; 49 for(int i=len-1;i>=0;i--) 50 d=d*10+a.a[i],c.a[i]=d/b,d=d%b; 51 while(len>1&&c.a[len-1]==0) len--; 52 c.len=len; 53 return c; 54 } 55 friend bign operator / (bign a,bign b){ 56 bign c,d; 57 int len=a.len; 58 for(int i=len-1;i>=0;i--){ 59 d=d*10; 60 d.a[0]=a.a[i]; 61 while(d>=b) c.a[i]++, d=d-b;//这里效率不是很高 62 } 63 while(len>1&&c.a[len-1]==0) len--; 64 c.len=len; 65 return c; 66 }
取模:
1 friend int operator % (bign a,int b){//注意返回值为int 2 int d=0,len=a.len; 3 for(int i=len-1;i>=0;i--) 4 d=d*10+a.a[i],d=d%b; 5 return d; 6 } 7 friend bign operator % (bign a,bign b){ 8 bign d; 9 int len=a.len; 10 for(int i=len-1;i>=0;i--){ 11 d=d*10; 12 d.a[0]=a.a[i]; 13 while(d>=b) d=d-b;//同理,效率不够高 14 } 15 while(len>1&&d.a[len-1]==0) len--; 16 d.len=len; 17 return d; 18 }
完整代码:
1 template<int SIZE> 2 class bign{ 3 private: 4 int a[SIZE],len; 5 public: 6 bign(){len=1;memset(a,0,sizeof(a));} 7 void in(){ 8 char s[SIZE]; 9 scanf("%s",s); 10 len=strlen(s); 11 for(int i=0,j=len-1;i<len;i++,j--) a[i]=s[j]-48; 12 while(len>1&&a[len-1]==0) len--; 13 } 14 void out(bool Entr=true){ 15 for(int i=len-1;i>=0;i--) putchar(a[i]+'0'); 16 if(Entr) putchar('\n'); 17 } 18 void operator = (char *s){ 19 len=strlen(s); 20 for(int i=0,j=len-1;i<len;i++,j--) a[i]=s[j]-48; 21 while(len>1&&a[len-1]==0) len--; 22 } 23 void operator = (int x){ 24 len=0;if(x==0) len=1,a[0]=0; 25 while(x>0) a[len++]=x%10,x/=10; 26 } 27 void operator = (long long x){ 28 len=0;if(x==0) len=1,a[0]=0; 29 while(x>0) a[len++]=x%10,x/=10; 30 } 31 friend bool operator < (bign a,bign b){ 32 if(a.len!=b.len) return a.len<b.len; 33 for(int i=a.len-1;i>=0;i--) 34 if(a.a[i]!=b.a[i]) return a.a[i]<b.a[i]; 35 return false; 36 } 37 friend bool operator > (bign a,bign b){ 38 return b<a; 39 } 40 friend bool operator <= (bign a,bign b){ 41 return !(a>b); 42 } 43 friend bool operator >= (bign a,bign b){ 44 return !(a<b); 45 } 46 friend bool operator == (bign a,bign b){ 47 return !(a>b&&b<a); 48 } 49 friend bool operator != (bign a,bign b){ 50 return a<b||b<a; 51 } 52 friend bign operator + (bign a,bign b){ 53 bign c; 54 int lenth=max(a.len,b.len)+1; 55 for(int i=0;i<lenth;i++) c.a[i]=a.a[i]+b.a[i]; 56 for(int i=0;i<lenth;i++){ 57 if(c.a[i]>9) c.a[i]-=10,c.a[i+1]++; 58 } 59 while(lenth>1&&c.a[lenth-1]==0) lenth--; 60 c.len=lenth; 61 return c; 62 } 63 friend bign operator - (bign a,bign b){ 64 bign c; 65 if(a<b) std::swap(a,b); 66 int len=a.len; 67 for(int i=0;i<len;i++) 68 c.a[i]=a.a[i]-b.a[i]; 69 for(int i=0;i<len;i++) 70 if(c.a[i]<0) c.a[i+1]--, c.a[i]+=10; 71 while(len>1&&c.a[len-1]==0) len--; 72 c.len=len; 73 return c; 74 } 75 friend bign operator * (bign a,bign b){ 76 bign ans; 77 int len=a.len+b.len; 78 for(int i=0;i<a.len;i++) 79 for(int j=0;j<b.len;j++) 80 ans.a[i+j]+=a.a[i]*b.a[j]; 81 for(int i=0;i<len;i++) 82 if(ans.a[i]>9) ans.a[i+1]+=ans.a[i]/10,ans.a[i]%=10; 83 while(len>1&&ans.a[len-1]==0) len--; 84 ans.len=len; 85 return ans; 86 } 87 friend bign operator * (bign a,int x){ 88 bign ans; 89 int len=a.len+20; 90 for(int i=0;i<len;i++) ans.a[i]=a.a[i]*x; 91 for(int i=0;i<len;i++) 92 if(ans.a[i]>9) ans.a[i+1]+=ans.a[i]/10,ans.a[i]%=10; 93 while(ans.a[len-1]==0&&len>1) len--; 94 ans.len=len; 95 return ans; 96 } 97 friend bign operator / (bign a,int b){ 98 bign c; 99 int d=0,len=a.len; 100 for(int i=len-1;i>=0;i--) 101 d=d*10+a.a[i],c.a[i]=d/b,d=d%b; 102 while(len>1&&c.a[len-1]==0) len--; 103 c.len=len; 104 return c; 105 } 106 friend bign operator / (bign a,bign b){ 107 bign c,d; 108 int len=a.len; 109 for(int i=len-1;i>=0;i--){ 110 d=d*10; 111 d.a[0]=a.a[i]; 112 while(d>=b) c.a[i]++, d=d-b; 113 } 114 while(len>1&&c.a[len-1]==0) len--; 115 c.len=len; 116 return c; 117 } 118 friend int operator % (bign a,int b){ 119 int d=0,len=a.len; 120 for(int i=len-1;i>=0;i--) 121 d=d*10+a.a[i],d=d%b; 122 return d; 123 } 124 friend bign operator % (bign a,bign b){ 125 bign d; 126 int len=a.len; 127 for(int i=len-1;i>=0;i--){ 128 d=d*10; 129 d.a[0]=a.a[i]; 130 while(d>=b) d=d-b; 131 } 132 while(len>1&&d.a[len-1]==0) len--; 133 d.len=len; 134 return d; 135 } 136 friend bign gcd(bign a,bign b){ 137 bign zero; 138 zero=0; 139 return b==zero?a:gcd(b,a%b); 140 } 141 friend bign pow_mod(int a,int n,bign m){ 142 bign ret,X; 143 if(n==0){ 144 ret=1;return ret; 145 } 146 X=pow_mod(a,n/2,m); 147 ret=(X*X)%m; 148 if(n%2) ret=(ret*a)%m; 149 return ret; 150 } 151 };完整代码
标签:10,return,运算,高精度,int,len,--,bign,模板 来源: https://www.cnblogs.com/la-oj/p/14066052.html