I have given Fibonacci digits only
, i have to find out the numbers of ways to generate a number using K
Fibonacci numbers only.
Constraints:
1<=K<=10
1<=N<=10^9
For Example:
N=14 and K=3
There are two ways:
(8,5,1) and (8,3,3)
Here is my recursive solution:
public static void num_gen(int i ,long val ,int used){
if(used==0){
if(val==n) ans++;
return ;
}
if(i==Fib.length) return ;
for(int j=0;j<=used;j++){
long x = j*Fib[i];
if(x+val<=n){
num_gen(i+1,x+val, used-j);
}
}
}
This solution will timeout for large value of N and K=10. Can you provide me algorithm with better complexity.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…