高精度
结构体,重载运算符:
1 #include <cstdio> 2 #include <cstdlib> 3 #include <cmath> 4 #include <ctime> 5 #include <cstring> 6 #include <string> 7 #include <map> 8 #include <set> 9 #include <list> 10 #include <queue> 11 #include <stack> 12 #include <bitset> 13 #include <vector> 14 #include <algorithm> 15 #include <iostream> 16 using namespace std; 17 #define ll long long 18 const int maxn=1e4+10; 19 20 char str[maxn]; 21 22 ///for faster speed, 压位 23 struct node 24 { 25 int sym,w,s[maxn]; 26 ///sym 1:- 0:+ 27 ///初始化 28 node() 29 { 30 w=1; 31 sym=0; 32 ///不需要,默认值为0 33 // for (int i=1;i<=1e4;i++) 34 // s[i]=0; 35 } 36 ///大于 37 bool operator>(const node &b) const 38 { 39 if (w<b.w) 40 return 0; 41 if (w>b.w) 42 return 1; 43 for (int i=1;i<=w;i++) 44 if (s[i]<b.s[i]) 45 return 0; 46 else if (s[i]>b.s[i]) 47 return 1; 48 return 0; 49 } 50 ///直接加 51 node plus(int sym,const node &b) const 52 { 53 node c; 54 int i; 55 c.sym=sym; 56 c.w=max(w,b.w); 57 for (i=1;i<=c.w;i++) 58 { 59 c.s[i]+=s[i]+b.s[i];///! 60 if (c.s[i]>=10) 61 { 62 c.s[i]-=10; 63 c.s[i+1]++; 64 } 65 } 66 if (c.s[i]!=0) 67 c.w++; 68 if (c.w==1 && c.s[1]==0) 69 c.sym=0; 70 return c; 71 } 72 ///直接减 73 node minus(int sym,const node &b,int cond) const 74 { 75 node c; 76 int i; 77 c.sym=sym; 78 c.w=max(w,b.w); 79 for (i=1;i<=c.w;i++) 80 { 81 if (cond) 82 c.s[i]+=s[i]-b.s[i]; ///! 83 else 84 c.s[i]+=b.s[i]-s[i]; 85 if (c.s[i]<0) 86 { 87 c.s[i]+=10; 88 c.s[i+1]--; 89 } 90 } 91 while (c.w>1 && c.s[c.w]==0) 92 c.w--; 93 if (c.w==1 && c.s[1]==0) 94 c.sym=0; 95 return c; 96 } 97 ///加 98 node operator+(const node &b) const 99 { 100 if (sym==b.sym) 101 return plus(sym,b); 102 else if (*this>b) 103 return minus(sym,b,1); 104 return minus(b.sym,b,0); 105 } 106 ///减 107 node operator-(const node &b) const 108 { 109 if (sym!=b.sym) 110 return plus(sym,b); 111 else if (*this>b) 112 return minus(sym,b,1); 113 return minus(!sym,b,0); 114 } 115 ///乘 116 node operator*(const node &b) const 117 { 118 node c; 119 int i,j,k; 120 if (sym==b.sym) 121 c.sym=0; 122 else 123 c.sym=1; 124 for (i=1;i<=w;i++) 125 for (j=1;j<=b.w;j++) 126 { 127 k=i+j-1; 128 c.s[k]+=s[i]*b.s[j]; 129 c.s[k+1]+=c.s[k]/10; 130 c.s[k]%=10; 131 } 132 c.w=w+b.w; 133 while (c.w>1 && c.s[c.w]==0) 134 c.w--; 135 if (c.w==1 && c.s[1]==0) 136 c.sym=0; 137 return c; 138 } 139 ///低除 140 node operator/(ll &b) const 141 { 142 node c; 143 int i; 144 ll v=0; 145 c.sym=sym; 146 for (i=w;i>=1;i--) ///! 147 { 148 v=v*10+s[i]; 149 c.s[i]=v/b; 150 v%=b; 151 } 152 c.w=w; 153 while (c.w>1 && c.s[c.w]==0) 154 c.w--; 155 if (c.w==1 && c.s[1]==0) 156 c.sym=0; 157 return c; 158 } 159 ///取模 160 ll operator%(ll b) const 161 { 162 int i; 163 ll v=0; 164 for (i=w;i>=1;i--) ///! 165 v=(v*10+s[i])%b; 166 return v; 167 } 168 ///最大公约数 169 friend node operator|(node a,node b) 170 { 171 node c; 172 node b2; 173 int cond; 174 ll value=2; 175 c.s[1]=1; 176 b2.s[1]=2; 177 while (!((a.w==1 && a.s[1]==0) || (b.w==1 && b.s[1]==0))) 178 { 179 cond=0; 180 if ((a.s[1] & 1)==0) 181 a=a/value,cond++; 182 if ((b.s[1] & 1)==0) 183 b=b/value,cond++; 184 if (cond==2) 185 c=c*b2; 186 else if (cond==0) 187 { 188 if (a>b) 189 a=a-b; 190 else 191 b=b-a; 192 } 193 } 194 if (a.w==1 && a.s[1]==0) 195 c=c*b; 196 else 197 c=c*a; 198 return c; 199 } 200 ///读入 201 void read() 202 { 203 scanf("%s",str); 204 w=strlen(str); 205 if (str[0]=='-') 206 sym=1; 207 else 208 sym=0; 209 for (int i=sym;i<w;i++) 210 s[w-i]=str[i]-48; 211 if (sym) 212 w--; 213 } 214 ///输出 215 void write() 216 { 217 if (sym) 218 printf("-"); 219 for (int i=w;i>=1;i--) 220 printf("%d",s[i]); 221 printf("\n"); 222 } 223 }; 224 225 int main() 226 { 227 node s1,s2; 228 ll b; 229 230 // ///加 231 s1.read(); 232 s2.read(); 233 s1=s1+s2; 234 s1.write(); 235 236 // ///减 237 // s1.read(); 238 // s2.read(); 239 // s1=s1-s2; 240 // s1.write(); 241 242 // ///乘 243 // s1.read(); 244 // s2.read(); 245 // s1=s1*s2; 246 // s1.write(); 247 248 // ///除 249 // s1.read(); 250 // scanf("%lld",&b); 251 // s1=s1/b; 252 // s1.write(); 253 254 // s1.read(); 255 // scanf("%lld",&b); 256 // printf("%lld\n",s1%b); 257 258 // node s3; 259 // s1.read(); 260 // s2.read(); 261 // s3=s1|s2; 262 // s3.write(); 263 264 return 0; 265 } 266 /* 267 + 268 2 1 269 2 -1 270 2 -3 271 -2 1 272 -2 3 273 -2 -1 274 -2 2 275 276 - 277 2 1 278 2 3 279 2 -1 280 -2 1 281 -2 -1 282 -2 -3 283 -2 -2 284 2 2 285 286 * 287 1 2 288 1 -2 289 -1 2 290 -1 -2 291 132412 0 292 293 / 294 100 3 295 -1 3 296 0 1234124 297 298 % 299 100 3 300 301 gcd 302 125 100 303 304 */
其中bzoj 1876 TLE,需要压位
单独:
luogu
加法
1601
1 #include <cstdio> 2 #include <cstdlib> 3 #include <cmath> 4 #include <ctime> 5 #include <cstring> 6 #include <string> 7 #include <map> 8 #include <set> 9 #include <list> 10 #include <queue> 11 #include <stack> 12 #include <vector> 13 #include <bitset> 14 #include <algorithm> 15 #include <iostream> 16 using namespace std; 17 #define ll long long 18 const int maxn=1e5+10; 19 20 int a[maxn],b[maxn];///init all 0 21 22 char s[maxn]; 23 24 int main() 25 { 26 int lena,lenb,i; 27 scanf("%s",s); 28 lena=strlen(s); 29 for (i=0;i<lena;i++) 30 a[lena-i]=s[i]-48; 31 32 scanf("%s",s); 33 lenb=strlen(s); 34 for (i=0;i<lenb;i++) 35 b[lenb-i]=s[i]-48; 36 37 lena=max(lena,lenb); 38 for (i=1;i<=lena;i++) 39 { 40 a[i]+=b[i]; 41 if (a[i]>9) 42 { 43 a[i]-=10; 44 a[i+1]++; 45 } 46 } 47 if (a[i]!=0) 48 lena++; 49 for (i=lena;i>=1;i--) 50 printf("%d",a[i]); 51 return 0; 52 }
减法
2142
1 #include <cstdio> 2 #include <cstdlib> 3 #include <cmath> 4 #include <ctime> 5 #include <cstring> 6 #include <string> 7 #include <map> 8 #include <set> 9 #include <list> 10 #include <queue> 11 #include <stack> 12 #include <vector> 13 #include <bitset> 14 #include <algorithm> 15 #include <iostream> 16 using namespace std; 17 #define ll long long 18 const int maxn=1e5+10; 19 20 int a[maxn],b[maxn],lena,lenb;///init all 0 21 char s[maxn]; 22 23 bool judge() 24 { 25 if (lena>lenb) 26 return 1; 27 else if (lena<lenb) 28 return 0; 29 else 30 { 31 for (int i=lena;i>=1;i--) 32 if (a[i]>b[i]) 33 return 1; 34 else if (a[i]<b[i]) 35 return 0; 36 } 37 return 1;///attention! 38 } 39 40 int main() 41 { 42 int i; 43 bool vis=0; 44 scanf("%s",s); 45 lena=strlen(s); 46 for (i=0;i<lena;i++) 47 a[lena-i]=s[i]-48; 48 49 scanf("%s",s); 50 lenb=strlen(s); 51 for (i=0;i<lenb;i++) 52 b[lenb-i]=s[i]-48; 53 54 if (!judge()) 55 { 56 vis=1; 57 swap(lena,lenb); 58 swap(a,b); 59 // for (i=1;i<=lena;i++) 60 // swap(a[i],b[i]); 61 } 62 63 lena=max(lena,lenb); 64 for (i=1;i<=lena;i++) 65 { 66 a[i]-=b[i]; 67 if (a[i]<0) 68 { 69 a[i]+=10; 70 a[i+1]--; 71 } 72 } 73 while (lena>1 && a[lena]==0)///attention 74 lena--; 75 if (vis) 76 printf("-"); 77 for (i=lena;i>=1;i--) 78 printf("%d",a[i]); 79 return 0; 80 } 81 /* 82 1 83 1 84 85 1 86 100 87 */
乘法
1303
1 #include <cstdio> 2 #include <cstdlib> 3 #include <cmath> 4 #include <ctime> 5 #include <cstring> 6 #include <string> 7 #include <map> 8 #include <set> 9 #include <list> 10 #include <queue> 11 #include <stack> 12 #include <vector> 13 #include <bitset> 14 #include <algorithm> 15 #include <iostream> 16 using namespace std; 17 #define ll long long 18 const int maxn=1e5+10; 19 20 ///attention:the length of c =a+b 21 int a[maxn],b[maxn],c[maxn+maxn],lena,lenb;///init all 0 22 char s[maxn]; 23 24 int main() 25 { 26 int i,j,k; 27 scanf("%s",s); 28 lena=strlen(s); 29 for (i=0;i<lena;i++) 30 a[lena-i]=s[i]-48; 31 32 scanf("%s",s); 33 lenb=strlen(s); 34 for (i=0;i<lenb;i++) 35 b[lenb-i]=s[i]-48; 36 37 for (i=1;i<=lena;i++) 38 for (j=1;j<=lenb;j++) 39 { 40 k=i+j-1; 41 c[k]+=a[i]*b[j]; 42 c[k+1]+=c[k]/10; 43 c[k]%=10; 44 } 45 k=lena+lenb; 46 while (k>1 && c[k]==0) 47 k--; 48 for (i=k;i>=1;i--) 49 printf("%d",c[i]); 50 return 0; 51 } 52 /* 53 0 54 10 55 */
除法
1480
1 #include <cstdio> 2 #include <cstdlib> 3 #include <cmath> 4 #include <ctime> 5 #include <cstring> 6 #include <string> 7 #include <map> 8 #include <set> 9 #include <list> 10 #include <queue> 11 #include <stack> 12 #include <vector> 13 #include <bitset> 14 #include <algorithm> 15 #include <iostream> 16 using namespace std; 17 #define ll long long 18 const int maxn=1e5+10; 19 20 int a[maxn],lena,d[maxn]; 21 ll b; 22 char s[maxn]; 23 24 int main() 25 { 26 int i; 27 ll c; 28 scanf("%s",s); 29 lena=strlen(s); 30 for (i=0;i<lena;i++) 31 a[lena-i]=s[i]-48; 32 scanf("%lld",&b); 33 34 c=0; 35 i=lena; 36 for (i=lena;i>=1;i--) 37 { 38 c=c*10+a[i]; 39 d[i]=c/b; 40 c%=b; 41 } 42 43 while (lena>1 && d[lena]==0) 44 lena--; 45 for (i=lena;i>=1;i--) 46 printf("%d",d[i]); 47 return 0; 48 } 49 /* 50 0 51 10 52 53 10 54 2 55 */