20251024 baekjoon

This commit is contained in:
songyc macbook 2025-10-24 23:57:54 +09:00
parent d8151ea4fa
commit 68f05d2f50

View File

@ -0,0 +1,71 @@
#include <stdio.h>
// Fn | 1 1 | ^n-1 F1(1)
// Fn-1 | 1 0 | F0(0)
#define MOD 1000000007
typedef long long matrix[2][2];
void multyply_matrix(matrix A, matrix B, matrix result);
void power_matrix(matrix A, long long exp, matrix result);
int main() {
long long n;
scanf("%lld", &n);
if (n <= 1) {
printf("%lld\n", n);
return 0;
}
matrix A = {{1, 1}, {1, 0}};
matrix result_matrix;
power_matrix(A, n - 1, result_matrix);
printf("%lld\n", result_matrix[0][0]);
return 0;
}
void multyply_matrix(matrix A, matrix B, matrix result) {
matrix temp;
temp[0][0] = (A[0][0] * B[0][0] + A[0][1] * B[1][0]) % MOD;
temp[0][1] = (A[0][0] * B[0][1] + A[0][1] * B[1][1]) % MOD;
temp[1][0] = (A[1][0] * B[0][0] + A[1][1] * B[1][0]) % MOD;
temp[1][1] = (A[1][0] * B[0][1] + A[1][1] * B[1][1]) % MOD;
result[0][0] = temp[0][0];
result[0][1] = temp[0][1];
result[1][0] = temp[1][0];
result[1][1] = temp[1][1];
}
void power_matrix(matrix A, long long exp, matrix result) {
if (exp == 0) {
result[0][0] = 1; result[0][1] = 0;
result[1][0] = 0; result[1][1] = 1;
return;
}
if (exp == 1) {
result[0][0] = A[0][0]; result[0][1] = A[0][1];
result[1][0] = A[1][0]; result[1][1] = A[1][1];
return;
}
matrix half;
power_matrix(A, exp / 2, half);
matrix temp;
multyply_matrix(half, half, temp);
if (exp % 2 == 1) {
multyply_matrix(temp, A, result);
} else {
result[0][0] = temp[0][0];
result[0][1] = temp[0][1];
result[1][0] = temp[1][0];
result[1][1] = temp[1][1];
}
}