模板 高精度
阿新 • • 發佈:2018-11-02
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<queue> 5 #define ms(a,b) memset(a,b,sizeof a) 6 #define rep(i,a,n) for(int i = a;i <= n;i++) 7 #define per(i,n,a) for(int i = n;i >= a;i--) 8 #define inf 1000000007 9 using namespace std;10 typedef long long ll; 11 typedef double D; 12 #define eps 1e-8 13 ll read() { 14 ll as = 0,fu = 1; 15 char c = getchar(); 16 while(c < '0' || c > '9') { 17 if(c == '-') fu = -1; 18 c = getchar(); 19 } 20 while(c >= '0' && c <= '9') { 21as = as * 10 + c - '0'; 22 c = getchar(); 23 } 24 return as * fu; 25 } 26 struct Big { 27 const static int N = 5005; 28 int a[N]; 29 bool flag; 30 Big(){ms(a,0),flag = 0;} 31 Big( ll x ){ 32 ms(a,0),flag = 0; 33 flag = (x < 0); 34 x = max(x,-x);35 while(x) a[++a[0]] = x%10,x/=10; 36 clr0(); 37 } 38 void read() { 39 ms(a,0),flag = 0; 40 char s[N]; 41 scanf("%s",s+1); 42 a[0] = strlen(s+1); 43 if(s[1] == '-') a[0]--,flag = 1; 44 rep(i,1,a[0]) a[i] = s[a[0] - i + flag + 1] - '0'; 45 clr0(); 46 } 47 void clr0() { 48 while(a[0] && a[a[0]] == 0) a[0]--; 49 while(a[0] < 0) a[0]++; 50 if(a[0] == 0) flag = 0; 51 } 52 void print() { 53 clr0(); 54 if(!a[0]) return void(puts("0")); 55 if(flag) putchar('-'); 56 per(i,a[0],1) putchar(a[i] + '0'); 57 putchar('\n'); 58 } 59 //clr0 before use 60 bool operator < (const Big &o) const { 61 if(o.a[0] == 0) return flag; 62 if(a[0] == 0) return !o.flag; 63 if(flag ^ o.flag) return flag; 64 if(flag) { 65 rep(i,0,a[0]) { 66 if(a[i] > o.a[i]) return 1; 67 if(a[i] < o.a[i]) return 0; 68 } 69 return 0; 70 } else { 71 rep(i,0,a[0]) { 72 if(a[i] < o.a[i]) return 1; 73 if(a[i] > o.a[i]) return 0; 74 } 75 return 0; 76 } 77 } 78 bool operator == (const Big &o) const { 79 Big r = *this; 80 return !(r < o || o < r); 81 } 82 //保證同號 83 Big operator + (const Big &o) const { 84 if(a[0] == 0) return o; 85 if(o.a[0] == 0) return *this; 86 if(flag ^ o.flag) { 87 Big x = *this,y = o; 88 if(x.flag) { 89 x.flag = 0; 90 return y - x; 91 } 92 else { 93 y.flag = 0; 94 return x - y; 95 } 96 } 97 Big ans; 98 ms(ans.a,0); 99 ans.a[0] = max(a[0],o.a[0]),ans.flag = flag; 100 rep(i,1,ans.a[0]) { 101 ans.a[i] += a[i] + o.a[i]; 102 if(i == ans.a[0] && ans.a[i] >= 10) { 103 ans.a[0]++; 104 } 105 ans.a[i+1] += ans.a[i] / 10; 106 ans.a[i] %= 10; 107 } 108 return ans; 109 } 110 //保證同號 111 Big operator - (const Big &o) const { 112 Big x = *this; 113 Big y = o; 114 if(flag ^ o.flag) { 115 y.flag ^= 1; 116 return x + y; 117 } 118 Big ans; 119 ms(ans.a,0); 120 ans.a[0] = ans.flag = 0; 121 ans.flag = flag; 122 x.flag = y.flag = 0; 123 if(x == y) return ans; 124 if(x < y) swap(x,y),ans.flag ^= 1; 125 rep(i,1,x.a[0]) { 126 if(x.a[i] < y.a[i]) x.a[i] += 10,x.a[i+1]--; 127 ans.a[i] = x.a[i] - y.a[i]; 128 } 129 ans.a[0] = x.a[0]; 130 ans.clr0(); 131 return ans; 132 } 133 //O(n^2) 高精乘 134 Big operator * (const Big &o) const { 135 if(a[0] == 0) return *this; 136 if(o.a[0] == 0) return o; 137 Big ans; 138 ms(ans.a,0); 139 ans.a[0] = a[0] + o.a[0],ans.flag = o.flag ^ flag; 140 rep(i,1,a[0]) rep(j,1,o.a[0]) 141 ans.a[i+j-1] += a[i] * o.a[j]; 142 rep(i,1,ans.a[0]) { 143 if(i == ans.a[0] && ans.a[i] >= 10) ans.a[0]++; 144 ans.a[i+1] += ans.a[i] / 10; 145 ans.a[i] %= 10; 146 } 147 return ans; 148 } 149 }x,y,z; 150 Big zero = Big(0); 151 Big one = Big(1); 152 Big two = Big(2); 153 Big three = Big(3); 154 #define Max(a,b) ((b)<(a)?(a):(b)) 155 #define Min(a,b) ((a)<(b)?(a):(b)) 156 void tst() { 157 while(1) { 158 x.read(),y.read(); 159 z = x + y; 160 printf("plus:"),z.print(); 161 z = x - y; 162 printf("minus:"),z.print(); 163 z = x * y; 164 printf("mult:"),z.print(); 165 } 166 }
今天終於寫完了高精真開心
熱烈歡迎大家Hack
提供資料 [email protected]