DuongNhi99

FIBSUM

Nov 30th, 2020
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.81 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int n, k;
  5. int fib[] = {1, 2, 3, 5, 8, 13, 21, 34, 55, 89};
  6. vector<int> ans;
  7.  
  8. void Print() {
  9.     cout << fib[ans[0]];
  10.     for (int i = 1; i < ans.size(); ++i)
  11.         cout << '+' << fib[ans[i]];
  12.     cout << '\n';
  13. }
  14.  
  15. void Backtrack(int s, int m, int t) {
  16.     if(s > n) return;
  17.     if(s == n) Print();
  18.  
  19.     if(t > 0) {
  20.         ans.push_back(m);
  21.         Backtrack(s + fib[m], m, t - 1);
  22.         ans.pop_back();
  23.     }
  24.  
  25.     for(int i = m + 1; i < 10; ++i) {
  26.         ans.push_back(i);
  27.         Backtrack(s + fib[i], i, k - 1);
  28.         ans.pop_back();
  29.     }
  30. }
  31.  
  32. int main()
  33. {
  34.     //freopen("in.txt", "r", stdin);
  35.     freopen("FIBSUM.inp", "r", stdin);
  36.     freopen("FIBSUM.out", "w", stdout);
  37.     ios_base::sync_with_stdio(false);
  38.     cin.tie(NULL); cout.tie(NULL);
  39.  
  40.     cin >> n >> k;
  41.     Backtrack(0, 0, k);
  42.  
  43.     return 0;
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment