Padovan Sequence

PROBLEM :
A Padovan Sequence is a sequence which is represented by the following recurrence
P(n) = P(n-2) + P(n-3)
P(0) = P(1) = P(2) = 1
Now given a number N your task is to find the Nth no in this sequence.

Note: Since the output could be very long take mod 1000000007

Input:
The first line of input contains an integer T denoting the no of test cases. Then T test cases follow. Each line contains an integer N.

Output:
For each test case in a new line print the nth no of the Padovan sequence.

Constraints:
1<=T<=100
1<=N<=100

Example:
Input:
2
12
4

Output:
21
2

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

#include<iostream>
using namespace std;
long long Padovan_Sequence(int ) ;
int main()
 {
int t,no ;
cin>>t ;
while(t--)
{
   cin>>no ;
   cout<<Padovan_Sequence(no)<<endl ;
}
return 0;
}

long long Padovan_Sequence(int no)
{
    int arr[no+1] ;
    int i ;
   
    if(no>=0&&no<=2)
        return 1 ;
       
    arr[0]=1,arr[1]=1,arr[2]=1 ;
   
    for(i=3;i<=no;i++)
        arr[i]=(arr[i-2]+arr[i-3])%1000000007 ;
       
    return arr[no] ;
}

---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )