Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.10 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. class BigInt
  4. {
  5. private:
  6. vector<int> a;
  7. const int base = 10;
  8. int sign = 1;
  9.  
  10. bool isNull() const;
  11. void clearNulls();
  12. public:
  13. BigInt(vector<int>);
  14. BigInt(int, vector<int>);
  15. BigInt(int);
  16. BigInt(size_t);
  17. BigInt();
  18. BigInt(string);
  19.  
  20. int toInt();
  21. BigInt abs(BigInt);
  22.  
  23. BigInt& operator=(BigInt);
  24. BigInt& operator=(string);
  25. BigInt operator+();
  26. BigInt operator-();
  27. BigInt operator+(int);
  28. BigInt operator-(int);
  29. BigInt operator*(int);
  30. BigInt operator/(int);
  31. BigInt operator%(int);
  32. BigInt operator+(BigInt);
  33. BigInt operator-(BigInt);
  34. BigInt operator*(BigInt);
  35.  
  36. bool operator>(BigInt);
  37. bool operator<(BigInt);
  38. bool operator==(BigInt);
  39. bool operator!=(BigInt);
  40. bool operator<=(BigInt);
  41. bool operator>=(BigInt);
  42.  
  43. BigInt operator+=(int);
  44. BigInt operator+=(BigInt);
  45. BigInt operator-=(int);
  46. BigInt operator-=(BigInt);
  47. BigInt operator*=(int);
  48. BigInt operator*=(BigInt);
  49. BigInt operator/=(int);
  50. BigInt operator%=(int);
  51.  
  52. friend ostream& operator<<(ostream &, const BigInt&);
  53. friend istream& operator>>(istream &, BigInt&);
  54. };
  55.  
  56. BigInt::BigInt()
  57. {
  58. return;
  59. }
  60. BigInt::BigInt(vector<int> _a)
  61. {
  62. sign = 1;
  63. a.resize(_a.size());
  64. for(size_t i = 0; i < a.size(); i++)
  65. a[i] = _a[i];
  66. }
  67. BigInt::BigInt(int _sign, vector<int> _a)
  68. {
  69. sign = _sign;
  70. a.resize(_a.size());
  71. for(size_t i = 0; i < a.size(); i++)
  72. a[i] = _a[i];
  73. }
  74. BigInt::BigInt(size_t x)
  75. {
  76. a.resize(x, 0);
  77. }
  78. BigInt::BigInt(int x)
  79. {
  80. sign = (x >= 0 ? 1 : -1);
  81. while(x)
  82. {
  83. a.push_back(x % base);
  84. x /= base;
  85. }
  86. }
  87. BigInt::BigInt(string x)
  88. {
  89. sign = (x[0] == '-' ? -1 : 1);
  90. while(x.size() && x.back() != '-')
  91. {
  92. a.push_back(x.back() - '0');
  93. x.pop_back();
  94. }
  95. }
  96.  
  97. int BigInt::toInt()
  98. {
  99. if(isNull()) return 0;
  100. int res = 0;
  101. for(size_t i = a.size() - 1; i >= 0; i--)
  102. {
  103. res = res * 10 + a[i];
  104. }
  105. res *= sign;
  106. return res;
  107. }
  108. bool BigInt::isNull() const
  109. {
  110. return a.size() == 1 && a[0] == 0;
  111. }
  112. void BigInt::clearNulls()
  113. {
  114. while(a.size() > 1 && a.back() == 0)
  115. a.pop_back();
  116. }
  117. istream& operator>>(istream &input, BigInt &_a)
  118. {
  119. _a.a.clear();
  120. char x;
  121. input.get(x);
  122. if(x == '-')
  123. _a.sign = -1;
  124. else
  125. {
  126. _a.sign = 1;
  127. _a.a.push_back(x - '0');
  128. }
  129. while(input.get(x))
  130. {
  131. if(x < '0' || x > '9')
  132. {
  133. reverse(_a.a.begin(), _a.a.end());
  134. return input;
  135. }
  136. _a.a.push_back(x - '0');
  137. }
  138. return input;
  139. }
  140. ostream& operator<<(ostream &output, const BigInt &_a)
  141. {
  142. if(_a.sign == -1 && !_a.isNull())
  143. output << '-';
  144. for(int i = (int) _a.a.size() - 1; i >= 0; i--)
  145. output << _a.a[i];
  146. return output;
  147. }
  148. bool BigInt::operator==(BigInt _a)
  149. {
  150. if(isNull() && _a.isNull())
  151. return 1;
  152. if(sign * _a.sign == -1)
  153. return 0;
  154. if(a.size() != _a.a.size())
  155. return 0;
  156. for(int i = (int)a.size() - 1; i >= 0; i--)
  157. if(a[i] != _a.a[i]) return 0;
  158. return 1;
  159. }
  160. BigInt BigInt::abs(BigInt _a)
  161. {
  162. return BigInt(1, _a.a);
  163. }
  164. bool BigInt::operator!=(BigInt _a)
  165. {
  166. return 1 ^ (*this == _a);
  167. }
  168. bool BigInt::operator<(BigInt _a)
  169. {
  170. int x = 0;
  171. if(a.size() == 0 && _a.a.size() == 0)
  172. return 0;
  173. if(sign == -1 && _a.sign == 1)
  174. return 1;
  175. if(sign == 1 && _a.sign == -1)
  176. return 0;
  177. if(sign == -1 && _a.sign == -1)
  178. x = 1;
  179. if(a.size() != _a.a.size())
  180. return a.size() < _a.a.size();
  181. for(int i = (int)a.size() - 1; i >= 0; i--)
  182. if(a[i] < _a.a[i]) return x ^ 1;
  183. else if(a[i] > _a.a[i]) return x ^ 0;
  184. return 0;
  185. }
  186. bool BigInt::operator>=(BigInt _a)
  187. {
  188. return 1 ^ (*this < _a);
  189. }
  190. bool BigInt::operator>(BigInt _a)
  191. {
  192. return (*this >= _a) && (*this != _a);
  193. }
  194. bool BigInt::operator<=(BigInt _a)
  195. {
  196. return 1 ^ (*this > _a);
  197. }
  198. BigInt& BigInt::operator=(BigInt _a)
  199. {
  200. a.clear();
  201. a.resize(_a.a.size());
  202. sign = _a.sign;
  203. for(size_t i = 0; i < _a.a.size(); i++)
  204. a[i] = _a.a[i];
  205. return *this;
  206. }
  207. BigInt& BigInt::operator=(string x)
  208. {
  209. sign = (x[0] == '-' ? -1 : 1);
  210. while(x.size() && x.back() != '-')
  211. {
  212. a.push_back(x.back() - '0');
  213. x.pop_back();
  214. }
  215. return *this;
  216. }
  217. BigInt BigInt::operator+()
  218. {
  219. return BigInt(*this);
  220. }
  221. BigInt BigInt::operator-()
  222. {
  223. return BigInt(-sign, a);
  224. }
  225. BigInt BigInt::operator+(BigInt x)
  226. {
  227. BigInt res;
  228. bool carry = 0;
  229. if(sign == 1 && x.sign == 1)
  230. {
  231. res.sign = 1;
  232. for(size_t i = 0; i < max(a.size(), x.a.size()) || carry; i++)
  233. {
  234. res.a.push_back((i < a.size() ? a[i] : 0) + (i < x.a.size() ? x.a[i] : 0) + carry);
  235. carry = res.a.back() >= base;
  236. res.a.back() %= base;
  237. }
  238. }
  239. else if(sign == 1 && x.sign == -1)
  240. {
  241. if(abs(*this) >= abs(x))
  242. {
  243. res.sign = 1;
  244. for(size_t i = 0; i < a.size() || carry; i++)
  245. {
  246. res.a.push_back(a[i] - (i < x.a.size() ? x.a[i] : 0) - carry);
  247. carry = res.a.back() < 0;
  248. if(carry)
  249. res.a.back() += base;
  250. }
  251. res.clearNulls();
  252. }
  253. else
  254. return -(BigInt(1, x.a) - BigInt(1, a));
  255. }
  256. else if(sign == -1 && x.sign == 1)
  257. {
  258. if(abs(*this) >= abs(x))
  259. return -(BigInt(1, a) - BigInt(1, x.a));
  260. else
  261. return (BigInt(1, x.a) - BigInt(1, a));
  262. }
  263. else if(sign == -1 && x.sign == -1)
  264. {
  265. return -(BigInt(1, a) + BigInt(1, x.a));
  266. }
  267. return res;
  268. }
  269. BigInt BigInt::operator-(BigInt x)
  270. {
  271. return *this + BigInt(-x.sign, x.a);
  272. }
  273. BigInt BigInt::operator+(int x)
  274. {
  275. return *this + BigInt(x);
  276. }
  277. BigInt BigInt::operator-(int x)
  278. {
  279. return *this - BigInt(x);
  280. }
  281. BigInt BigInt::operator+=(int x)
  282. {
  283. *this = *this + BigInt(x);
  284. return *this;
  285. }
  286. BigInt BigInt::operator+=(BigInt _a)
  287. {
  288. *this = *this + _a;
  289. return *this;
  290. }
  291. BigInt BigInt::operator-=(int x)
  292. {
  293. *this = *this - BigInt(x);
  294. return *this;
  295. }
  296. BigInt BigInt::operator-=(BigInt _a)
  297. {
  298. *this = *this - _a;
  299. return *this;
  300. }
  301. BigInt BigInt::operator*(BigInt _a)
  302. {
  303. BigInt res((size_t)(a.size() + _a.a.size()));
  304. res.sign = sign * _a.sign;
  305. for(size_t i = 0; i < a.size(); i++)
  306. {
  307. int carry = 0;
  308. for(size_t j = 0; j < _a.a.size() || carry; j++)
  309. {
  310. long long cur = res.a[i + j] + a[i] * 1ll * (j < _a.a.size() ? _a.a[j] : 0) + carry;
  311. res.a[i + j] = int(cur % base);
  312. carry = int(cur / base);
  313. }
  314. }
  315. res.clearNulls();
  316. return res;
  317. }
  318. BigInt BigInt::operator*(int x)
  319. {
  320. return *this * BigInt(x);
  321. }
  322. BigInt BigInt::operator*=(BigInt x)
  323. {
  324. *this = *this * x;
  325. return *this;
  326. }
  327. BigInt BigInt::operator*=(int x)
  328. {
  329. *this *= BigInt(x);
  330. return *this;
  331. }
  332. BigInt BigInt::operator/(int x)
  333. {
  334. if(x == 0)
  335. throw "Division by zero";
  336. BigInt res(a.size());
  337. int carry = 0;
  338. for(int i = (int)a.size() - 1; i >= 0; i--)
  339. {
  340. long long cur = a[i] + carry * 1ll * base;
  341. res.a[i] = int(cur / x);
  342. carry = int(cur % x);
  343. }
  344. res.clearNulls();
  345. return res;
  346. }
  347. BigInt BigInt::operator/=(int x)
  348. {
  349. *this = *this / x;
  350. return *this;
  351. }
  352. BigInt BigInt::operator%(int x)
  353. {
  354. if(x == 0)
  355. throw "Division by zero";
  356. BigInt res(a.size());
  357. int carry = 0;
  358. for(int i = (int)a.size() - 1; i >= 0; i--)
  359. {
  360. long long cur = a[i] + carry * 1ll * base;
  361. res.a[i] = int(cur / x);
  362. carry = int(cur % x);
  363. }
  364. res.clearNulls();
  365. return BigInt(carry);
  366. }
  367. BigInt BigInt::operator%=(int x)
  368. {
  369. *this = *this % x;
  370. return *this;
  371. }
  372. int main()
  373. {
  374. BitInt a, b;
  375. cin >> a >> b;
  376. return 0;
  377. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement