ostream &operator<<(ostream &out, const number &a) { if (a.neg) out << '-'; out << a.num; return out; }
number operator+(const number &a, const number &b) { // 加法, 不考虑负数 stringstream ss; auto x = a.num.rbegin(), y = b.num.rbegin(); int c = 0; // 进位标志 while (x != a.num.rend() || y != b.num.rend() || c > 0) { if (x != a.num.rend()) { c += (*x) - '0'; ++x; } if (y != b.num.rend()) { c += (*y) - '0'; ++y; } ss << c % 10; c /= 10; } string ans = ss.str(); // 结果是反的,要逆过来存下 reverse(ans.begin(), ans.end()); returnnumber(ans); } booloperator<(const number &a, const number &b) { // 比较大小,不考虑负数 int n1 = a.num.size(), n2 = b.num.size(); if (n1 != n2) return n1 < n2; for (auto x = a.num.begin(), y = b.num.begin(); x != a.num.end(); ++x, ++y) { if ((*x) != (*y)) return (*x) < (*y); } returnfalse; // 不考虑等于 } booloperator>=(const number &a, const number &b) { // 比较大小,不考虑负数 int n1 = a.num.size(), n2 = b.num.size(); if (n1 != n2) return n1 > n2; for (auto x = a.num.begin(), y = b.num.begin(); x != a.num.end(); ++x, ++y) { if ((*x) != (*y)) return (*x) > (*y); } returntrue; // 考虑等于 }
number operator-(const number &a, const number &b) { // 减法,结果可能是负数 if (a < b) { auto ans = b - a; ans.neg = true; return ans; } stringstream ss; auto x = a.num.rbegin(), y = b.num.rbegin(); // 借位标志 c for (int c = 0; x != a.num.rend(); ++x) { c = (*x) - '0' - c; if (y != b.num.rend()) { c -= (*y) - '0'; ++y; } ss << (c + 10) % 10; if (c < 0) c = 1; else c = 0; } string ans = ss.str(); while (ans.back() == '0') ans.pop_back(); reverse(ans.begin(), ans.end()); if (ans.size() == 0) returnnumber(0); returnnumber(ans); }
number operator*(const number &a, constint b) { // 与个位数的乘法 if (b == 0) returnnumber(0); stringstream ss; int c = 0; // 进位标志 for (auto x = a.num.rbegin(); x != a.num.rend() || c != 0; c /= 10) { if (x != a.num.rend()){ c = (*x - '0') * b + c; ++x; } ss << c % 10; } string ans = ss.str(); reverse(ans.begin(), ans.end()); returnnumber(ans); }
number operator*(const number &a, const number &b) { // 高精度乘法,不考虑负数 number ans(0); int cnt = 0; // 移位标志 for (auto x = b.num.rbegin(); x != b.num.rend(); ++x, ++cnt) { auto tmp = a * (*x - '0'); if (tmp.num == "0") continue; // 0 不需要处理 for (int i = 0; i < cnt; ++i) tmp.num.push_back('0'); ans = ans + tmp; } return ans; }
number operator/(const number &a, const number &b) { // 高精度除法,只保留整数部分,不考虑负数 if (a < b) returnnumber(0); string tmp = a.num.substr(0, b.num.size()); number base(tmp); stringstream ss; for (auto x = a.num.begin() + b.num.size(); ; x++) { int cnt = 0; while (base >= b * (cnt + 1)) ++cnt; ss << cnt; if (x == a.num.end()) break; base = base - (b * cnt); if (base.num == "0") base.num = *x; else base.num.push_back(*x); } string ans = ss.str(); // 去掉前置 0 reverse(ans.begin(), ans.end()); while (ans.back() == '0') ans.pop_back(); reverse(ans.begin(), ans.end()); returnnumber(ans); }
intread(char *s){ int i = 0; char c = getchar(); while (c < '0' || c > '9') c = getchar(); for (; c >= '0' && c <= '9'; c = getchar()) s[i++] = c; return i; }
voidrev(char *s, int n){ char tmp; for (int i = 0, j = n - 1; i < j; ++i, --j) { tmp = s[i]; s[i] = s[j]; s[j] = tmp; } }
intadd(char *s1, int n1, char *s2, int n2, char *s3){ // 将长度为 n1, n2 的 s1, s2 相加,结果放在 s3, 返回长度 int f = 0, num, i = 0; // 进位标志 --n1; --n2; while (n1 >= 0 || n2 >= 0) { num = (n1 >= 0 ? s1[n1] - '0' : 0) + (n2 >= 0 ? s2[n2] - '0' : 0) + f; --n1; --n2; if (num > 9) { f = 1; num -= 10; } else f = 0; s3[i++] = num + '0'; } if (f != 0) s3[i++] = f + '0'; rev(s3, i); // 结果是反的,要反一下 return i; }
intmain(){
redir(); int n1 = read(a), n2 = read(b); int n3 = add(a, n1, b, n2, c); for (int i = 0; i < n3; ++i) putchar(c[i]); return0; }
intread(char *s){ int i = 0; char c = getchar(); while (c < '0' || c > '9') c = getchar(); for (; c >= '0' && c <= '9'; c = getchar()) s[i++] = c; return i; }
voidrev(char *s, int n){ char tmp; for (int i = 0, j = n - 1; i < j; ++i, --j) { tmp = s[i]; s[i] = s[j]; s[j] = tmp; } }
boolcomp(char *s1, int n1, char *s2, int n2){ // return s1 >= s2 if (n1 != n2) return n1 >= n2; for (int i = 0; i < n1; ++i) { if (s1[i] != s2[i]) return s1[i] >= s2[i]; } returntrue; // s1 == s2 }
inthp_minus(char *s1, int n1, char *s2, int n2, char *s3){ int idx = 0, num, f = 0, neg = 0; // f 借位, neg 是否为负数 if (!comp(s1, n1, s2, n2)) { char* tmp = s1; s1 = s2; s2 = tmp; neg = n1; n1 = n2; n2 = neg; neg = 1; } for (--n1, --n2; n1 >= 0; --n1, --n2) { num = s1[n1] - (n2 >= 0 ? s2[n2] : '0') - f; if (num < 0) { f = 1; num += 10; } else f = 0; s3[idx++] = num + '0'; } while (idx > 0 && s3[idx - 1] == '0') --idx; if (neg == 1 && idx > 0) s3[idx++] = '-'; if (idx == 0) s3[idx++] = '0'; rev(s3, idx); return idx; }
intmain(){
redir(); int n1 = read(a), n2 = read(b); int n3 = hp_minus(a, n1, b, n2, c); for (int i = 0; i < n3; ++i) putchar(c[i]); return0; }
ostream &operator<<(ostream &out, const number &a) { if (a.neg) out << '-'; out << a.num; return out; }
number operator+(const number &a, const number &b) { // 加法, 不考虑负数 stringstream ss; auto x = a.num.rbegin(), y = b.num.rbegin(); int c = 0; // 进位标志 while (x != a.num.rend() || y != b.num.rend() || c > 0) { if (x != a.num.rend()) { c += (*x) - '0'; ++x; } if (y != b.num.rend()) { c += (*y) - '0'; ++y; } ss << c % 10; c /= 10; } string ans = ss.str(); // 结果是反的,要逆过来存下 reverse(ans.begin(), ans.end()); returnnumber(ans); } booloperator<(const number &a, const number &b) { // 比较大小,不考虑负数 int n1 = a.num.size(), n2 = b.num.size(); if (n1 != n2) return n1 < n2; for (auto x = a.num.begin(), y = b.num.begin(); x != a.num.end(); ++x, ++y) { if ((*x) != (*y)) return (*x) < (*y); } returnfalse; // 不考虑等于 }
number operator-(const number &a, const number &b) { // 减法,结果可能是负数 if (a < b) { auto ans = b - a; ans.neg = true; return ans; } stringstream ss; auto x = a.num.rbegin(), y = b.num.rbegin(); // 借位标志 c for (int c = 0; x != a.num.rend(); ++x) { c = (*x) - '0' - c; if (y != b.num.rend()) { c -= (*y) - '0'; ++y; } ss << (c + 10) % 10; if (c < 0) c = 1; else c = 0; } string ans = ss.str(); while (ans.back() == '0') ans.pop_back(); reverse(ans.begin(), ans.end()); if (ans.size() == 0) returnnumber(0); returnnumber(ans); }
number operator*(const number &a, constint b) { // 与个位数的乘法 if (b == 0) returnnumber(0); stringstream ss; int c = 0; // 进位标志 for (auto x = a.num.rbegin(); x != a.num.rend() || c != 0; c /= 10) { if (x != a.num.rend()){ c = (*x - '0') * b + c; ++x; } ss << c % 10; } string ans = ss.str(); reverse(ans.begin(), ans.end()); returnnumber(ans); }
number operator*(const number &a, const number &b) { // 高精度乘法 number ans(0); int cnt = 0; // 移位标志 for (auto x = b.num.rbegin(); x != b.num.rend(); ++x, ++cnt) { auto tmp = a * (*x - '0'); if (tmp.num == "0") continue; for (int i = 0; i < cnt; ++i) tmp.num.push_back('0'); ans = ans + tmp; } return ans; }
ostream &operator<<(ostream &out, const number &a) { if (a.neg) out << '-'; out << a.num; return out; }
number operator+(const number &a, const number &b) { // 加法, 不考虑负数 stringstream ss; auto x = a.num.rbegin(), y = b.num.rbegin(); int c = 0; // 进位标志 while (x != a.num.rend() || y != b.num.rend() || c > 0) { if (x != a.num.rend()) { c += (*x) - '0'; ++x; } if (y != b.num.rend()) { c += (*y) - '0'; ++y; } ss << c % 10; c /= 10; } string ans = ss.str(); // 结果是反的,要逆过来存下 reverse(ans.begin(), ans.end()); returnnumber(ans); } booloperator<(const number &a, const number &b) { // 比较大小,不考虑负数 int n1 = a.num.size(), n2 = b.num.size(); if (n1 != n2) return n1 < n2; for (auto x = a.num.begin(), y = b.num.begin(); x != a.num.end(); ++x, ++y) { if ((*x) != (*y)) return (*x) < (*y); } returnfalse; // 不考虑等于 } booloperator>=(const number &a, const number &b) { // 比较大小,不考虑负数 int n1 = a.num.size(), n2 = b.num.size(); if (n1 != n2) return n1 > n2; for (auto x = a.num.begin(), y = b.num.begin(); x != a.num.end(); ++x, ++y) { if ((*x) != (*y)) return (*x) > (*y); } returntrue; // 考虑等于 }
number operator-(const number &a, const number &b) { // 减法,结果可能是负数 if (a < b) { auto ans = b - a; ans.neg = true; return ans; } stringstream ss; auto x = a.num.rbegin(), y = b.num.rbegin(); // 借位标志 c for (int c = 0; x != a.num.rend(); ++x) { c = (*x) - '0' - c; if (y != b.num.rend()) { c -= (*y) - '0'; ++y; } ss << (c + 10) % 10; if (c < 0) c = 1; else c = 0; } string ans = ss.str(); while (ans.back() == '0') ans.pop_back(); reverse(ans.begin(), ans.end()); if (ans.size() == 0) returnnumber(0); returnnumber(ans); }
number operator*(const number &a, constint b) { // 与个位数的乘法 if (b == 0) returnnumber(0); stringstream ss; int c = 0; // 进位标志 for (auto x = a.num.rbegin(); x != a.num.rend() || c != 0; c /= 10) { if (x != a.num.rend()){ c = (*x - '0') * b + c; ++x; } ss << c % 10; } string ans = ss.str(); reverse(ans.begin(), ans.end()); returnnumber(ans); }
number operator*(const number &a, const number &b) { // 高精度乘法,不考虑负数 number ans(0); int cnt = 0; // 移位标志 for (auto x = b.num.rbegin(); x != b.num.rend(); ++x, ++cnt) { auto tmp = a * (*x - '0'); if (tmp.num == "0") continue; // 0 不需要处理 for (int i = 0; i < cnt; ++i) tmp.num.push_back('0'); ans = ans + tmp; } return ans; }
number operator/(const number &a, const number &b) { // 高精度除法,只保留整数部分,不考虑负数 if (a < b) returnnumber(0); string tmp = a.num.substr(0, b.num.size()); number base(tmp); stringstream ss; for (auto x = a.num.begin() + b.num.size(); ; x++) { int cnt = 0; while (base >= b * (cnt + 1)) ++cnt; ss << cnt; if (x == a.num.end()) break; base = base - (b * cnt); if (base.num == "0") base.num = *x; else base.num.push_back(*x); } string ans = ss.str(); // 去掉前置 0 reverse(ans.begin(), ans.end()); while (ans.back() == '0') ans.pop_back(); reverse(ans.begin(), ans.end()); returnnumber(ans); }
ostream &operator<<(ostream &out, const number &a) { if (a.neg) out << '-'; out << a.num; return out; }
number operator+(const number &a, const number &b) { // 加法, 不考虑负数 stringstream ss; auto x = a.num.rbegin(), y = b.num.rbegin(); int c = 0; // 进位标志 while (x != a.num.rend() || y != b.num.rend() || c > 0) { if (x != a.num.rend()) { c += (*x) - '0'; ++x; } if (y != b.num.rend()) { c += (*y) - '0'; ++y; } ss << c % 10; c /= 10; } string ans = ss.str(); // 结果是反的,要逆过来存下 reverse(ans.begin(), ans.end()); returnnumber(ans); }
number operator*(const number &a, constint b) { // 与个位数的乘法 if (b == 0) returnnumber(0); stringstream ss; int c = 0; // 进位标志 for (auto x = a.num.rbegin(); x != a.num.rend() || c != 0; c /= 10) { if (x != a.num.rend()){ c = (*x - '0') * b + c; ++x; } ss << c % 10; } string ans = ss.str(); reverse(ans.begin(), ans.end()); returnnumber(ans); }
number operator*(const number &a, const number &b) { // 高精度乘法,不考虑负数 number ans(0); int cnt = 0; // 移位标志 for (auto x = b.num.rbegin(); x != b.num.rend(); ++x, ++cnt) { auto tmp = a * (*x - '0'); if (tmp.num == "0") continue; // 0 不需要处理 for (int i = 0; i < cnt; ++i) tmp.num.push_back('0'); ans = ans + tmp; } return ans; }
number factorial(longlong n){ // 计算 n 的阶乘,应用高精度乘法和加法即可 number ans(1), cnt(1); for (longlong i = 2; i <= n; ++i) { cnt = cnt * number(i); ans = ans + cnt; } return ans; }