Problem Link : http://lightoj.com/volume_showproblem.php?problem=1006
The concept is just like Fibonacci series except that it has six terms. Convert the recursion method into iterative method and it will work. Now the problem lies with the overflow.
The first 6 numbers are given . You have to find nth term of this series . Simple a loop is enough for this .
Use Modular arithmatic for intermdeiate calculation . The modular arithmetic says
i) (A + B ) % M = ( (A% M) + (B % M) ) % M.
ii) (A x B ) % M = ( (A% M) x (B % M) ) % M.
Use long long int for data type . Hope it helps
Code :
#include<iostream>
#include<cstdio>
using namespace std;
int main() {
long long int a,i,fn[10001],n,j, cases;
cin>>cases;
for(j=1;j<=cases;j++){
cin>>a>>b>>c>>d>>e>>f>>n;
fn[0]= a ;
fn[1]= b ;
fn[2]= c ;
fn[3]= d ;
fn[4]= e ;
fn[5]= f ;
for(i=6;i<=n;i++)
fn[i]=( fn[i-1] + fn[i-2] + fn[i-3] + fn[i-4] + fn[i-5] + fn[i-6] ) % 10000007 ;
printf("Case %lld: ", j);
cout<<fn[n]% 10000007<<endl;
}
return 0;
}
The concept is just like Fibonacci series except that it has six terms. Convert the recursion method into iterative method and it will work. Now the problem lies with the overflow.
The first 6 numbers are given . You have to find nth term of this series . Simple a loop is enough for this .
Use Modular arithmatic for intermdeiate calculation . The modular arithmetic says
i) (A + B ) % M = ( (A% M) + (B % M) ) % M.
ii) (A x B ) % M = ( (A% M) x (B % M) ) % M.
Use long long int for data type . Hope it helps
Code :
#include<iostream>
#include<cstdio>
using namespace std;
int main() {
long long int a,i,fn[10001],n,j, cases;
cin>>cases;
for(j=1;j<=cases;j++){
cin>>a>>b>>c>>d>>e>>f>>n;
fn[0]= a ;
fn[1]= b ;
fn[2]= c ;
fn[3]= d ;
fn[4]= e ;
fn[5]= f ;
for(i=6;i<=n;i++)
fn[i]=( fn[i-1] + fn[i-2] + fn[i-3] + fn[i-4] + fn[i-5] + fn[i-6] ) % 10000007 ;
printf("Case %lld: ", j);
cout<<fn[n]% 10000007<<endl;
}
return 0;
}
কোন মন্তব্য নেই:
একটি মন্তব্য পোস্ট করুন