Pagini recente » Cod sursa (job #582859) | Cod sursa (job #3218592) | Cod sursa (job #1408783) | Cod sursa (job #2745206) | Cod sursa (job #2166307)
#include <fstream>
using namespace std;
ifstream in("kfib.in");
ofstream out("kfib.out");
const int N = 3, R = 666013;
long long aux[N][N], rez[N][N], inm[N][N];
void matrixMultiplication(long long s[][N]){ /// pe cine inmultesc cu inm
aux[1][1] = aux[1][2] = aux[2][1] = aux[2][2] = 0;
for(int i=1;i<=2;i++)
for(int j=1;j<=2;j++)
for(int k=1;k<=2;k++)
aux[i][j] = (aux[i][j] + 1LL * s[i][k] * inm[k][j]) % R;
for(int i=1;i<=2;i++)
for(int j=1;j<=2;j++)
s[i][j] = aux[i][j];
}
void fastExpMatrix(int n){
rez[1][1] = rez[2][2] = 1;
inm[1][2] = inm[2][1] = inm[2][2] = 1;
for(;n;n>>=1){
if(n&1)
matrixMultiplication(rez);
matrixMultiplication(inm);
}
}
int main()
{
int n;
in>>n;
in.close();
fastExpMatrix(n);
out<<rez[2][1]<<"\n";
out.close();
return 0;
}