Advertisement
nq1s788

игра камни через рекурсию

Apr 26th, 2025 (edited)
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.79 KB | None | 0 0
  1. ///https://informatics.msk.ru/mod/statements/view.php?id=298&chapterid=366#1
  2.  
  3. #include <iostream>
  4. #include <set>
  5. #include <vector>
  6. #include <deque>
  7.  
  8. #define se second
  9. #define fi first
  10. #define mp make_pair
  11. #define pb push_back
  12.  
  13. using namespace std;
  14.  
  15. vector<int> F;
  16.  
  17. int f(int n) {
  18.     if (n <= 0) return 2;
  19.     if (F[n] != 0) return F[n];
  20.     vector<int> next;
  21.     if (n % 3 == 0) {
  22.         next = {n - 1, n - 2};
  23.     } else if (n % 3 == 1) {
  24.         next = {n - 1, n - 3};
  25.     } else {
  26.         next = {n - 1, n - 2, n - 3};
  27.     }
  28.     for (auto e : next) {
  29.         if (f(e) == 2) {
  30.             F[n] = 1;
  31.             return 1;
  32.         }
  33.     }
  34.     F[n] = 2;
  35.     return 2;
  36. }
  37.  
  38. int main() {
  39.     int n;
  40.     cin >> n;
  41.     F.assign(n + 1, 0);
  42.     cout << f(n);
  43.     return 0;
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement