Cod sursa(job #1241144)

Utilizator Alex_dudeDudescu Alexandru Alex_dude Data 12 octombrie 2014 18:55:40
Problema Al k-lea termen Fibonacci Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.02 kb
#include <stdio.h>
#include <cstdlib>
using namespace std;
long  A[2][2]={{0,1},{1,1}},n,F[2][2],I2[2][2]={{1,0},{0,1}};
FILE *f1=fopen("kfib.in","r"),*f2=fopen("kfib.out","w");
void asign( long X[2][2],long  Y[2][2]){ X[0][0]=Y[0][0];X[1][0]=Y[1][0];X[0][1]=Y[0][1];X[1][1]=Y[1][1];}
void inmulteste(long a[2][2],long x[2][2])
{
      int u,y,z,t;
      u=a[0][0]*x[0][0]+a[0][1]*x[1][0]%666013;
      y=a[0][0]*x[0][1]+a[0][1]*x[1][1]%666013;
      z=a[1][0]*x[0][0]+a[1][1]*x[1][0]%666013;
      t=a[1][0]*x[0][1]+a[1][1]*x[1][1]%666013;
      a[0][0]=u;
      a[1][0]=y;
      a[0][1]=z;
      a[1][1]=t;
}
void exp(long x[2][2],long p)
{
    if(p==0)asign(x,I2);
    if(p==1)asign(x,A);
    else if(p%2==0){exp(x,p/2);inmulteste(x,x);}
    else if(p%2!=0){exp(x,p/2);inmulteste(x,x);inmulteste(x,A);}
}
int main()
{
  fscanf(f1,"%ld",&n);
  exp(F,n-1);
  fprintf(f2,"%ld",F[1][1]);
  return 0;
}

//Our greatest weakness lies in giving up. The most certain way to succeed is always to try just one more time.