ENG  RUS Timus Online Judge
Online Judge
Problems
Authors
Online contests
Site news
Webboard
Problem set
Submit solution
Judge status
Guide
Register
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

## Discussion of Problem 1133. Fibonacci Sequence

WA 5. WTF?
Posted by nexerd 17 Mar 2015 21:41
#include <iostream>
#include <conio.h>
using namespace std;

void find(int j, int i, int &k1,int &k2)
{
if (j==i+1) return;
else
{
int buf = k1;
k1 = k1 + k2;
k2 = buf;
find(j-1,i,k1,k2);
}
}

int main()
{
long long Fi, Fj, f1,f2, f3;
int i,j,n;
cin>>i>>Fi>>j>>Fj>>n;
if (j==n)  {cout<<Fj<<endl; return 0;}
if (i==n)  {cout<<Fi<<endl; return 0;}
if (i<j)
{
int buf = i;
i = j;
j = buf;
buf = Fi;
Fi = Fj;
Fj = buf;
}
int k1 = 1, k2 = 1;
f1 = Fj;
if (i!=j+1)
{
find(i-1,j,k1,k2);
f2 = (Fi - k2*Fj)/k1;
}
else
f2 = Fi;

int lim;
if (j<0) lim = n - j -1;
else
if (j == 0 ) lim = n;
else lim = n - j +1;
for (int count = j +1; count < lim; count++)
{
f3 = f1 + f2;
f1 = f2;
f2 = f3;
}
cout<<f3<<endl;
return 0;
}