fork download
  1. #include<deque>
  2. #include<string>
  3. #include<vector>
  4. #include<complex>
  5. #include<assert.h>
  6. #include<algorithm>
  7. #define PI 3.14159265358979323846
  8. void fft(std::vector<std::complex<double>>&a, bool invert)
  9. {
  10. for (int i = 1, j = 0; i < a.size(); i++)
  11. {
  12. int bit = int(a.size()) >> 1;
  13. for (; j & bit; bit >>= 1)
  14. {
  15. j ^= bit;
  16. }
  17. j ^= bit;
  18. if (i < j)
  19. {
  20. swap(a[i], a[j]);
  21. }
  22. }
  23. for (int len = 2; len <= a.size(); len <<= 1)
  24. {
  25. double ang = 2 * PI / len * (invert ? -1 : 1);
  26. std::complex<double>wlen(cos(ang), sin(ang));
  27. for (int i = 0; i < a.size(); i += len)
  28. {
  29. std::complex<double>w(1);
  30. for (int j = 0; j < (len >> 1); j++)
  31. {
  32. std::complex<double>u = a[i + j], v = a[i + j + (len >> 1)] * w;
  33. a[i + j] = u + v;
  34. a[i + j + (len >> 1)] = u - v;
  35. w *= wlen;
  36. }
  37. }
  38. }
  39. if (invert)
  40. {
  41. for (int i = 0; i < a.size(); i++)
  42. {
  43. a[i] /= a.size();
  44. }
  45. }
  46. }
  47. struct BigInt
  48. {
  49. bool is_negative = false;
  50. std::deque<int>Digits = { 0 };
  51. BigInt() { }
  52. template<typename T>
  53. BigInt(T number)
  54. {
  55. (*this) = number;
  56. }
  57. inline operator std::string() const
  58. {
  59. std::stringstream string_stream;
  60. string_stream << *this;
  61. return string_stream.str();
  62. }
  63. inline operator bool() const
  64. {
  65. return *this != 0;
  66. }
  67. inline operator int32_t() const
  68. {
  69. return std::stoi(*this);
  70. }
  71. inline operator int64_t() const
  72. {
  73. return std::stoll(*this);
  74. }
  75. inline operator unsigned() const
  76. {
  77. return std::stoul(*this);
  78. }
  79. inline operator double() const
  80. {
  81. return std::stod(*this);
  82. }
  83. inline operator float() const
  84. {
  85. return std::stof(*this);
  86. }
  87. inline operator char() const
  88. {
  89. return int(*this);
  90. }
  91. inline void shrink_to_fit()
  92. {
  93. int new_size = Digits.size();
  94. while (new_size > 1 && Digits[new_size - 1] == 0)
  95. {
  96. new_size--;
  97. }
  98. Digits.resize(new_size);
  99. }
  100. inline friend std::istream& operator >> (std::istream& input_stream, BigInt& object)
  101. {
  102. std::string number;
  103. input_stream >> number;
  104. object = number;
  105. return input_stream;
  106. }
  107. friend std::ostream& operator << (std::ostream& output_stream, BigInt object)
  108. {
  109. object.shrink_to_fit();
  110. if (object.is_negative && (object.Digits.size() != 1 || object.Digits[0] != 0))
  111. {
  112. output_stream << '-';
  113. }
  114. bool leading_zeros = true;
  115. for (int i = object.Digits.size() - 1; i >= 0; i--)
  116. {
  117. leading_zeros &= !object.Digits[i];
  118. if (object.Digits[i] || !leading_zeros)
  119. {
  120. output_stream << object.Digits[i];
  121. }
  122. }
  123. if (leading_zeros)
  124. {
  125. output_stream << '0';
  126. }
  127. return output_stream;
  128. }
  129. BigInt& operator = (std::string number)
  130. {
  131. if (number[0] == '-')
  132. {
  133. is_negative = true;
  134. }
  135. else
  136. {
  137. is_negative = false;
  138. }
  139. Digits.resize(number.size());
  140. int number_size = number.size();
  141. while (number.size() > is_negative)
  142. {
  143. if (number.back() != '.')
  144. {
  145. Digits[number_size - number.size()] = number.back() - '0';
  146. }
  147. else
  148. {
  149. number.pop_back();
  150. *this = number;
  151. return *this;
  152. }
  153. number.pop_back();
  154. }
  155. if (number.size())
  156. {
  157. Digits.pop_back();
  158. }
  159. return *this;
  160. }
  161. template<typename T>
  162. inline BigInt& operator = (T number)
  163. {
  164. std::stringstream string_stream;
  165. string_stream << std::fixed << number;
  166. *this = string_stream.str();
  167. return *this;
  168. }
  169. friend bool operator > (BigInt first_object, BigInt second_object)
  170. {
  171. if (first_object.is_negative)
  172. {
  173. if (second_object.is_negative)
  174. {
  175. first_object.is_negative = false;
  176. second_object.is_negative = false;
  177. return first_object < second_object;
  178. }
  179. else
  180. {
  181. return false;
  182. }
  183. }
  184. else if (second_object.is_negative)
  185. {
  186. return true;
  187. }
  188. if (first_object.Digits.size() < second_object.Digits.size())
  189. {
  190. first_object.Digits.resize(second_object.Digits.size());
  191. }
  192. else if (first_object.Digits.size() > second_object.Digits.size())
  193. {
  194. second_object.Digits.resize(first_object.Digits.size());
  195. }
  196. for (int i = first_object.Digits.size() - 1; i >= 0; i--)
  197. {
  198. if (first_object.Digits[i] < second_object.Digits[i])
  199. {
  200. return false;
  201. }
  202. else if (first_object.Digits[i] > second_object.Digits[i])
  203. {
  204. return true;
  205. }
  206. }
  207. return false;
  208. }
  209. friend bool operator < (BigInt first_object, BigInt second_object)
  210. {
  211. if (first_object.is_negative)
  212. {
  213. if (second_object.is_negative)
  214. {
  215. first_object.is_negative = false;
  216. second_object.is_negative = false;
  217. return first_object > second_object;
  218. }
  219. else
  220. {
  221. return true;
  222. }
  223. }
  224. else if (second_object.is_negative)
  225. {
  226. return false;
  227. }
  228. if (first_object.Digits.size() < second_object.Digits.size())
  229. {
  230. first_object.Digits.resize(second_object.Digits.size());
  231. }
  232. else if (first_object.Digits.size() > second_object.Digits.size())
  233. {
  234. second_object.Digits.resize(first_object.Digits.size());
  235. }
  236. for (int i = first_object.Digits.size() - 1; i >= 0; i--)
  237. {
  238. if (first_object.Digits[i] < second_object.Digits[i])
  239. {
  240. return true;
  241. }
  242. else if (first_object.Digits[i] > second_object.Digits[i])
  243. {
  244. return false;
  245. }
  246. }
  247. return false;
  248. }
  249. friend bool operator == (BigInt first_object, BigInt second_object)
  250. {
  251. if (first_object.Digits.size() < second_object.Digits.size())
  252. {
  253. first_object.Digits.resize(second_object.Digits.size());
  254. }
  255. else if (first_object.Digits.size() > second_object.Digits.size())
  256. {
  257. second_object.Digits.resize(first_object.Digits.size());
  258. }
  259. if (first_object.Digits == second_object.Digits)
  260. {
  261. first_object.shrink_to_fit();
  262. second_object.shrink_to_fit();
  263. return (!(first_object.is_negative ^ second_object.is_negative) || (first_object.Digits.size() == 1 && first_object.Digits[0] == 0));
  264. }
  265. else
  266. {
  267. return false;
  268. }
  269. }
  270. inline friend bool operator >= (BigInt first_object, BigInt second_object)
  271. {
  272. return first_object > second_object || first_object == second_object;
  273. }
  274. inline friend bool operator <= (BigInt first_object, BigInt second_object)
  275. {
  276. return first_object < second_object || first_object == second_object;
  277. }
  278. inline friend bool operator != (BigInt first_object, BigInt second_object)
  279. {
  280. return !(first_object == second_object);
  281. }
  282. template<typename T>
  283. inline friend bool operator > (BigInt first_object, T number)
  284. {
  285. BigInt second_object;
  286. second_object = number;
  287. return first_object > second_object;
  288. }
  289. template<typename T>
  290. inline friend bool operator < (BigInt first_object, T number)
  291. {
  292. BigInt second_object;
  293. second_object = number;
  294. return first_object < second_object;
  295. }
  296. template<typename T>
  297. inline friend bool operator == (BigInt first_object, T number)
  298. {
  299. BigInt second_object;
  300. second_object = number;
  301. return first_object == second_object;
  302. }
  303. template<typename T>
  304. inline friend bool operator >= (BigInt first_object, T number)
  305. {
  306. BigInt second_object;
  307. second_object = number;
  308. return first_object >= second_object;
  309. }
  310. template<typename T>
  311. inline friend bool operator <= (BigInt first_object, T number)
  312. {
  313. BigInt second_object;
  314. second_object = number;
  315. return first_object <= second_object;
  316. }
  317. template<typename T>
  318. inline friend bool operator != (BigInt first_object, T number)
  319. {
  320. BigInt second_object;
  321. second_object = number;
  322. return first_object != second_object;
  323. }
  324. template<typename T>
  325. inline friend bool operator > (T number, BigInt second_object)
  326. {
  327. BigInt first_object;
  328. first_object = number;
  329. return first_object > second_object;
  330. }
  331. template<typename T>
  332. inline friend bool operator < (T number, BigInt second_object)
  333. {
  334. BigInt first_object;
  335. first_object = number;
  336. return first_object < second_object;
  337. }
  338. template<typename T>
  339. inline friend bool operator == (T number, BigInt second_object)
  340. {
  341. BigInt first_object;
  342. first_object = number;
  343. return first_object == second_object;
  344. }
  345. template<typename T>
  346. inline friend bool operator >= (T number, BigInt second_object)
  347. {
  348. BigInt first_object;
  349. first_object = number;
  350. return first_object >= second_object;
  351. }
  352. template<typename T>
  353. inline friend bool operator <= (T number, BigInt second_object)
  354. {
  355. BigInt first_object;
  356. first_object = number;
  357. return first_object <= second_object;
  358. }
  359. template<typename T>
  360. inline friend bool operator != (T number, BigInt second_object)
  361. {
  362. BigInt first_object;
  363. first_object = number;
  364. return first_object != second_object;
  365. }
  366. friend BigInt operator + (BigInt first_object, BigInt second_object)
  367. {
  368. if (second_object.is_negative)
  369. {
  370. second_object.is_negative = false;
  371. return first_object - second_object;
  372. }
  373. if (first_object.is_negative)
  374. {
  375. first_object.is_negative = false;
  376. return second_object - first_object;
  377. }
  378. int carry = 0;
  379. first_object.Digits.resize((first_object.Digits.size() > second_object.Digits.size() ? first_object.Digits.size() : second_object.Digits.size()) + 1);
  380. if (first_object.Digits.size() > second_object.Digits.size())
  381. {
  382. second_object.Digits.resize(first_object.Digits.size());
  383. }
  384. for (int i = 0; i < first_object.Digits.size() - 1; i++)
  385. {
  386. first_object.Digits[i] += carry + second_object.Digits[i];
  387. carry = first_object.Digits[i] / 10;
  388. first_object.Digits[i] %= 10;
  389. }
  390. first_object.Digits[first_object.Digits.size() - 1] += carry;
  391. first_object.shrink_to_fit();
  392. return first_object;
  393. }
  394. friend BigInt operator - (BigInt first_object, BigInt second_object)
  395. {
  396. if (second_object.is_negative)
  397. {
  398. second_object.is_negative = false;
  399. return first_object + second_object;
  400. }
  401. if (first_object < second_object)
  402. {
  403. first_object = second_object - first_object;
  404. first_object.is_negative = !first_object.is_negative;
  405. return first_object;
  406. }
  407. bool borrow = false;
  408. second_object.Digits.resize(first_object.Digits.size());
  409. for (int i = 0; i < first_object.Digits.size(); i++)
  410. {
  411. first_object.Digits[i] -= second_object.Digits[i] + borrow;
  412. if (first_object.Digits[i] < 0)
  413. {
  414. first_object.Digits[i] += 10;
  415. borrow = true;
  416. }
  417. else
  418. {
  419. borrow = false;
  420. }
  421. }
  422. first_object.shrink_to_fit();
  423. return first_object;
  424. }
  425. friend BigInt operator * (BigInt first_object, BigInt second_object)
  426. {
  427. if (first_object.Digits.size() * second_object.Digits.size() <= (first_object.Digits.size() + second_object.Digits.size()) * log2(first_object.Digits.size() + second_object.Digits.size()))
  428. {
  429. BigInt result;
  430. result.Digits.resize(first_object.Digits.size() + second_object.Digits.size());
  431. for (int i = 0; i < second_object.Digits.size(); i++)
  432. {
  433. int carry = 0;
  434. for (int j = 0; j < first_object.Digits.size(); j++)
  435. {
  436. result.Digits[i + j] += first_object.Digits[j] * second_object.Digits[i] + carry;
  437. carry = result.Digits[i + j] / 10;
  438. result.Digits[i + j] %= 10;
  439. }
  440. result.Digits[i + first_object.Digits.size()] = carry;
  441. }
  442. result.shrink_to_fit();
  443. result.is_negative = first_object.is_negative ^ second_object.is_negative;
  444. return result;
  445. }
  446. first_object.is_negative ^= second_object.is_negative;
  447. int new_size = 1;
  448. while (new_size < first_object.Digits.size() + second_object.Digits.size())
  449. {
  450. new_size <<= 1;
  451. }
  452. first_object.Digits.resize(new_size);
  453. second_object.Digits.resize(new_size);
  454. std::vector<std::complex<double>>fa(first_object.Digits.begin(), first_object.Digits.end()), fb(second_object.Digits.begin(), second_object.Digits.end());
  455. fft(fa, false);
  456. fft(fb, false);
  457. for (int i = 0; i < new_size; i++)
  458. {
  459. fa[i] *= fb[i];
  460. }
  461. fft(fa, true);
  462. for (int i = 0; i < new_size; i++)
  463. {
  464. first_object.Digits[i] = round(fa[i].real());
  465. }
  466. int carry = 0;
  467. for (int i = 0; i < new_size; i++)
  468. {
  469. first_object.Digits[i] += carry;
  470. carry = first_object.Digits[i] / 10;
  471. first_object.Digits[i] %= 10;
  472. }
  473. first_object.shrink_to_fit();
  474. return first_object;
  475. }
  476. friend BigInt operator / (BigInt first_object, BigInt second_object)
  477. {
  478. assert(second_object != 0);
  479. BigInt result, carry;
  480. result.is_negative = first_object.is_negative ^ second_object.is_negative;
  481. first_object.is_negative = false;
  482. second_object.is_negative = false;
  483. carry.Digits.resize(first_object.Digits.size());
  484. result.Digits.resize(first_object.Digits.size());
  485. for (int i = first_object.Digits.size() - 1; i >= 0; i--)
  486. {
  487. carry.Digits.push_front(first_object.Digits[i]);
  488. int answer = 9;
  489. BigInt temp = second_object * 9;
  490. while (answer >= 0)
  491. {
  492. if (temp <= carry)
  493. {
  494. break;
  495. }
  496. temp -= second_object;
  497. answer--;
  498. }
  499. result.Digits[i] = answer;
  500. carry -= second_object * answer;
  501. }
  502. result.shrink_to_fit();
  503. return result;
  504. }
  505. friend BigInt operator % (BigInt first_object, BigInt second_object)
  506. {
  507. assert(second_object != 0);
  508. BigInt result, carry;
  509. bool negative_temp = first_object.is_negative;
  510. first_object.is_negative = false;
  511. second_object.is_negative = false;
  512. carry.Digits.resize(first_object.Digits.size());
  513. result.Digits.resize(first_object.Digits.size());
  514. for (int i = first_object.Digits.size() - 1; i >= 0; i--)
  515. {
  516. carry.Digits.push_front(first_object.Digits[i]);
  517. int answer = 9;
  518. BigInt temp = second_object * 9;
  519. while (answer >= 0)
  520. {
  521. if (temp <= carry)
  522. {
  523. break;
  524. }
  525. temp -= second_object;
  526. answer--;
  527. }
  528. result.Digits[i] = answer;
  529. carry -= second_object * answer;
  530. }
  531. carry.is_negative = negative_temp;
  532. carry.shrink_to_fit();
  533. return carry;
  534. }
  535. friend BigInt operator | (BigInt first_object, BigInt second_object)
  536. {
  537. if (first_object.is_negative)
  538. {
  539. return ~(~first_object & ~second_object);
  540. }
  541. bool flip = false;
  542. if (second_object.is_negative)
  543. {
  544. flip = true;
  545. second_object = ~second_object;
  546. }
  547. std::string first_number, second_number;
  548. while (first_object)
  549. {
  550. int64_t block = first_object % (1 << 30);
  551. first_object /= (1 << 30);
  552. for (int i = 0; i < 30; i++)
  553. {
  554. if (!block && !first_object)
  555. {
  556. break;
  557. }
  558. first_number.push_back((block % 2) + '0');
  559. block /= 2;
  560. }
  561. }
  562. while (second_object)
  563. {
  564. int64_t block = second_object % (1 << 30);
  565. second_object /= (1 << 30);
  566. for (int i = 0; i < 30; i++)
  567. {
  568. if (!block && !second_object)
  569. {
  570. break;
  571. }
  572. second_number.push_back((block % 2) + '0');
  573. block /= 2;
  574. }
  575. }
  576. first_number.push_back('0');
  577. second_number.push_back('0');
  578. while (first_number.size() < second_number.size())
  579. {
  580. first_number.push_back('0');
  581. }
  582. while (first_number.size() > second_number.size())
  583. {
  584. second_number.push_back('0');
  585. }
  586. if (flip)
  587. {
  588. for (int i = 0; i < second_number.size(); i++)
  589. {
  590. if (second_number[i] == '0')
  591. {
  592. second_number[i] = '1';
  593. }
  594. else
  595. {
  596. second_number[i] = '0';
  597. }
  598. }
  599. }
  600. for (int i = 0; i < first_number.size(); i++)
  601. {
  602. first_number[i] = ((first_number[i] - '0') || (second_number[i] - '0')) + '0';
  603. }
  604. if (first_number.back() - '0')
  605. {
  606. flip = true;
  607. for (int i = 0; i < first_number.size(); i++)
  608. {
  609. if (first_number[i] == '0')
  610. {
  611. first_number[i] = '1';
  612. }
  613. else
  614. {
  615. first_number[i] = '0';
  616. }
  617. }
  618. }
  619. else
  620. {
  621. flip = false;
  622. }
  623. BigInt answer, temp = 1;
  624. reverse(first_number.begin(), first_number.end());
  625. while (first_number.size())
  626. {
  627. if (first_number.back() - '0')
  628. {
  629. answer += temp;
  630. }
  631. temp += temp;
  632. first_number.pop_back();
  633. }
  634. if (flip)
  635. {
  636. return ~answer;
  637. }
  638. return answer;
  639. }
  640. friend BigInt operator & (BigInt first_object, BigInt second_object)
  641. {
  642. if (first_object.is_negative)
  643. {
  644. return ~(~first_object | ~second_object);
  645. }
  646. bool flip = false;
  647. if (second_object.is_negative)
  648. {
  649. flip = true;
  650. second_object = ~second_object;
  651. }
  652. std::string first_number, second_number;
  653. while (first_object)
  654. {
  655. int64_t block = first_object % (1 << 30);
  656. first_object /= (1 << 30);
  657. for (int i = 0; i < 30; i++)
  658. {
  659. if (!block && !first_object)
  660. {
  661. break;
  662. }
  663. first_number.push_back((block % 2) + '0');
  664. block /= 2;
  665. }
  666. }
  667. while (second_object)
  668. {
  669. int64_t block = second_object % (1 << 30);
  670. second_object /= (1 << 30);
  671. for (int i = 0; i < 30; i++)
  672. {
  673. if (!block && !second_object)
  674. {
  675. break;
  676. }
  677. second_number.push_back((block % 2) + '0');
  678. block /= 2;
  679. }
  680. }
  681. first_number.push_back('0');
  682. second_number.push_back('0');
  683. while (first_number.size() < second_number.size())
  684. {
  685. first_number.push_back('0');
  686. }
  687. while (first_number.size() > second_number.size())
  688. {
  689. second_number.push_back('0');
  690. }
  691. if (flip)
  692. {
  693. for (int i = 0; i < second_number.size(); i++)
  694. {
  695. if (second_number[i] == '0')
  696. {
  697. second_number[i] = '1';
  698. }
  699. else
  700. {
  701. second_number[i] = '0';
  702. }
  703. }
  704. }
  705. for (int i = 0; i < first_number.size(); i++)
  706. {
  707. first_number[i] = ((first_number[i] - '0') && (second_number[i] - '0')) + '0';
  708. }
  709. if (first_number.back() - '0')
  710. {
  711. flip = true;
  712. for (int i = 0; i < first_number.size(); i++)
  713. {
  714. if (first_number[i] == '0')
  715. {
  716. first_number[i] = '1';
  717. }
  718. else
  719. {
  720. first_number[i] = '0';
  721. }
  722. }
  723. }
  724. else
  725. {
  726. flip = false;
  727. }
  728. BigInt answer, temp = 1;
  729. reverse(first_number.begin(), first_number.end());
  730. while (first_number.size())
  731. {
  732. if (first_number.back() - '0')
  733. {
  734. answer += temp;
  735. }
  736. temp += temp;
  737. first_number.pop_back();
  738. }
  739. if (flip)
  740. {
  741. return ~answer;
  742. }
  743. return answer;
  744. }
  745. friend BigInt operator ^ (BigInt first_object, BigInt second_object)
  746. {
  747. if (first_object == 0)
  748. {
  749. return second_object;
  750. }
  751. if (second_object == 0)
  752. {
  753. return first_object;
  754. }
  755. if (first_object < 0 && second_object < 0)
  756. {
  757. return ~first_object ^ ~second_object;
  758. }
  759. if (first_object < 0)
  760. {
  761. return ~(~first_object ^ second_object);
  762. }
  763. if (second_object < 0)
  764. {
  765. return ~(first_object ^ ~second_object);
  766. }
  767. std::string first_number, second_number;
  768. while (first_object)
  769. {
  770. int64_t block = first_object % (1 << 30);
  771. first_object /= (1 << 30);
  772. for (int i = 0; i < 30; i++)
  773. {
  774. if (!block && !first_object)
  775. {
  776. break;
  777. }
  778. first_number.push_back((block % 2) + '0');
  779. block /= 2;
  780. }
  781. }
  782. while (second_object)
  783. {
  784. int64_t block = second_object % (1 << 30);
  785. second_object /= (1 << 30);
  786. for (int i = 0; i < 30; i++)
  787. {
  788. if (!block && !second_object)
  789. {
  790. break;
  791. }
  792. second_number.push_back((block % 2) + '0');
  793. block /= 2;
  794. }
  795. }
  796. while (first_number.size() < second_number.size())
  797. {
  798. first_number.push_back('0');
  799. }
  800. while (first_number.size() > second_number.size())
  801. {
  802. second_number.push_back('0');
  803. }
  804. for (int i = 0; i < first_number.size(); i++)
  805. {
  806. first_number[i] = ((first_number[i] - '0') ^ (second_number[i] - '0')) + '0';
  807. }
  808. reverse(first_number.begin(), first_number.end());
  809. BigInt answer, temp = 1;
  810. while (first_number.size())
  811. {
  812. if (first_number.back() - '0')
  813. {
  814. answer += temp;
  815. }
  816. temp += temp;
  817. first_number.pop_back();
  818. }
  819. return answer;
  820. }
  821. friend BigInt operator >> (BigInt object, int number)
  822. {
  823. if (number < 0)
  824. {
  825. return object << -number;
  826. }
  827. if (object.is_negative)
  828. {
  829. return ~(~object >> number);
  830. }
  831. std::string bits;
  832. while (object)
  833. {
  834. int64_t block = object % (1 << 30);
  835. object /= (1 << 30);
  836. for (int i = 0; i < 30; i++)
  837. {
  838. if (!block && !object)
  839. {
  840. break;
  841. }
  842. bits.push_back((block % 2) + '0');
  843. block /= 2;
  844. }
  845. }
  846. reverse(bits.begin(), bits.end());
  847. bits.resize((number > bits.size() ? 0 : bits.size() - number));
  848. BigInt answer, temp = 1;
  849. while (bits.size())
  850. {
  851. if (bits.back() - '0')
  852. {
  853. answer += temp;
  854. }
  855. temp += temp;
  856. bits.pop_back();
  857. }
  858. return answer;
  859. }
  860. friend BigInt operator << (BigInt object, int number)
  861. {
  862. if (number == 0)
  863. {
  864. return object;
  865. }
  866. else if (number < 0)
  867. {
  868. return object >> -number;
  869. }
  870. std::string bits;
  871. bool negative_temp = object.is_negative;
  872. object.is_negative = false;
  873. while (object)
  874. {
  875. int64_t block = object % (1 << 30);
  876. object /= (1 << 30);
  877. for (int i = 0; i < 30; i++)
  878. {
  879. if (!block && !object)
  880. {
  881. break;
  882. }
  883. bits.push_back((block % 2) + '0');
  884. block /= 2;
  885. }
  886. }
  887. reverse(bits.begin(), bits.end());
  888. while (number--)
  889. {
  890. bits.push_back('0');
  891. }
  892. BigInt answer, temp = 1;
  893. while (bits.size())
  894. {
  895. if (bits.back() - '0')
  896. {
  897. answer += temp;
  898. }
  899. temp += temp;
  900. bits.pop_back();
  901. }
  902. answer.is_negative = negative_temp;
  903. return answer;
  904. }
  905. template<typename T>
  906. inline friend BigInt operator + (BigInt first_object, T number)
  907. {
  908. BigInt second_object;
  909. second_object = number;
  910. return first_object + second_object;
  911. }
  912. template<typename T>
  913. inline friend BigInt operator - (BigInt first_object, T number)
  914. {
  915. BigInt second_object;
  916. second_object = number;
  917. return first_object - second_object;
  918. }
  919. template<typename T>
  920. inline friend BigInt operator * (BigInt first_object, T number)
  921. {
  922. BigInt second_object;
  923. second_object = number;
  924. return first_object * second_object;
  925. }
  926. template<typename T>
  927. inline friend BigInt operator / (BigInt first_object, T number)
  928. {
  929. BigInt second_object;
  930. second_object = number;
  931. return first_object / second_object;
  932. }
  933. template<typename T>
  934. inline friend BigInt operator % (BigInt first_object, T number)
  935. {
  936. BigInt second_object;
  937. second_object = number;
  938. return first_object % second_object;
  939. }
  940. template<typename T>
  941. inline friend BigInt operator | (BigInt first_object, T number)
  942. {
  943. BigInt second_object;
  944. second_object = number;
  945. return first_object | second_object;
  946. }
  947. template<typename T>
  948. inline friend BigInt operator & (BigInt first_object, T number)
  949. {
  950. BigInt second_object;
  951. second_object = number;
  952. return first_object & second_object;
  953. }
  954. template<typename T>
  955. inline friend BigInt operator ^ (BigInt first_object, T number)
  956. {
  957. BigInt second_object;
  958. second_object = number;
  959. return first_object ^ second_object;
  960. }
  961. template<typename T>
  962. inline friend BigInt operator + (T number, BigInt second_object)
  963. {
  964. BigInt first_object;
  965. first_object = number;
  966. return first_object + second_object;
  967. }
  968. template<typename T>
  969. inline friend BigInt operator - (T number, BigInt second_object)
  970. {
  971. BigInt first_object;
  972. first_object = number;
  973. return first_object - second_object;
  974. }
  975. template<typename T>
  976. inline friend BigInt operator * (T number, BigInt second_object)
  977. {
  978. BigInt first_object;
  979. first_object = number;
  980. return first_object * second_object;
  981. }
  982. template<typename T>
  983. inline friend BigInt operator / (T number, BigInt second_object)
  984. {
  985. BigInt first_object;
  986. first_object = number;
  987. return first_object / second_object;
  988. }
  989. template<typename T>
  990. inline friend BigInt operator % (T number, BigInt second_object)
  991. {
  992. BigInt first_object;
  993. first_object = number;
  994. return first_object % second_object;
  995. }
  996. template<typename T>
  997. inline friend BigInt operator | (T number, BigInt second_object)
  998. {
  999. BigInt first_object;
  1000. first_object = number;
  1001. return first_object | second_object;
  1002. }
  1003. template<typename T>
  1004. inline friend BigInt operator & (T number, BigInt second_object)
  1005. {
  1006. BigInt first_object;
  1007. first_object = number;
  1008. return first_object & second_object;
  1009. }
  1010. template<typename T>
  1011. inline friend BigInt operator ^ (T number, BigInt second_object)
  1012. {
  1013. BigInt first_object;
  1014. first_object = number;
  1015. return first_object ^ second_object;
  1016. }
  1017. template<typename T>
  1018. inline BigInt& operator += (T number)
  1019. {
  1020. *this = *this + number;
  1021. return *this;
  1022. }
  1023. template<typename T>
  1024. inline BigInt& operator -= (T number)
  1025. {
  1026. *this = *this - number;
  1027. return *this;
  1028. }
  1029. template<typename T>
  1030. inline BigInt& operator *= (T number)
  1031. {
  1032. *this = *this * number;
  1033. return *this;
  1034. }
  1035. template<typename T>
  1036. inline BigInt& operator /= (T number)
  1037. {
  1038. *this = *this / number;
  1039. return *this;
  1040. }
  1041. template<typename T>
  1042. inline BigInt& operator %= (T number)
  1043. {
  1044. *this = *this % number;
  1045. return *this;
  1046. }
  1047. template<typename T>
  1048. inline BigInt& operator |= (T number)
  1049. {
  1050. *this = *this | number;
  1051. return *this;
  1052. }
  1053. template<typename T>
  1054. inline BigInt& operator &= (T number)
  1055. {
  1056. *this = *this & number;
  1057. return *this;
  1058. }
  1059. template<typename T>
  1060. inline BigInt& operator ^= (T number)
  1061. {
  1062. *this = *this ^ number;
  1063. return *this;
  1064. }
  1065. inline BigInt& operator >>= (int number)
  1066. {
  1067. *this = *this >> number;
  1068. return *this;
  1069. }
  1070. inline BigInt& operator <<= (int number)
  1071. {
  1072. *this = *this << number;
  1073. return *this;
  1074. }
  1075. inline BigInt& operator ++ ()
  1076. {
  1077. (*this) = (*this) + 1;
  1078. return (*this);
  1079. }
  1080. inline BigInt operator ++ (int32_t)
  1081. {
  1082. (*this) = (*this) + 1;
  1083. return (*this) - 1;
  1084. }
  1085. inline BigInt& operator -- ()
  1086. {
  1087. (*this) = (*this) - 1;
  1088. return (*this);
  1089. }
  1090. inline BigInt operator -- (int32_t)
  1091. {
  1092. (*this) = (*this) - 1;
  1093. return (*this) + 1;
  1094. }
  1095. inline friend BigInt operator - (BigInt object)
  1096. {
  1097. return 0 - object;
  1098. }
  1099. inline BigInt operator ~ ()
  1100. {
  1101. return 0 - (*this) - 1;
  1102. }
  1103. };
  1104. BigInt __gcd(BigInt first_number, BigInt second_number)
  1105. {
  1106. return second_number == 0 ? first_number : __gcd(second_number, first_number % second_number);
  1107. }
  1108. inline BigInt __lcm(BigInt first_number, BigInt second_number)
  1109. {
  1110. return first_number * second_number / __gcd(first_number, second_number);
  1111. }
  1112. BigInt phi(BigInt number)
  1113. {
  1114. BigInt answer = number;
  1115. for (BigInt i = 2; i * i <= number; i++)
  1116. {
  1117. if (number % i == 0)
  1118. {
  1119. while (number % i == 0)
  1120. {
  1121. number /= i;
  1122. }
  1123. answer -= answer / i;
  1124. }
  1125. }
  1126. if (number > 1)
  1127. {
  1128. answer -= answer / number;
  1129. }
  1130. return answer;
  1131. }
  1132. int phi(int number)
  1133. {
  1134. int answer = number;
  1135. for (int i = 2; i * i <= number; i += 1)
  1136. {
  1137. if (number % i == 0)
  1138. {
  1139. while (number % i == 0)
  1140. {
  1141. number /= i;
  1142. }
  1143. answer -= answer / i;
  1144. }
  1145. }
  1146. if (number > 1)
  1147. {
  1148. answer -= answer / number;
  1149. }
  1150. return answer;
  1151. }
  1152. BigInt pow(BigInt base, BigInt power)
  1153. {
  1154. BigInt answer;
  1155. answer = 1;
  1156. while (power)
  1157. {
  1158. if (power.Digits[0] % 2)
  1159. {
  1160. answer *= base;
  1161. }
  1162. base *= base;
  1163. power /= 2;
  1164. }
  1165. return answer;
  1166. }
  1167. BigInt pow(BigInt base, int power)
  1168. {
  1169. if (power < 0)
  1170. {
  1171. return 0;
  1172. }
  1173. BigInt answer;
  1174. answer = 1;
  1175. while (power)
  1176. {
  1177. if (power & 1)
  1178. {
  1179. answer *= base;
  1180. }
  1181. base *= base;
  1182. power >>= 1;
  1183. }
  1184. return answer;
  1185. }
  1186. inline BigInt abs(BigInt number)
  1187. {
  1188. number.is_negative = false;
  1189. return number;
  1190. }
  1191. class BigFloat
  1192. {
  1193. private :
  1194. BigInt Numerator, Denominator;
  1195. public :
  1196. BigFloat() { }
  1197. template<typename T>
  1198. BigFloat(T number)
  1199. {
  1200. (*this) = number;
  1201. }
  1202. inline operator bool() const
  1203. {
  1204. return *this != 0;
  1205. }
  1206. inline operator int() const
  1207. {
  1208. return Numerator / Denominator;
  1209. }
  1210. inline operator unsigned() const
  1211. {
  1212. return Numerator / Denominator;
  1213. }
  1214. inline operator double()
  1215. {
  1216. return stod((*this).str(15));
  1217. }
  1218. inline operator float()
  1219. {
  1220. return stof((*this).str(7));
  1221. }
  1222. inline operator char() const
  1223. {
  1224. return int(*this);
  1225. }
  1226. inline operator BigInt() const
  1227. {
  1228. return Numerator / Denominator;
  1229. }
  1230. inline BigInt numerator()
  1231. {
  1232. return Numerator;
  1233. }
  1234. inline BigInt denominator()
  1235. {
  1236. return Denominator;
  1237. }
  1238. inline void numerator(BigInt number)
  1239. {
  1240. Numerator = number;
  1241. (*this).shrink_to_fit();
  1242. }
  1243. inline void denominator(BigInt number)
  1244. {
  1245. Denominator = number;
  1246. (*this).shrink_to_fit();
  1247. }
  1248. template<typename T>
  1249. inline void numerator(T number)
  1250. {
  1251. Numerator = number;
  1252. (*this).shrink_to_fit();
  1253. }
  1254. template<typename T>
  1255. inline void denominator(T number)
  1256. {
  1257. Denominator = number;
  1258. (*this).shrink_to_fit();
  1259. }
  1260. inline void shrink_to_fit()
  1261. {
  1262. Numerator.is_negative ^= Denominator.is_negative;
  1263. Denominator.is_negative = false;
  1264. if (Denominator.Digits.size() >= 1000)
  1265. {
  1266. BigInt GCD = __gcd(Numerator, Denominator);
  1267. Numerator /= GCD;
  1268. Denominator /= GCD;
  1269. }
  1270. }
  1271. std::string str(int Digits = 0)
  1272. {
  1273. if (Denominator == 0)
  1274. {
  1275. return "inf";
  1276. }
  1277. std::string answer;
  1278. bool negative_temp = false;
  1279. BigInt numerator = Numerator, denominator = Denominator;
  1280. if (numerator.is_negative ^ denominator.is_negative)
  1281. {
  1282. negative_temp = true;
  1283. }
  1284. numerator.is_negative = false;
  1285. denominator.is_negative = false;
  1286. for (int i = 0; i < Digits; i++)
  1287. {
  1288. numerator.Digits.push_front(0);
  1289. }
  1290. numerator /= denominator;
  1291. for (int i = Digits; i < numerator.Digits.size(); i++)
  1292. {
  1293. answer.push_back(numerator.Digits[i] + '0');
  1294. }
  1295. if (Digits >= numerator.Digits.size())
  1296. {
  1297. answer.push_back('0');
  1298. }
  1299. if (negative_temp)
  1300. {
  1301. answer.push_back('-');
  1302. }
  1303. reverse(answer.begin(), answer.end());
  1304. if (Digits)
  1305. {
  1306. answer.push_back('.');
  1307. std::string temp;
  1308. for (int i = 0; i < Digits; i++)
  1309. {
  1310. temp.push_back(numerator.Digits[i] + '0');
  1311. }
  1312. reverse(temp.begin(), temp.end());
  1313. for (int i = temp.size(); i < Digits; i++)
  1314. {
  1315. answer.push_back('0');
  1316. }
  1317. answer += temp;
  1318. }
  1319. return answer;
  1320. }
  1321. inline friend std::istream& operator >> (std::istream& input_stream, BigFloat& object)
  1322. {
  1323. std::string number;
  1324. input_stream >> number;
  1325. object = number;
  1326. return input_stream;
  1327. }
  1328. friend std::ostream& operator << (std::ostream& output_stream, BigFloat object)
  1329. {
  1330. if (object.Denominator == 0)
  1331. {
  1332. if (object.Numerator == 0)
  1333. {
  1334. output_stream << "Undefined";
  1335. return output_stream;
  1336. }
  1337. else
  1338. {
  1339. output_stream << "inf";
  1340. return output_stream;
  1341. }
  1342. }
  1343. BigInt GCD = __gcd(abs(object.Numerator), abs(object.Denominator));
  1344. object.Numerator /= GCD;
  1345. object.Denominator /= GCD;
  1346. output_stream << std::fixed << object.Numerator << '/' << object.Denominator;
  1347. return output_stream;
  1348. }
  1349. BigFloat& operator = (std::string number)
  1350. {
  1351. if (number[0] == '-')
  1352. {
  1353. Numerator.is_negative = true;
  1354. }
  1355. int dot_place = 0;
  1356. Numerator.Digits.resize(number.size());
  1357. Numerator.Digits[number.size() - 1] = 0;
  1358. for (int i = number.size() - 1; i >= Numerator.is_negative; i--)
  1359. {
  1360. if (number[i] != '.')
  1361. {
  1362. Numerator.Digits[number.size() - i - (bool)dot_place - 1] = number[i] - '0';
  1363. }
  1364. else
  1365. {
  1366. dot_place = number.size() - i - 1;
  1367. }
  1368. }
  1369. if (dot_place)
  1370. {
  1371. Numerator.Digits.pop_back();
  1372. }
  1373. Denominator.Digits = std::deque<int>(dot_place + 1);
  1374. Denominator.Digits[dot_place] = 1;
  1375. (*this).shrink_to_fit();
  1376. return *this;
  1377. }
  1378. template<typename T>
  1379. inline BigFloat& operator = (T number)
  1380. {
  1381. std::stringstream string_stream;
  1382. string_stream << std::fixed << number;
  1383. *this = string_stream.str();
  1384. return *this;
  1385. }
  1386. inline friend bool operator > (BigFloat first_object, BigFloat second_object)
  1387. {
  1388. return first_object.Numerator * second_object.Denominator > second_object.Numerator * first_object.Denominator;
  1389. }
  1390. inline friend bool operator < (BigFloat first_object, BigFloat second_object)
  1391. {
  1392. return first_object.Numerator * second_object.Denominator < second_object.Numerator * first_object.Denominator;
  1393. }
  1394. inline friend bool operator == (BigFloat first_object, BigFloat second_object)
  1395. {
  1396. return first_object.Numerator * second_object.Denominator == second_object.Numerator * first_object.Denominator;
  1397. }
  1398. inline friend bool operator >= (BigFloat first_object, BigFloat second_object)
  1399. {
  1400. return first_object.Numerator * second_object.Denominator >= second_object.Numerator * first_object.Denominator;
  1401. }
  1402. inline friend bool operator <= (BigFloat first_object, BigFloat second_object)
  1403. {
  1404. return first_object.Numerator * second_object.Denominator <= second_object.Numerator * first_object.Denominator;
  1405. }
  1406. inline friend bool operator != (BigFloat first_object, BigFloat second_object)
  1407. {
  1408. return first_object.Numerator * second_object.Denominator != second_object.Numerator * first_object.Denominator;
  1409. }
  1410. template<typename T>
  1411. inline friend bool operator > (BigFloat first_object, T number)
  1412. {
  1413. BigFloat second_object;
  1414. second_object = number;
  1415. return first_object > second_object;
  1416. }
  1417. template<typename T>
  1418. inline friend bool operator < (BigFloat first_object, T number)
  1419. {
  1420. BigFloat second_object;
  1421. second_object = number;
  1422. return first_object < second_object;
  1423. }
  1424. template<typename T>
  1425. inline friend bool operator == (BigFloat first_object, T number)
  1426. {
  1427. BigFloat second_object;
  1428. second_object = number;
  1429. return first_object == second_object;
  1430. }
  1431. template<typename T>
  1432. inline friend bool operator >= (BigFloat first_object, T number)
  1433. {
  1434. BigFloat second_object;
  1435. second_object = number;
  1436. return first_object >= second_object;
  1437. }
  1438. template<typename T>
  1439. inline friend bool operator <= (BigFloat first_object, T number)
  1440. {
  1441. BigFloat second_object;
  1442. second_object = number;
  1443. return first_object <= second_object;
  1444. }
  1445. template<typename T>
  1446. inline friend bool operator != (BigFloat first_object, T number)
  1447. {
  1448. BigFloat second_object;
  1449. second_object = number;
  1450. return first_object != second_object;
  1451. }
  1452. template<typename T>
  1453. inline friend bool operator > (T number, BigFloat second_object)
  1454. {
  1455. BigFloat first_object;
  1456. first_object = number;
  1457. return first_object > second_object;
  1458. }
  1459. template<typename T>
  1460. inline friend bool operator < (T number, BigFloat second_object)
  1461. {
  1462. BigFloat first_object;
  1463. first_object = number;
  1464. return first_object < second_object;
  1465. }
  1466. template<typename T>
  1467. inline friend bool operator == (T number, BigFloat second_object)
  1468. {
  1469. BigFloat first_object;
  1470. first_object = number;
  1471. return first_object == second_object;
  1472. }
  1473. template<typename T>
  1474. inline friend bool operator >= (T number, BigFloat second_object)
  1475. {
  1476. BigFloat first_object;
  1477. first_object = number;
  1478. return first_object >= second_object;
  1479. }
  1480. template<typename T>
  1481. inline friend bool operator <= (T number, BigFloat second_object)
  1482. {
  1483. BigFloat first_object;
  1484. first_object = number;
  1485. return first_object <= second_object;
  1486. }
  1487. template<typename T>
  1488. inline friend bool operator != (T number, BigFloat second_object)
  1489. {
  1490. BigFloat first_object;
  1491. first_object = number;
  1492. return first_object != second_object;
  1493. }
  1494. inline friend bool operator > (BigFloat first_object, BigInt number)
  1495. {
  1496. BigFloat second_object;
  1497. second_object = number;
  1498. return first_object > second_object;
  1499. }
  1500. inline friend bool operator < (BigFloat first_object, BigInt number)
  1501. {
  1502. BigFloat second_object;
  1503. second_object = number;
  1504. return first_object < second_object;
  1505. }
  1506. inline friend bool operator == (BigFloat first_object, BigInt number)
  1507. {
  1508. BigFloat second_object;
  1509. second_object = number;
  1510. return first_object == second_object;
  1511. }
  1512. inline friend bool operator >= (BigFloat first_object, BigInt number)
  1513. {
  1514. BigFloat second_object;
  1515. second_object = number;
  1516. return first_object >= second_object;
  1517. }
  1518. inline friend bool operator <= (BigFloat first_object, BigInt number)
  1519. {
  1520. BigFloat second_object;
  1521. second_object = number;
  1522. return first_object <= second_object;
  1523. }
  1524. inline friend bool operator != (BigFloat first_object, BigInt number)
  1525. {
  1526. BigFloat second_object;
  1527. second_object = number;
  1528. return first_object != second_object;
  1529. }
  1530. inline friend bool operator > (BigInt number, BigFloat second_object)
  1531. {
  1532. BigFloat first_object;
  1533. first_object = number;
  1534. return first_object > second_object;
  1535. }
  1536. inline friend bool operator < (BigInt number, BigFloat second_object)
  1537. {
  1538. BigFloat first_object;
  1539. first_object = number;
  1540. return first_object < second_object;
  1541. }
  1542. inline friend bool operator == (BigInt number, BigFloat second_object)
  1543. {
  1544. BigFloat first_object;
  1545. first_object = number;
  1546. return first_object == second_object;
  1547. }
  1548. inline friend bool operator >= (BigInt number, BigFloat second_object)
  1549. {
  1550. BigFloat first_object;
  1551. first_object = number;
  1552. return first_object >= second_object;
  1553. }
  1554. inline friend bool operator <= (BigInt number, BigFloat second_object)
  1555. {
  1556. BigFloat first_object;
  1557. first_object = number;
  1558. return first_object <= second_object;
  1559. }
  1560. inline friend bool operator != (BigInt number, BigFloat second_object)
  1561. {
  1562. BigFloat first_object;
  1563. first_object = number;
  1564. return first_object != second_object;
  1565. }
  1566. inline friend BigFloat operator - (BigFloat object)
  1567. {
  1568. return 0 - object;
  1569. }
  1570. inline friend BigFloat operator + (BigFloat first_object, BigFloat second_object)
  1571. {
  1572. first_object.Numerator = first_object.Numerator * second_object.Denominator + second_object.Numerator * first_object.Denominator;
  1573. first_object.Denominator *= second_object.Denominator;
  1574. first_object.shrink_to_fit();
  1575. return first_object;
  1576. }
  1577. inline friend BigFloat operator - (BigFloat first_object, BigFloat second_object)
  1578. {
  1579. first_object.Numerator = first_object.Numerator * second_object.Denominator - second_object.Numerator * first_object.Denominator;
  1580. first_object.Denominator *= second_object.Denominator;
  1581. first_object.shrink_to_fit();
  1582. return first_object;
  1583. }
  1584. inline friend BigFloat operator * (BigFloat first_object, BigFloat second_object)
  1585. {
  1586. first_object.Numerator *= second_object.Numerator;
  1587. first_object.Denominator *= second_object.Denominator;
  1588. first_object.shrink_to_fit();
  1589. return first_object;
  1590. }
  1591. inline friend BigFloat operator / (BigFloat first_object, BigFloat second_object)
  1592. {
  1593. first_object.Numerator *= second_object.Denominator;
  1594. first_object.Denominator *= second_object.Numerator;
  1595. first_object.shrink_to_fit();
  1596. return first_object;
  1597. }
  1598. inline friend BigFloat operator % (BigFloat object, BigInt MOD)
  1599. {
  1600. object.Numerator *= pow(object.Denominator, phi(MOD) - 1);
  1601. object.Numerator %= MOD;
  1602. object.Denominator = 1;
  1603. return object;
  1604. }
  1605. template<typename T>
  1606. inline friend BigFloat operator + (BigFloat first_object, T number)
  1607. {
  1608. BigFloat second_object;
  1609. second_object = number;
  1610. return first_object + second_object;
  1611. }
  1612. template<typename T>
  1613. inline friend BigFloat operator - (BigFloat first_object, T number)
  1614. {
  1615. BigFloat second_object;
  1616. second_object = number;
  1617. return first_object - second_object;
  1618. }
  1619. template<typename T>
  1620. inline friend BigFloat operator * (BigFloat first_object, T number)
  1621. {
  1622. BigFloat second_object;
  1623. second_object = number;
  1624. return first_object * second_object;
  1625. }
  1626. template<typename T>
  1627. inline friend BigFloat operator / (BigFloat first_object, T number)
  1628. {
  1629. BigFloat second_object;
  1630. second_object = number;
  1631. return first_object / second_object;
  1632. }
  1633. inline friend BigFloat operator % (BigFloat object, int MOD)
  1634. {
  1635. object.Numerator *= pow(object.Denominator, phi(MOD) - 1);
  1636. object.Numerator %= MOD;
  1637. object.Denominator = 1;
  1638. return object;
  1639. }
  1640. template<typename T>
  1641. inline friend BigFloat operator + (T number, BigFloat second_object)
  1642. {
  1643. BigFloat first_object;
  1644. first_object = number;
  1645. return first_object + second_object;
  1646. }
  1647. template<typename T>
  1648. inline friend BigFloat operator - (T number, BigFloat second_object)
  1649. {
  1650. BigFloat first_object;
  1651. first_object = number;
  1652. return first_object - second_object;
  1653. }
  1654. template<typename T>
  1655. inline friend BigFloat operator * (T number, BigFloat second_object)
  1656. {
  1657. BigFloat first_object;
  1658. first_object = number;
  1659. return first_object * second_object;
  1660. }
  1661. template<typename T>
  1662. inline friend BigFloat operator / (T number, BigFloat second_object)
  1663. {
  1664. BigFloat first_object;
  1665. first_object = number;
  1666. return first_object / second_object;
  1667. }
  1668. inline friend BigFloat operator + (BigFloat first_object, BigInt number)
  1669. {
  1670. BigFloat second_object;
  1671. second_object = number;
  1672. return first_object + second_object;
  1673. }
  1674. inline friend BigFloat operator - (BigFloat first_object, BigInt number)
  1675. {
  1676. BigFloat second_object;
  1677. second_object = number;
  1678. return first_object - second_object;
  1679. }
  1680. inline friend BigFloat operator * (BigFloat first_object, BigInt number)
  1681. {
  1682. BigFloat second_object;
  1683. second_object = number;
  1684. return first_object * second_object;
  1685. }
  1686. inline friend BigFloat operator / (BigFloat first_object, BigInt number)
  1687. {
  1688. BigFloat second_object;
  1689. second_object = number;
  1690. return first_object / second_object;
  1691. }
  1692. inline friend BigFloat operator + (BigInt number, BigFloat second_object)
  1693. {
  1694. BigFloat first_object;
  1695. first_object = number;
  1696. return first_object + second_object;
  1697. }
  1698. inline friend BigFloat operator - (BigInt number, BigFloat second_object)
  1699. {
  1700. BigFloat first_object;
  1701. first_object = number;
  1702. return first_object - second_object;
  1703. }
  1704. inline friend BigFloat operator * (BigInt number, BigFloat second_object)
  1705. {
  1706. BigFloat first_object;
  1707. first_object = number;
  1708. return first_object * second_object;
  1709. }
  1710. inline friend BigFloat operator / (BigInt number, BigFloat second_object)
  1711. {
  1712. BigFloat first_object;
  1713. first_object = number;
  1714. return first_object / second_object;
  1715. }
  1716. template<typename T>
  1717. inline BigFloat& operator += (T number)
  1718. {
  1719. *this = *this + number;
  1720. return *this;
  1721. }
  1722. template<typename T>
  1723. inline BigFloat& operator -= (T number)
  1724. {
  1725. *this = *this - number;
  1726. return *this;
  1727. }
  1728. template<typename T>
  1729. inline BigFloat& operator *= (T number)
  1730. {
  1731. *this = *this * number;
  1732. return *this;
  1733. }
  1734. template<typename T>
  1735. inline BigFloat& operator /= (T number)
  1736. {
  1737. *this = *this / number;
  1738. return *this;
  1739. }
  1740. template<typename T>
  1741. inline BigFloat& operator %= (T number)
  1742. {
  1743. *this = *this % number;
  1744. return *this;
  1745. }
  1746. inline BigFloat& operator += (BigInt number)
  1747. {
  1748. *this = *this + number;
  1749. return *this;
  1750. }
  1751. inline BigFloat& operator -= (BigInt number)
  1752. {
  1753. *this = *this - number;
  1754. return *this;
  1755. }
  1756. inline BigFloat& operator *= (BigInt number)
  1757. {
  1758. *this = *this * number;
  1759. return *this;
  1760. }
  1761. inline BigFloat& operator /= (BigInt number)
  1762. {
  1763. *this = *this / number;
  1764. return *this;
  1765. }
  1766. inline BigFloat& operator %= (BigInt number)
  1767. {
  1768. *this = *this % number;
  1769. return *this;
  1770. }
  1771. inline BigFloat& operator ++ ()
  1772. {
  1773. Numerator += Denominator;
  1774. return (*this);
  1775. }
  1776. inline BigFloat operator ++ (int32_t)
  1777. {
  1778. Numerator += Denominator;
  1779. return (*this) - 1;
  1780. }
  1781. inline BigFloat& operator -- ()
  1782. {
  1783. Numerator -= Denominator;
  1784. return (*this);
  1785. }
  1786. inline BigFloat operator -- (int32_t)
  1787. {
  1788. Numerator -= Denominator;
  1789. return (*this) + 1;
  1790. }
  1791. };
  1792. inline BigFloat abs(BigFloat number)
  1793. {
  1794. BigInt Numerator = number.numerator();
  1795. Numerator.is_negative = false;
  1796. number.numerator(Numerator);
  1797. BigInt Denominator = number.denominator();
  1798. Denominator.is_negative = false;
  1799. number.denominator(Denominator);
  1800. return number;
  1801. }
  1802. BigFloat floor(BigFloat number, int Digits = 0)
  1803. {
  1804. if (number.denominator() == 0)
  1805. {
  1806. return number;
  1807. }
  1808. number = number.str(Digits);
  1809. return number;
  1810. }
  1811. BigFloat ceil(BigFloat number, int Digits = 0)
  1812. {
  1813. if (number.denominator() == 0)
  1814. {
  1815. return number;
  1816. }
  1817. BigFloat number2;
  1818. number2.numerator(1);
  1819. std::string temp = "1";
  1820. while (Digits--)
  1821. {
  1822. temp.push_back('0');
  1823. }
  1824. number2.denominator(temp);
  1825. number += number2;
  1826. number2.denominator(number.denominator());
  1827. number -= number2;
  1828. return number;
  1829. }
  1830. BigFloat round(BigFloat number, int Digits = 0)
  1831. {
  1832. if (number.denominator() == 0)
  1833. {
  1834. return number;
  1835. }
  1836. std::string temp = number.str(Digits + 1);
  1837. if (temp[temp.size() - 1] >= '5')
  1838. {
  1839. temp = '0' + temp;
  1840. for (int i = temp.size() - 2; i >= 0; i--)
  1841. {
  1842. if (temp[i] == '.')
  1843. {
  1844. continue;
  1845. }
  1846. if (temp[i] != '9')
  1847. {
  1848. temp[i]++;
  1849. break;
  1850. }
  1851. else
  1852. {
  1853. temp[i] = '0';
  1854. }
  1855. }
  1856. }
  1857. temp.pop_back();
  1858. number = temp;
  1859. return number;
  1860. }
  1861. BigFloat sqrt(BigInt number, int depth = 7)
  1862. {
  1863. BigFloat temp = 0;
  1864. BigFloat ans = 1;
  1865. while (ans != temp)
  1866. {
  1867. temp = ans;
  1868. ans = (5 * temp) / 4 + number / (4 * temp) - (temp * temp * temp) / (temp * temp + number);
  1869. ans = floor(ans, depth);
  1870. }
  1871. return ans;
  1872. }
  1873. BigFloat sqrt(BigFloat number, int depth = 7)
  1874. {
  1875. BigFloat temp = 0;
  1876. BigFloat ans = 1;
  1877. while (ans != temp)
  1878. {
  1879. temp = ans;
  1880. ans = (5 * temp) / 4 + number / (4 * temp) - (temp * temp * temp) / (temp * temp + number);
  1881. ans = floor(ans, depth);
  1882. }
  1883. return ans;
  1884. }
  1885. BigFloat cbrt(BigInt number, int depth = 7)
  1886. {
  1887. BigFloat temp = 0;
  1888. BigFloat ans = 1;
  1889. while (ans != temp)
  1890. {
  1891. temp = ans;
  1892. ans = temp - (temp * temp * temp - number) / (3 * temp * temp);
  1893. ans = floor(ans, depth);
  1894. }
  1895. return ans;
  1896. }
  1897. BigFloat cbrt(BigFloat number, int depth = 7)
  1898. {
  1899. BigFloat temp = 0;
  1900. BigFloat ans = 1;
  1901. while (ans != temp)
  1902. {
  1903. temp = ans;
  1904. ans = temp - (temp * temp * temp - number) / (3 * temp * temp);
  1905. ans = floor(ans, depth);
  1906. }
  1907. return ans;
  1908. }
  1909. int main()
  1910. {
  1911. return 0;
  1912. }
  1913.  
Success #stdin #stdout 0.01s 5272KB
stdin
Standard input is empty
stdout
Standard output is empty