高精封装模板
1 #include<iostream> 2 #include<string.h> 3 #include<stdio.h> 4 #include<cstdlib> 5 #include<algorithm> 6 #define base 100000 7 #define cut 5 8 #define L 10000 9 using namespace std; 10 11 struct bignum 12 { 13 int len; 14 long long num[L]; 15 bignum() 16 { 17 len=1; 18 memset(num,0,sizeof(num)); 19 } 20 bignum operator=(const bignum &a) 21 { 22 len=a.len; 23 for(int i=0;i<len;i++) 24 num[i]=a.num[i]; 25 return *this; 26 } 27 long long &operator[](int a) 28 { 29 return num[a]; 30 } 31 long long operator[](int a)const 32 { 33 return num[a]; 34 } 35 friend istream&operator>>(istream&,bignum&); 36 friend ostream&operator<<(ostream&,bignum&); 37 }; 38 39 int operator>(const bignum a,const bignum b) 40 { 41 if(a.len!=b.len) 42 { 43 if(a.len>b.len) 44 return 1; 45 return 0; 46 } 47 for(int i=a.len-1;i>=0;i--) 48 if(a[i]!=b[i]) 49 { 50 if(a[i]>b[i]) 51 return 1; 52 return 0; 53 } 54 return 0; 55 } 56 57 int operator==(const bignum a,const bignum b) 58 { 59 if(a.len!=b.len) 60 return 0; 61 for(int i=0;i<a.len;i++) 62 if(a[i]!=b[i]) 63 return 0; 64 return 1; 65 } 66 67 int operator<(const bignum a,const bignum b) 68 { 69 if(a>b||a==b)return 0; 70 return 1; 71 } 72 73 bignum operator+(bignum a,bignum b) 74 { 75 bignum ret; 76 long long carry=0; 77 for(int i=0;;i++) 78 { 79 ret[i]=a[i]+b[i]+carry; 80 carry=ret[i]/base; 81 ret[i]%=base; 82 if(i>=a.len&&i>=b.len&&carry==0) 83 break; 84 } 85 ret.len=min(L,max(a.len,b.len)+10); 86 while(ret.len>0&&ret[ret.len-1]==0) 87 ret.len--; 88 if(ret.len==0) 89 ret.len=1; 90 return ret; 91 } 92 93 bignum operator+(bignum a,int b) 94 { 95 long long carry=b; 96 for(int i=0;;i++) 97 { 98 a[i]+=carry; 99 carry=a[i]/base; 100 a[i]%=base; 101 if(a[i]==0&&carry==0&&i>=a.len) 102 break; 103 } 104 a.len=min(L,a.len+10); 105 while(a.len>0&&a[a.len-1]==0) 106 a.len--; 107 return a; 108 } 109 110 bignum operator*(bignum a,bignum b) 111 { 112 bignum ret; 113 for(int i=0;i<a.len;i++) 114 for(int j=0;j<b.len;j++) 115 ret[i+j]+=a[i]*b[j]; 116 long long carry=0; 117 for(int i=0;;i++) 118 { 119 ret[i]+=carry; 120 carry=ret[i]/base; 121 ret[i]%=base; 122 if(ret[i]==0&&carry==0&&i>=a.len+b.len-1) 123 break; 124 } 125 a.len=min(L,a.len+b.len+10); 126 while(a.len>0&&a[a.len-1]==0) 127 a.len--; 128 return a; 129 } 130 131 bignum operator*(bignum a,int b) 132 { 133 long long carry=0; 134 for(int i=0;;i++) 135 { 136 carry+=a[i]*b; 137 a[i]=carry%base; 138 carry/=base; 139 if(carry==0&&a[i]==0&&i>=a.len) 140 break; 141 } 142 a.len=min(L,a.len+10); 143 while(a.len>0&&a[a.len-1]==0) 144 a.len--; 145 return a; 146 } 147 148 bignum operator-(bignum a,bignum b) 149 { 150 long long carry=0; 151 for(int i=0;;i++) 152 { 153 a[i]-=b[i]+carry; 154 if(a[i]<0) 155 carry=(-a[i]/base+1); 156 else carry=0; 157 a[i]+=carry*base; 158 if(carry==0&&i>=b.len) 159 break; 160 } 161 while(a.len>0&&a[a.len-1]==0) 162 a.len--; 163 return a; 164 } 165 166 bignum operator-(bignum a,int b) 167 { 168 long long carry=b; 169 for(int i=0;;i++) 170 { 171 a[i]-=carry; 172 if(a[i]<0) 173 carry=(-a[i]/base+1); 174 else carry=0; 175 a[i]+=carry*base; 176 if(carry==0) 177 break; 178 } 179 while(a.len>0&&a[a.len-1]==0) 180 a.len--; 181 return a; 182 } 183 184 bignum operator/(bignum a,int b) 185 { 186 long long carry=0; 187 for(int i=a.len-1;i>=0;i--) 188 { 189 a[i]+=carry*base; 190 carry=a[i]%b; 191 a[i]/=b; 192 } 193 while(a.len>0&&a[a.len-1]==0) 194 a.len--; 195 return a; 196 } 197 198 bignum operator%(const bignum a,int b) 199 { 200 return a-(a/b)*b; 201 } 202 203 istream& operator>>(istream & in,bignum &b) 204 { 205 char ch[L*cut+5]; 206 in>>ch; 207 int l=strlen(ch); 208 int count=0,sum=0,r=0; 209 for(int i=0;i<l;i++) 210 if(ch[i]!='0') 211 { 212 r=i; 213 break; 214 } 215 if(r==0&&ch[r]=='0') 216 r=l-1; 217 for(int i=l-1;i>=r;) 218 { 219 sum=0; 220 int t=1; 221 for(int j=0;j<cut&&i>=0;j++,i--,t*=10) 222 sum+=(ch[i]-'0')*t; 223 b[count]=sum; 224 count++; 225 } 226 b.len=count; 227 return in; 228 } 229 230 ostream& operator<<(ostream& out,bignum& b) 231 { 232 cout<<b[b.len-1]; 233 for(int i=b.len-2;i>=0;i--) 234 { 235 cout.width(cut); 236 cout.fill('0'); 237 cout<<b[i]; 238 } 239 return out; 240 } 241 242 int main() 243 { 244 }
论高精板的重要性(摊手