20251002 baekjoon
This commit is contained in:
parent
9a209363fe
commit
2b46e3d9a6
98
code_study/Baekjoon/c/10830.c
Normal file
98
code_study/Baekjoon/c/10830.c
Normal file
@ -0,0 +1,98 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
int** allocation(int N, bool input);
|
||||
void free_memory(int** arr, int N);
|
||||
void printArr(int** arr, int N);
|
||||
int** multyply(int** A, int** B, int N);
|
||||
int** product(int** arr, int N, long long B);
|
||||
|
||||
int main() {
|
||||
int N;
|
||||
long long B;
|
||||
scanf("%d %lld", &N, &B);
|
||||
int** arr = allocation(N, true);
|
||||
int** result = product(arr, N, B);
|
||||
|
||||
printArr(result, N);
|
||||
|
||||
free_memory(arr, N);
|
||||
free_memory(result, N);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int** allocation(int N, bool input) {
|
||||
int** arr = (int**)malloc(sizeof(int*)*N);
|
||||
|
||||
for(int i=0; i<N; i++) {
|
||||
arr[i] = (int*)malloc(sizeof(int)*N);
|
||||
|
||||
if(input) {
|
||||
for(int n=0; n<N; n++) scanf("%d", &arr[i][n]);
|
||||
}
|
||||
}
|
||||
|
||||
return arr;
|
||||
}
|
||||
|
||||
void free_memory(int** arr, int N) {
|
||||
for(int i=0; i<N; i++) free(arr[i]);
|
||||
free(arr);
|
||||
}
|
||||
|
||||
void printArr(int** arr, int N) {
|
||||
for(int i=0; i<N; i++) {
|
||||
for(int j=0; j<N; j++) {
|
||||
printf("%d ",arr[i][j]);
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
int** multyply(int** A, int** B, int N) {
|
||||
int** result = allocation(N, false);
|
||||
|
||||
for(int y=0; y<N; y++) {
|
||||
for(int x=0; x<N; x++) {
|
||||
result[y][x] = 0;
|
||||
|
||||
for(int i=0; i<N; i++) {
|
||||
result[y][x] += A[y][i] * B[i][x];
|
||||
}
|
||||
|
||||
result[y][x] %= 1000;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int** product(int** arr, int N, long long B) {
|
||||
if (B==1) {
|
||||
int** basedArr = allocation(N, false);
|
||||
for(int y=0; y<N; y++) {
|
||||
for(int x=0; x<N; x++) {
|
||||
basedArr[y][x] = arr[y][x] % 1000;
|
||||
}
|
||||
}
|
||||
|
||||
return basedArr;
|
||||
}
|
||||
|
||||
int** half = product(arr, N, B/2);
|
||||
int** temp = multyply(half, half, N);
|
||||
free_memory(half, N);
|
||||
|
||||
if (B%2 == 1) {
|
||||
int** result = multyply(temp, arr, N);
|
||||
free_memory(temp, N);
|
||||
|
||||
return result;
|
||||
}
|
||||
else {
|
||||
return temp;
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user