Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.19 KB | None | 0 0
  1. //Daniel Grzegorzewski
  2. #include <bits/stdc++.h>
  3.  
  4. #define MP make_pair
  5. #define PB push_back
  6. #define ST first
  7. #define ND second
  8.  
  9. using namespace std;
  10.  
  11. typedef pair<int, int> PII;
  12. typedef vector<int> VI;
  13. typedef vector<PII> VII;
  14.  
  15. void init_ios()
  16. {
  17.      ios_base::sync_with_stdio(0);
  18.      cin.tie(0);
  19. }
  20.  
  21. int n, res = 3000;
  22. bool mamy[3000];
  23. VI ruchy;
  24.  
  25. void bt(VI &s, int wyn, int id)
  26. {
  27.     if (res > wyn+1 && mamy[n-s.back()]) {
  28.         res = wyn+1;
  29.         ruchy = s;
  30.         return;
  31.     }
  32.     if (wyn+1 >= res)
  33.         return ;
  34.     while (s.back()+s[id] > n)
  35.         --id;
  36.     for (int i = id; i >= 0; --i) {
  37.         s.PB(s.back()+s[i]);
  38.         mamy[s.back()] = true;
  39.         bt(s, wyn+1, id+1);
  40.         mamy[s.back()] = false;
  41.         s.pop_back();
  42.         if (wyn+1 >= res)
  43.             return;
  44.     }
  45. }
  46.  
  47. int main()
  48. {
  49.     init_ios();
  50.     cin >> n;
  51.     if (n == 1) {
  52.         cout<<"0\n1\n";
  53.         return 0;
  54.     }
  55.     VI s;
  56.     s.PB(1);
  57.     mamy[1] = true;
  58.     if (n >= 700)
  59.         for (int j = 2; j <= 64; j *= 2) {
  60.             s.PB(j);
  61.             mamy[j] = true;
  62.         }
  63.     if (n >= 700)
  64.         bt(s, 6, 6);
  65.     else
  66.         bt(s, 0, 0);
  67.     ruchy.PB(n);
  68.     cout<<res<<"\n";
  69.     for (int i = 0; i < ruchy.size(); ++i)
  70.         cout<<ruchy[i]<<" ";
  71.     cout<<"\n";
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement