Cod sursa(job #3371482)

Utilizator AnSeDraAndrei Sebastian Dragulescu AnSeDra Data 30 octombrie 2026 15:46:13
Problema Al k-lea termen Fibonacci Scor 100
Compilator c-64 Status done
Runda Arhiva educationala Marime 1.16 kb
#include <stdio.h>
#include <stdlib.h>

#define SIZE 2
#define MOD 666013

void multiply_matrix(int A[SIZE][SIZE], int B[SIZE][SIZE], int result[SIZE][SIZE]) 
{
	for (int i = 0; i < SIZE; i++) {
		for (int j = 0; j < SIZE; j++) {
			result[i][j] = 0;
			for(int k = 0; k < SIZE; k++){
				result[i][j] = (result[i][j] + 1LL * A[i][k] * B[k][j]) % MOD;
			}
		}
	}
}

void copy_matrix(int to[SIZE][SIZE], int from[SIZE][SIZE]) 
{
	for (int i = 0; i < SIZE; i++) {
		for(int j = 0; j < SIZE; j++) {
			to[i][j] = from[i][j];
		}
	}
}

void lgput(int matrix[SIZE][SIZE], int pow)
{
	int result[SIZE][SIZE] = {{1, 0}, {0, 1}}, aux[SIZE][SIZE];

	while (pow) {
		if (pow & 1) {
			multiply_matrix(result, matrix, aux);
			copy_matrix(result, aux);
		}

		multiply_matrix(matrix, matrix, aux);
		copy_matrix(matrix, aux);
		pow >>= 1;
	}

	copy_matrix(matrix, result);
}

int main(void)
{
	int n, first_element;
	int matrix[SIZE][SIZE] = {{1, 1}, {1, 0}};

	FILE* file = fopen("kfib.in", "r");
	fscanf(file, "%d", &n);
	fclose(file);

	first_element = 1;
	lgput(matrix, n - 1);

	file = fopen("kfib.out", "w");
	fprintf(file, "%d\n", first_element * matrix[0][0]);
	fclose(file);

	return 0;
}