Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int n, k;
- int fib[] = {1, 2, 3, 5, 8, 13, 21, 34, 55, 89};
- vector<int> ans;
- void Print() {
- cout << fib[ans[0]];
- for (int i = 1; i < ans.size(); ++i)
- cout << '+' << fib[ans[i]];
- cout << '\n';
- }
- void Backtrack(int s, int m, int t) {
- if(s > n) return;
- if(s == n) Print();
- if(t > 0) {
- ans.push_back(m);
- Backtrack(s + fib[m], m, t - 1);
- ans.pop_back();
- }
- for(int i = m + 1; i < 10; ++i) {
- ans.push_back(i);
- Backtrack(s + fib[i], i, k - 1);
- ans.pop_back();
- }
- }
- int main()
- {
- //freopen("in.txt", "r", stdin);
- freopen("FIBSUM.inp", "r", stdin);
- freopen("FIBSUM.out", "w", stdout);
- ios_base::sync_with_stdio(false);
- cin.tie(NULL); cout.tie(NULL);
- cin >> n >> k;
- Backtrack(0, 0, k);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment