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*=(int b) {
  72. if (b >= BASE) {
  73. return *this *= BigInt(b);
  74. }
  75. int n = a.size();
  76. int carry = 0;
  77. for (int i = 0; i < n || carry; i++) {
  78. if (i == a.size()) {
  79. a.push_back(0);
  80. }
  81. ll cur = 1ll * a[i] * b + carry;
  82. a[i] = cur % BASE;
  83. carry = cur / BASE;
  84. }
  85. trim();
  86. return *this;
  87. }
  88.  
  89. BigInt operator*=(const BigInt &other) {
  90. int n = a.size(), m = other.a.size();
  91. vector<int> c(n + m, 0);
  92. for (int i = 0; i < n; i++) {
  93. int carry = 0;
  94. for (int j = 0; j < m || carry; j++) {
  95. ll cur = c[i + j] + 1ll * a[i] * (j < m ? other.a[j] : 0) + carry;
  96. c[i + j] = cur % BASE;
  97. carry = cur / BASE;
  98. }
  99. }
  100. swap(a, c);
  101. trim();
  102. return *this;
  103. }
  104.  
  105. BigInt operator/=(int b) {
  106. int n = a.size();
  107. int carry = 0;
  108. for (int i = n - 1; i >= 0; i--) {
  109. ll cur = a[i] + 1ll * carry * BASE;
  110. a[i] = cur / b;
  111. carry = cur % b;
  112. }
  113. trim();
  114. return *this;
  115. }
  116.  
  117. BigInt operator+(const BigInt &other) const {
  118. return BigInt(*this) += other;
  119. }
  120.  
  121. BigInt operator-(const BigInt &other) const {
  122. return BigInt(*this) -= other;
  123. }
  124.  
  125. BigInt operator*(int b) const {
  126. return BigInt(*this) *= b;
  127. }
  128.  
  129. BigInt operator*(const BigInt &other) const {
  130. return BigInt(*this) *= other;
  131. }
  132.  
  133. BigInt operator/(int b) const {
  134. return BigInt(*this) /= b;
  135. }
  136.  
  137. int operator%(int b) const {
  138. int n = a.size();
  139. int carry = 0;
  140. for (int i = n - 1; i >= 0; i--) {
  141. ll cur = a[i] + 1ll * carry * BASE;
  142. carry = cur % b;
  143. }
  144. return carry;
  145. }
  146.  
  147. bool operator==(const BigInt &other) const {
  148. return (a == other.a);
  149. }
  150.  
  151. bool operator!=(const BigInt &other) const {
  152. return !(*this == other);
  153. }
  154.  
  155. bool operator<(const BigInt &other) const {
  156. int n = a.size(), m = other.a.size();
  157. if (n != m) {
  158. return (n < m);
  159. }
  160. for (int i = n - 1; i >= 0; i--) {
  161. if (a[i] != other.a[i]) {
  162. return (a[i] < other.a[i]);
  163. }
  164. }
  165. return false;
  166. }
  167.  
  168. bool operator>(const BigInt &other) const {
  169. return (other < *this);
  170. }
  171.  
  172. bool operator<=(const BigInt &other) const {
  173. return !(other < *this);
  174. }
  175.  
  176. bool operator>=(const BigInt &other) const {
  177. return !(*this < other);
  178. }
  179.  
  180. friend istream& operator>>(istream &in, BigInt &num) {
  181. string s;
  182. in >> s;
  183. num = BigInt(s);
  184. return in;
  185. }
  186.  
  187. friend ostream& operator<<(ostream &out, const BigInt &num) {
  188. out << (num.a.empty() ? 0 : num.a.back());
  189. for (int i = (int)num.a.size() - 2; i >= 0; i--) {
  190. out << setw(B) << setfill('0') << num.a[i];
  191. }
  192. return out;
  193. }
  194. };
  195.  
  196. int main() {
  197. ios::sync_with_stdio(false);
  198. cin.tie(nullptr);
  199. BigInt a, b;
  200. cin >> a >> b;
  201.  
  202. cout << a + b << '\n';
  203.  
  204. if (a < b) {
  205. cout << '-' << b - a << '\n';
  206. }
  207. else {
  208. cout << a - b << '\n';
  209. }
  210.  
  211. cout << a * b << '\n';
  212. }
  213.  
Success #stdin #stdout 0.01s 5288KB
stdin
10
11
stdout
21
-1
110