Can anybody help me to find my mistake in order to solve the problem easily, please. I got WA from this problem.
Link of the problem is https://www.spoj.com/problems/MAIN74/24 .
My code is:
#include <bits/stdc++.h>
#define PII pair<ll,ll>
typedef long long ll;
using namespace std;
const ll EPS=1e9+7;
PII fib(ll n)
{
if(n==0)
return {0, 1};
auto p=fib(n >> 1);
int c=((p.first%EPS)*((2*(p.second%EPS))%EPS-p.first%EPS)%EPS)%EPS;
int d=((p.first%EPS)*(p.first%EPS))%EPS+((p.second%EPS)*(p.second%EPS))%EPS;
if(n&1)
return {d, (c + d)%EPS};
else
return {c, d};
}
int main()
{
ll t,n;
cin>>t;
while(t–)
{
cin>>n;
PII ans=fib(n+1);
if(n==0)
cout<<0<<endl;
else if(n==1)
cout<<2<<endl;
else
cout<<(ans.first%EPS+ans.second%EPS)%EPS<<endl;
}
return 0;
}
Please help me…
created
last reply
- 9
replies
- 1.6k
views
- 5
users
- 1
like
- 2
links