Cod sursa(job #1429375)

Utilizator andreiulianAndrei andreiulian Data 6 mai 2015 11:17:37
Problema Al k-lea termen Fibonacci Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.8 kb
#include<iostream>
#include<fstream>
const int mod=666013;
using namespace std;
int n;
struct matrice
{
    int a,b,c,d;
} z;
void produs(matrice &x, matrice y);
void putere(matrice &x, int p);
int main()
{
    ifstream in("kfib.in");
    ofstream out("kfib.out");
    in>>n;
    z.a=0,z.b=z.c=z.d=1;
    putere(z,3);
    cout<<z.a<<' '<<z.b<<'\n'<<z.c<<' '<<z.d;

}
void putere(matrice &x, int p)
{
    matrice N;
    N.a=N.d=1;
    N.b=N.c=0;
    while(p>1)
    {
        if(p&1) produs(N,x);
        produs(x,x);
        p/=2;
    }
    produs(x,N);
}
void produs(matrice &x, matrice y)
{
    int aa,bb,cc,dd;
    aa=x.a*y.a+x.b*y.b;
    bb=x.a*y.b+x.b*y.d;
    cc=x.c*y.a+x.d*y.c;
    dd=x.c*y.b+x.d*y.d;
    x.a=aa%mod;
    x.b=bb%mod;
    x.c=cc%mod;
    x.d=dd%mod;
}