Cod sursa(job #854914)

Utilizator roots4Irimia Alexandru Gabriel roots4 Data 14 ianuarie 2013 12:36:39
Problema Nunta Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.86 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],A[DIM][DIM];

int n;

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

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