1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
| string add(string a, string b); string sub(string a, string b); string mul(string a, string b); string div(string a, string b);
string add(string a, string b) { bool neg = false; if (a[0] == '+') a = a.substr(1); if (b[0] == '+') b = b.substr(1); if (a[0] == '-' && b[0] == '-') { neg = true, a = a.substr(1), b = b.substr(1); } else if (a[0] == '-' && b[0] != '-') return sub(b, a.substr(1)); else if (a[0] != '-' && b[0] == '-') return sub(a, b.substr(1)); ull maxlen = max(a.size(), b.size()), r = a.size() - 1; string ans(maxlen + 1, '0'); if (a.size() > b.size()) b.insert(0, a.size() - b.size(), '0'); else a.insert(0, b.size() - a.size(), '0'); reverse(a.begin(), a.end()); reverse(b.begin(), b.end()); for (ull i = 0; i < maxlen; ++i) { ans[i + 1] = (ans[i] - '0' + a[i] - '0' + b[i] - '0') / 10 + '0'; ans[i] = (ans[i] - '0' + a[i] - '0' + b[i] - '0') % 10 + '0'; } r = maxlen; while (r > 0 && ans[r] == '0') r--; ans = ans.substr(0, r + 1); reverse(ans.begin(), ans.end()); return neg ? "-" + ans : ans; }
string sub(string a, string b) { bool neg = false; if (a[0] == '+') a = a.substr(1); if (b[0] == '+') b = b.substr(1); if (b[0] == '-') return add(a, b.substr(1)); if (a[0] == '-') return "-" + add(a.substr(1), b); if (a.size() < b.size() || (a.size() == b.size() && a < b)) (neg = true, swap(a, b)); ull maxlen = max(a.size(), b.size()), r = a.size() - 1; b.insert(0, a.size() - b.size(), '0'); reverse(a.begin(), a.end()); reverse(b.begin(), b.end()); string ans = a; for (ull i = 0; i < maxlen; ++i) { ans[i + 1] -= ans[i] >= b[i] ? 0 : 1; ans[i] = (ans[i] - b[i] + 10) % 10 + '0'; } r = maxlen - 1; while (r > 0 && ans[r] == '0') r--; ans = ans.substr(0, r + 1); reverse(ans.begin(), ans.end()); return neg ? "-" + ans : ans; }
string mul(string a, string b) { bool neg = false; if (a[0] == '+') a = a.substr(1); if (b[0] == '+') b = b.substr(1); if (a[0] == '-' && b[0] != '-') { neg = true, a = a.substr(1); } else if (a[0] != '-' && b[0] == '-') { neg = true, b = b.substr(1); } else if (a[0] == '-' && a[0] == b[0]) { a = a.substr(1), b = b.substr(1); } ull maxlen = max(a.size(), b.size()); ull r = a.size() - 1; string ans(maxlen * 2, '0'); reverse(a.begin(), a.end()); reverse(b.begin(), b.end()); if (a.size() < b.size()) swap(a, b); for (ull i = 0; i < b.size(); ++i) for (ull j = 0; j < a.size(); ++j) { ans[i + j + 1] += (ans[i + j] - '0' + (a[j] - '0') * (b[i] - '0')) / 10; ans[i + j] = (ans[i + j] - '0' + (a[j] - '0') * (b[i] - '0')) % 10 + '0'; } r = 2 * maxlen - 1; while (r > 0 && ans[r] == '0') r--; ans = ans.substr(0, r + 1); reverse(ans.begin(), ans.end()); return neg && ans != "0" ? "-" + ans : ans; }
string div(string a, string b) { bool neg = false; if (a[0] == '+') a = a.substr(1); if (b[0] == '+') b = b.substr(1); if (a[0] == '-' && b[0] != '-') { neg = true, a = a.substr(1); } else if (a[0] != '-' && b[0] == '-') { neg = true, b = b.substr(1); } else if (a[0] == '-' && a[0] == b[0]) { a = a.substr(1), b = b.substr(1); } string ans; if (a.size() < b.size()) ans = "0"; else { string remain = a.substr(0, b.size() - 1), temp; for (ull i = b.size() - 1; i < a.size(); ++i) { remain += a[i], remain = mul(remain, "1"); if (remain.size() > b.size() || (remain.size() == b.size() && remain >= b)) for (char t = '9'; t >= '0'; --t) { temp = mul(b, string(1, t)); if (temp.size() < remain.size() || (temp.size() == remain.size() && temp <= remain)) { ans += t, remain = sub(remain, temp); break; } } else ans += '0'; } } if (ans != "0") { int i = 0; while (ans[i] == '0') i++; ans = ans.substr(i); } return neg && ans != "0" ? "-" + ans : ans; }
|