Pagini recente » Cod sursa (job #1014364) | Istoria paginii runda/13431/clasament | Cod sursa (job #2856519)
#include <fstream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <cstring>
#include <climits>
#include <iomanip>
#include <stack>
#include <cstdio>
#define MOD 666013
#define NMAX 4003
using namespace std;
int n;
int matriceInd[3][3];//matricea cu care fac inmultirile
int matriceInmult[3][3];//matricea const la puterea n-1
ifstream fin("kfib.in");
ofstream fout("kfib.out");
void inmultireMatrici(int a[][3], int b[][3], int rez[][3])
{
//initializarea
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 2; j++)
{
rez[i][j] = 0;
}
}
for (int lin = 1; lin <= 2; lin++)
{
//linia din a
//inmultesc cu coloanele din b
for (int col = 1; col <= 2; col++)
{
for (int k = 1; k <= 2; k++)
{
long long int prod = 1LL * a[lin][k] * b[k][col];
rez[lin][col] = (rez[lin][col]+prod) % MOD;
}
}
}
}
void putere(int k)
{
matriceInd[1][1] = 0;
matriceInd[1][2] = 1;
matriceInd[2][1] = 1;
matriceInd[2][2] = 1;
matriceInmult[1][1] = 1;
matriceInmult[2][2] = 1;
int aux[3][3];
while (k > 0)
{
if (k % 2 == 1)
{
inmultireMatrici(matriceInmult, matriceInd, aux);
memcpy(matriceInmult, aux, sizeof(aux));
}
inmultireMatrici(matriceInd, matriceInd, aux);
memcpy(matriceInd, aux, sizeof(aux));
k = k / 2;
}
}
int main()
{
fin >> n;
putere(n-1);
long long int rez = matriceInmult[2][2];
fout << rez%MOD;
return 0;
}