Cod sursa(job #854919)

Utilizator roots4Irimia Alexandru Gabriel roots4 Data 14 ianuarie 2013 12:51:45
Problema Nunta Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.89 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 multiply1(){
	int a,b,k;
	for(a=0;a<DIM;a++){
		for(b=0;b<DIM;b++){
			temp[a][b]=0;
			for(k=0;k<DIM;k++)
				temp[a][b]+=sol[a][k]*P[k][b];
		}
	}
	
}


void multiply2(){
	int a,b,k;
	for(a=0;a<DIM;a++){
		for(b=0;b<DIM;b++){
			temp[a][b]=0;
			for(k=0;k<DIM;k++)
				temp[a][b]+=P[a][k]*P[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){
				multiply1();
				memcpy(sol,temp,sizeof(temp));
			}
			multiply2();
			memcpy(P,temp,sizeof(temp));
			n/=2;
		}
		
		g<<sol[0][0]*2+sol[0][1];
		
	}
	
	return 0;
}