Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //{ Driver Code Starts
- // Initial Template for C++
- #include <bits/stdc++.h>
- using namespace std;
- // } Driver Code Ends
- // User function Template for C++
- class Solution {
- const int mod = 1000000007;
- public:
- int fib(int n , vector<int>&dp){
- if(dp[n]!=-1)return dp[n];
- else return dp[n] = ((fib(n-2,dp)%mod + fib(n-1,dp)%mod)%mod);
- }
- int nthFibonacci(int n){
- // code here
- vector<int>dp(n+1,-1);
- dp[1]=1;
- dp[2]=1;
- return fib(n,dp);
- }
- };
- //{ Driver Code Starts.
- int main() {
- int t;
- cin >> t;
- while (t--) {
- int n;
- cin >> n;
- Solution ob;
- cout << ob.nthFibonacci(n) << endl;
- }
- return 0;
- }
- // } Driver Code Ends
Advertisement
Add Comment
Please, Sign In to add comment