Cod sursa(job #854915)

Utilizator roots4Irimia Alexandru Gabriel roots4 Data 14 ianuarie 2013 12:41:10
Problema Nunta Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.76 kb
#include<fstream>
#include<string.h>
#define DIM 2

using namespace std;

ifstream f("nunta.in");
ofstream g("nunta.out");

int sol[DIM][DIM],P[DIM][DIM],temp[DIM][DIM];

int n;

void multiply(int A[DIM][DIM],int B[DIM][DIM]){
	
	for(int a=0;a<DIM;a++){
		for(int b=0;b<DIM;b++){
			temp[a][b]=0;
			for(int k=0;k<DIM;k++)
				temp[a][b]+=A[a][k]*B[k][b];
		}
	}
	
}

int main(){
	
	f>>n;
	
	if(n<=2){
		if(n==1)
			g<<"1";
		else
			g<<"2";
	}
	else{
		n-=2;
		P[0][0]=P[0][1]=P[1][0]=1;
		sol[0][0]=sol[1][1]=1;
		while(n){
			if(n%2==1){
				multiply(sol,P);
				memcpy(sol,temp,sizeof(temp));
			}
			multiply(P,P);
			memcpy(P,temp,sizeof(temp));
			n/=2;
		}
		
		g<<sol[0][0]*2+sol[0][1];
		
	}
	
	return 0;
}