Advertisement
paranid5

Vsesib4

Oct 18th, 2020 (edited)
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.22 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4.  
  5. constexpr  uint64_t del = 1e9 + 9;
  6.  
  7. uint64_t rec(const std::vector<std::pair<int, int>>& act, const std::vector<std::string>& moves, const int ind, const uint64_t val)
  8. {
  9.     if (moves[ind] == "x")
  10.         return val;
  11.  
  12.     else if (moves[ind] == "+")
  13.         return (rec(act, moves, act[ind].first, val) + rec(act, moves, act[ind].second, val)) % del;
  14.  
  15.     else if (moves[ind] == "*")
  16.         return (rec(act, moves, act[ind].first, val) * rec(act, moves, act[ind].second, val)) % del;
  17.  
  18.     else
  19.         return std::stoull(moves[ind]);
  20. }
  21.  
  22. int main()
  23. {
  24.     std::ios_base::sync_with_stdio(false);
  25.     std::cin.tie(nullptr);
  26.     std::cout.tie(nullptr);
  27.  
  28.     int n = 0, m = 0, q = 0;
  29.     std::cin >> n >> m >> q;
  30.  
  31.     std::vector<std::string> moves(n);
  32.  
  33.     for (auto& mo : moves)
  34.         std::cin >> mo;
  35.  
  36.     std::vector<std::pair<int, int>> act(n, std::make_pair(-1, -1));
  37.  
  38.     while (m--)
  39.     {
  40.         int f = 0, s = 0;
  41.         std::cin >> f >> s, f--, s--;
  42.         act[s].first == -1 ? (act[s].first = f) : (act[s].second = f);
  43.     }
  44.  
  45.     if (q == 0)
  46.         std::cout << rec(act, moves, 0, 0);
  47.     else
  48.     {
  49.         while (q--)
  50.         {
  51.             uint64_t elem = 0;
  52.             std::cin >> elem;
  53.             std::cout << rec(act, moves, 0, elem) << '\n';
  54.         }
  55.     }
  56.  
  57.     return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement