Cod sursa(job #973549)

Utilizator petrutsxMihaela Petruta Gaman petrutsx Data 14 iulie 2013 18:44:07
Problema Al k-lea termen Fibonacci Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.54 kb
#include<stdio.h>
#define modulo 666013

int main(){
	int K, i;
	int mult1, mult2, term, aux, auxK;

	FILE *pf, *pg;

	pf = fopen("kfib.in", "r");
	pg = fopen("kfib.out", "w");
	fscanf(pf,"%d", &K);

	if(K == 1 || K == 2)
		term = 1;
	else
		if(K == 3)
			term = 2;
		else{
	
			mult1 = 1;
			mult2 = 2;
			auxK = 2 * (modulo + 1);
			K = K % auxK;
			for(i=4; i<=K; i++){
				aux = mult2 % modulo;
				mult2 = (mult1 + mult2) % modulo;
				mult1 = aux;
			}
			term = mult2;
		}

	fprintf(pg, "%d\n", term);
	fclose(pf);
	fclose(pg);

	return 0;
}