-
Notifications
You must be signed in to change notification settings - Fork 0
/
SCTDL022.cpp
85 lines (72 loc) · 1.5 KB
/
SCTDL022.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/**
* @file SCTDL022.cpp
* @author long ([email protected])
* @brief C++ program counting of ways in which N can be
* represented as sum of Fibonacci numbers without repetition
* @version 0.1
* @date 2023-02-27
*
* @copyright Copyright (c) 2023
*
*/
#include <bits/stdc++.h>
using namespace std;
long long fib[101], dp1[101];
long long dp2[101], v[101];
// Function to generate the
// fibonacci number
void fibonacci()
{
// First two number of
// fibonacci sequence
fib[1] = 1;
fib[2] = 2;
for (int i = 3; i <= 87; i++) {
fib[i] = fib[i - 1] + fib[i - 2];
}
}
// Function to find maximum ways to
// represent num as the sum of
// fibonacci number
int find(int num)
{
int cnt = 0;
// Generate the Canonical form
// of given number
for (int i = 87; i > 0; i--) {
if (num >= fib[i]) {
v[cnt++] = i;
num -= fib[i];
}
}
// Reverse the number
reverse(v, v + cnt);
// Base condition of dp1 and dp2
dp1[0] = 1;
dp2[0] = (v[0] - 1) / 2;
// Iterate from 1 to cnt
for (int i = 1; i < cnt; i++) {
// Calculate dp1[]
dp1[i] = dp1[i - 1] + dp2[i - 1];
// Calculate dp2[]
dp2[i] = ((v[i] - v[i - 1]) / 2) * dp2[i - 1] + ((v[i] - v[i - 1] - 1) / 2) * dp1[i - 1];
}
// Return final ans
return (dp1[cnt - 1] + dp2[cnt - 1]);
}
int main()
{
// Function call to generate the
// fibonacci numbers
fibonacci();
int t;
cin >> t;
while(t--) {
int num;
// Given Number
cin >> num;
// Function Call
cout << find(num) << endl;
}
return 0;
}