Pagini recente » Cod sursa (job #1921294) | Cod sursa (job #1574532) | Cod sursa (job #1764427) | Cod sursa (job #1298952) | Cod sursa (job #1521806)
#include <fstream>
#include <cstdio>
#include <iostream>
using namespace std;
long long t,r,n,matrice[2][2]={{0,1},{1,1}},solutie[2][2]={{1,0},{0,1}},a,b,c,d;
void inmultire(long long matrice[][2],long long solutie[][2],long long k)
{
while(k)
{
if(k & 1)
{
a=matrice[0][0] * solutie[0][0] + matrice[0][1] * solutie[1][0];
b=matrice[0][0] * solutie[0][1] + matrice[0][1] * solutie[1][1];
c=matrice[1][0] * solutie[0][0] + matrice[1][1] * solutie[1][0];
d=matrice[1][0] * solutie[0][1] + matrice[1][1] * solutie[1][1];
solutie[0][0]=a % r;
solutie[0][1]=b % r;
solutie[1][0]=c % r;
solutie[1][1]=d % r;
}
a=matrice[0][0] * matrice[0][0] + matrice[0][1] * matrice[1][0];
b=matrice[0][0] * matrice[0][1] + matrice[0][1] * matrice[1][1];
c=matrice[1][0] * matrice[0][0] + matrice[1][1] * matrice[1][0];
d=matrice[1][0] * matrice[0][1] + matrice[1][1] * matrice[1][1];
matrice[0][0]=a % r;
matrice[0][1]=b % r;
matrice[1][0]=c % r;
matrice[1][1]=d % r;
k>>=1;
}
}
int main()
{
int i,n1=0,nr=0;
freopen("kfib.in","r",stdin);
freopen("kfib.out","w",stdout);
{
scanf("%li",&n);
if(n==2)
{
cout<<'2'<<"\n";
}else{
if(n>=n1 && nr!=0)
{
inmultire(matrice,solutie,n-n1+1);
cout<<solutie[1][1]<<"\n";
nr=1;
}else{
long long matrice[2][2]={{0,1},{1,1}},solutie[2][2]={{1,0},{0,1}};
inmultire(matrice,solutie,n);
cout<<solutie[1][1]<<"\n";
}
}
n1=n;
}
}