Advertisement
smatskevich

Seminar4

Mar 3rd, 2022
668
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.38 KB | None | 0 0
  1. // ============================================================================================
  2. // biginteger.h
  3.  
  4. #pragma once
  5.  
  6. #include <iostream>
  7. #include <string>
  8. #include <vector>
  9.  
  10. class BigIntegerOld {
  11.  public:
  12.   void SetValue(long long value);
  13.   long long Value() const { return value_; }
  14.  
  15.  private:
  16.   long long value_ = 0;
  17.  
  18. };
  19.  
  20. BigIntegerOld CreateVeryBigInteger();
  21.  
  22. typedef unsigned int uint;
  23.  
  24. class BigInteger {
  25.  public:
  26.   std::string ToString() const;
  27.   BigInteger operator+(const BigInteger& second) const;
  28.   BigInteger operator-(const BigInteger& second) const;
  29.   BigInteger operator*(const BigInteger& second) const;
  30.   BigInteger operator/(const BigInteger& second) const;
  31.   BigInteger operator%(const BigInteger& second) const;
  32.  
  33.   BigInteger& operator+=(const BigInteger& second);
  34.   BigInteger& operator-=(const BigInteger& second);
  35.   BigInteger& operator*=(const BigInteger& second);
  36.   BigInteger& operator/=(const BigInteger& second);
  37.   BigInteger& operator%=(const BigInteger& second);
  38.  
  39.   bool operator==(const BigInteger& second) const;
  40.   bool operator!=(const BigInteger& second) const { return !(*this == second);}
  41.  
  42.  private:
  43.   std::vector<uint> digits;  // Вначале младшие разряды
  44.   bool is_positive;  // true, если положительно или 0
  45.  
  46.   bool IsNull() const { return digits.empty(); }
  47.  
  48.   friend std::istream& operator>>(std::istream& stream, BigInteger& bigint);
  49. };
  50.  
  51. std::istream& operator>>(std::istream& stream, BigInteger& bigint);
  52.  
  53. std::ostream& operator<<(std::ostream& stream, const BigInteger& bigint);
  54.  
  55.  
  56. // ============================================================================================
  57. // biginteger.cpp
  58.  
  59. #include "biginteger.h"
  60.  
  61. #include <limits>
  62.  
  63. void BigIntegerOld::SetValue(long long value) {
  64.   value_ = value;
  65. }
  66.  
  67. BigIntegerOld CreateVeryBigInteger() {
  68.   BigIntegerOld bigint;
  69.   bigint.SetValue(std::numeric_limits<long long>::max());
  70.   return bigint;
  71. }
  72.  
  73. std::string BigInteger::ToString() const {
  74.   if (IsNull()) return "0";
  75.   std::string result;
  76.   if (!is_positive) result.push_back('-');
  77.   for (int i = digits.size() - 1; i >= 0; --i) {
  78.     result.push_back(char(digits[i]) + '0');
  79.   }
  80.   return result;
  81. }
  82.  
  83. std::istream& operator>>(std::istream& stream, BigInteger& bigint) {
  84.   std::string s;
  85.   stream >> s;
  86.   bigint.is_positive = s[0] != '-';
  87.   for (int i = s.size() - 1; i >= (bigint.is_positive ? 0 : 1); --i) {
  88.     bigint.digits.push_back(s[i] - '0');
  89.   }
  90.   if (bigint.digits.back() == 0) bigint.digits.pop_back();
  91.   return stream;
  92. }
  93.  
  94. std::ostream& operator<<(std::ostream& stream, const BigInteger& bigint) {
  95.   return stream << bigint.ToString();
  96. }
  97.  
  98.  
  99. // ============================================================================================
  100. // main.cpp
  101.  
  102. #include <algorithm>
  103. #include <iostream>
  104. #include <vector>
  105.  
  106. #include "biginteger.h"
  107. #include "help.h"
  108.  
  109. //        0
  110. //   1      3   4
  111. //   2      5
  112. // 6 7 8
  113.  
  114. // 9
  115. // 0 1   0 3   0 4   1 2   3 5   2 6   2 7   2 8
  116.  
  117. struct TreeNode {
  118.   int Count = 1;  // Число вершин в поддереве
  119.   int A = 0;  // Сумма длин путей до всех дочерних вершин
  120.   int Result = 0;
  121.   std::vector<int> Children;
  122. };
  123.  
  124. void DFS1(std::vector<TreeNode>& nodes, int /*Текущая*/ v) {
  125.   for (int k : nodes[v].Children) {
  126.     DFS1(nodes, k);
  127.     nodes[v].Count += nodes[k].Count;
  128.     nodes[v].A += nodes[k].A;
  129.   }
  130.   nodes[v].A += nodes[v].Count - 1;
  131. }
  132.  
  133. void UpdateChild(std::vector<TreeNode>& nodes, int vert, int child) {
  134.   nodes[child].Result = nodes[vert].Result - nodes[child].Count + nodes.size() - nodes[child].Count;
  135. }
  136.  
  137. int main1() {
  138.   int n = 0;
  139.   std::cin >> n;
  140.   // Вектор из n узлов.
  141.   std::vector<TreeNode> nodes(n);
  142.   for (int i = 1; i < n; ++i) {
  143.     int a = 0, aa = 0;
  144.     std::cin >> a >> aa;
  145.     if (a > aa) std::swap(a, aa);
  146.     nodes[a].Children.push_back(aa);
  147.   }
  148.  
  149.   DFS1(nodes, 0);
  150.   UpdateChild(nodes, 0, 1);
  151.   // std::cout << DFS2(nodes) << std::endl;
  152.   return 0;
  153. }
  154.  
  155. int main2() {
  156.   BigIntegerOld bigint;
  157.   bigint.SetValue(300);
  158.   std::cout << bigint.Value() << std::endl;
  159.   std::cout << CreateVeryBigInteger().Value() << std::endl;
  160.   return 0;
  161. }
  162.  
  163. int main() {
  164.   BigInteger bigint;
  165.   std::cin >> bigint;
  166.   std::cout << bigint.ToString() << std::endl;
  167.   std::cout << bigint << std::endl;
  168.  
  169.   return 0;
  170. }
  171.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement