fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. typedef long long ll;
  6. typedef pair<int, int> ii;
  7.  
  8. const int INF = 1e9;
  9. const ll LINF = 1e18;
  10.  
  11. struct BigInt {
  12. static const int BASE = 1e9;
  13. static const int B = 9;
  14.  
  15. vector<int> a;
  16.  
  17. BigInt() {}
  18.  
  19. BigInt(ll x) {
  20. for (; x > 0; x /= BASE) {
  21. a.push_back(x % BASE);
  22. }
  23. }
  24.  
  25. BigInt(const string &s) {
  26. for (int i = (int)s.size() - 1; i >= 0; i -= B) {
  27. int beg = max(0, i - B + 1);
  28. int len = i - beg + 1;
  29. int block = stoi(s.substr(beg, len));
  30. a.push_back(block);
  31. }
  32. trim();
  33. }
  34.  
  35. void trim() {
  36. while (!a.empty() && a.back() == 0) {
  37. a.pop_back();
  38. }
  39. }
  40.  
  41. bool isZero() {
  42. return (a.empty());
  43. }
  44.  
  45. BigInt operator+=(const BigInt &other) {
  46. int n = a.size(), m = other.a.size();
  47. int carry = 0;
  48. for (int i = 0; i < max(n, m) || carry; i++) {
  49. if (i == a.size()) {
  50. a.push_back(0);
  51. }
  52. a[i] += (i < m ? other.a[i] : 0) + carry;
  53. carry = (a[i] >= BASE);
  54. if (carry) a[i] -= BASE;
  55. }
  56. return *this;
  57. }
  58.  
  59. BigInt operator-=(const BigInt &other) {
  60. int m = other.a.size();
  61. int carry = 0;
  62. for (int i = 0; i < m || carry; i++) {
  63. a[i] -= (i < m ? other.a[i] : 0) + carry;
  64. carry = (a[i] < 0);
  65. if (carry) a[i] += BASE;
  66. }
  67. trim();
  68. return *this;
  69. }
  70.  
  71. BigInt operator*=(const BigInt &other) {
  72. int n = a.size(), m = other.a.size();
  73. vector<int> c(n + m, 0);
  74. for (int i = 0; i < n; i++) {
  75. int carry = 0;
  76. for (int j = 0; j < m || carry; j++) {
  77. ll cur = c[i + j] + 1ll * a[i] * (j < m ? other.a[j] : 0) + carry;
  78. c[i + j] = cur % BASE;
  79. carry = cur / BASE;
  80. }
  81. }
  82. swap(a, c);
  83. trim();
  84. return *this;
  85. }
  86.  
  87. BigInt operator+(const BigInt &other) const {
  88. return BigInt(*this) += other;
  89. }
  90.  
  91. BigInt operator-(const BigInt &other) const {
  92. return BigInt(*this) -= other;
  93. }
  94.  
  95. BigInt operator*(const BigInt &other) const {
  96. return BigInt(*this) *= other;
  97. }
  98.  
  99. bool operator<(const BigInt &other) const {
  100. int n = a.size(), m = other.a.size();
  101. if (n != m) {
  102. return (n < m);
  103. }
  104. for (int i = n - 1; i >= 0; i--) {
  105. if (a[i] != other.a[i]) {
  106. return (a[i] < other.a[i]);
  107. }
  108. }
  109. return false;
  110. }
  111.  
  112. friend istream& operator>>(istream &in, BigInt &num) {
  113. string s;
  114. in >> s;
  115. num = BigInt(s);
  116. return in;
  117. }
  118.  
  119. friend ostream& operator<<(ostream &out, const BigInt &num) {
  120. out << (num.a.empty() ? 0 : num.a.back());
  121. for (int i = (int)num.a.size() - 2; i >= 0; i--) {
  122. out << setw(B) << setfill('0') << num.a[i];
  123. }
  124. return out;
  125. }
  126. };
  127.  
  128. int main() {
  129. ios::sync_with_stdio(false);
  130. cin.tie(nullptr);
  131. BigInt a, b;
  132. cin >> a >> b;
  133.  
  134. cout << a + b << '\n';
  135.  
  136. if (a < b) {
  137. cout << '-' << b - a << '\n';
  138. }
  139. else {
  140. cout << a - b << '\n';
  141. }
  142.  
  143. cout << a * b << '\n';
  144. }
Success #stdin #stdout 0.01s 5288KB
stdin
10
11
stdout
21
-1
110