1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
| #include <stdio.h> #include <stdlib.h>
int n; int **C; void AddM(int **a, int **b, int **temp, int length, int ax, int ay, int bx, int by, int tx, int ty) { for (int i = 0; i < length; i++) { for (int j = 0; j < length; j++) { temp[tx + i][ty + j] = a[ax+i][ay+j] + b[bx+i][by+j]; } } } void SubM(int **a, int **b, int **temp, int length, int ax, int ay, int bx, int by, int tx, int ty) { for (int i = 0; i < length; i++) { for (int j = 0; j < length; j++) { temp[tx + i][ty + j] = a[ax+i][ay+j] - b[bx+i][by+j]; } } }
void del(int n, int **a) { for (int i = 0; i < n; ++i) { free(a[i]); } free(a); }
int **MulM(int **a, int **b, int length, int ax, int ay, int bx, int by) { int **temp; if (length == 1) { temp = (int **)malloc(sizeof(int *) * 1); temp[0] = (int *)malloc(sizeof(int) * 1); temp[0][0] = a[ax][ay] * b[bx][by]; return temp; } int l; if (length % 2 == 0) { l = length / 2; temp = (int **)malloc(sizeof(int *) * length); for (int i = 0; i < length; i++) { temp[i] = (int *)malloc(sizeof(int) * length); } int ax11 = ax; int ay11 = ay; int ax12 = ax; int ay12 = ay + l; int ax21 = ax + l; int ay21 = ay; int ax22 = ax + l; int ay22 = ay + l; int bx11 = bx; int by11 = by; int bx12 = bx; int by12 = by + l; int bx21 = bx + l; int by21 = by; int bx22 = bx + l; int by22 = by + l; int tx11 = 0; int ty11 = 0; int tx12 = 0; int ty12 = l; int tx21 = l; int ty21 = 0; int tx22 = l; int ty22 = l; int **t; int **t2; t = (int **)malloc(sizeof(int *) * l); t2 = (int **)malloc(sizeof(int *) * l); for (int i = 0; i < l; i++) { t[i] = (int *)malloc(sizeof(int) * l); t2[i] = (int *)malloc(sizeof(int) * l); } SubM( b , b , t , l, bx12 , by12 , bx22 , by22 , 0, 0); int **m1 = MulM(a, t, l, ax11, ay11, 0, 0); AddM( a , a , t , l, ax11 , ay11 , ax12 , ay12 ,0, 0); int **m2 = MulM(t, b, l, 0, 0, bx22,by22); AddM( a , a , t , l, ax21 , ay21 , ax22 , ay22 ,0, 0); int **m3 = MulM(t, b, l, 0, 0 , bx11, by11); SubM( b , b , t , l, bx21 , by21 , bx11 , by11 , 0, 0); int **m4 = MulM(a, t, l, ax22, ay22 , 0, 0); AddM( a , a , t , l, ax11 , ay11 , ax22 , ay22 , 0, 0); AddM( b , b , t2 , l, bx11 , by11 , bx22 , by22 , 0, 0); int **m5 = MulM(t, t2, l, 0, 0 , 0, 0); SubM( a , a , t , l, ax12 , ay12 , ax22 , ay22 , 0, 0); AddM( b , b , t2 , l, bx21 , by21 , bx22 , by22 , 0, 0); int **m6 = MulM(t, t2, l, 0, 0 , 0, 0); SubM( a , a , t , l, ax11 , ay11 , ax21 , ay21 , 0, 0); AddM( b , b , t2 , l, bx11 , by11 , bx12 , by12 , 0, 0); int **m7 = MulM(t, t2, l, 0, 0 , 0, 0); AddM( m5 , m4 , temp , l, 0 , 0 , 0 , 0 , tx11,ty11); SubM( temp , m2 , temp , l, tx11,ty11 , 0 , 0 , tx11,ty11); AddM( temp , m6 , temp , l, tx11,ty11 , 0 , 0 , tx11,ty11); del(l,m6); AddM( m1 , m2 , temp , l, 0 , 0 , 0 , 0 , tx12,ty12); del(l,m2); AddM( m3 , m4 , temp , l, 0 , 0 , 0 , 0 , tx21,ty21); del(l,m4); AddM( m5 , m1 , temp , l, 0 , 0 , 0 , 0 , tx22,ty22); SubM( temp , m3 , temp , l, tx22,ty22, 0 , 0 , tx22,ty22); SubM( temp , m7 , temp , l, tx22,ty22, 0 , 0 , tx22,ty22); del(l,m1);del(l,m3);del(l,m5);del(l,m7); return temp; } else { l = length / 2 + 1; int **at; int **bt; at = (int **)malloc(sizeof(int *) * (length + 1)); bt = (int **)malloc(sizeof(int *) * (length + 1)); temp = (int **)malloc(sizeof(int *) * (length + 1)); for (int i = 0; i < length + 1; i++) { temp[i] = (int *)malloc(sizeof(int) * (length + 1)); at[i] = (int *)malloc(sizeof(int) * (length + 1)); bt[i] = (int *)malloc(sizeof(int) * (length + 1)); } for (int i = 0; i < length + 1; i++) { for (int j = 0; j < length + 1; j++) { if (i == length || j == length) { at[i][j] = 0; bt[i][j] = 0; } else { at[i][j] = a[ax + i][ay + j]; bt[i][j] = b[bx + i][by + j]; } } } int ax11 = 0; int ay11 = 0; int ax12 = 0; int ay12 = l; int ax21 = l; int ay21 = 0; int ax22 = l; int ay22 = l; int bx11 = 0; int by11 = 0; int bx12 = 0; int by12 = l; int bx21 = l; int by21 = 0; int bx22 = l; int by22 = l; int tx11 = 0; int ty11 = 0; int tx12 = 0; int ty12 = l; int tx21 = l; int ty21 = 0; int tx22 = l; int ty22 = l; int **t; int **t2; t = (int **)malloc(sizeof(int *) * l); t2 = (int **)malloc(sizeof(int *) * l); for (int i = 0; i < l; i++) { t[i] = (int *)malloc(sizeof(int) * l); t2[i] = (int *)malloc(sizeof(int) * l); } SubM( bt , bt , t , l, bx12 , by12 , bx22 , by22 , 0, 0); int **m1 = MulM(at, t, l, ax11, ay11, 0, 0); AddM( at , at , t , l, ax11 , ay11 , ax12 , ay12 ,0, 0); int **m2 = MulM(t, bt, l, 0, 0, bx22,by22); AddM( at , at , t , l, ax21 , ay21 , ax22 , ay22 ,0, 0); int **m3 = MulM(t, bt, l, 0, 0 , bx11, by11); SubM( bt , bt , t , l, bx21 , by21 , bx11 , by11 , 0, 0); int **m4 = MulM(at, t, l, ax22, ay22 , 0, 0); AddM( at , at , t , l, ax11 , ay11 , ax22 , ay22 , 0, 0); AddM( bt , bt , t2 , l, bx11 , by11 , bx22 , by22 , 0, 0); int **m5 = MulM(t, t2, l, 0, 0 , 0, 0); SubM( at , at , t , l, ax12 , ay12 , ax22 , ay22 , 0, 0); AddM( bt , bt , t2 , l, bx21 , by21 , bx22 , by22 , 0, 0); int **m6 = MulM(t, t2, l, 0, 0 , 0, 0); SubM( at , at , t , l, ax11 , ay11 , ax21 , ay21 , 0, 0); AddM( bt , bt , t2 , l, bx11 , by11 , bx12 , by12 , 0, 0); int **m7 = MulM(t, t2, l, 0, 0 , 0, 0); AddM( m5 , m4 , temp , l, 0 , 0 , 0 , 0 , tx11,ty11); SubM( temp , m2 , temp , l, tx11,ty11 , 0 , 0 , tx11,ty11); AddM( temp , m6 , temp , l, tx11,ty11 , 0 , 0 , tx11,ty11); del(l,m6); AddM( m1 , m2 , temp , l, 0 , 0 , 0 , 0 , tx12,ty12); del(l,m2); AddM( m3 , m4 , temp , l, 0 , 0 , 0 , 0 , tx21,ty21); del(l,m4); AddM( m5 , m1 , temp , l, 0 , 0 , 0 , 0 , tx22,ty22); SubM( temp , m3 , temp , l, tx22,ty22, 0 , 0 , tx22,ty22); SubM( temp , m7 , temp , l, tx22,ty22, 0 , 0 , tx22,ty22); del(l,m1);del(l,m3);del(l,m5);del(l,m7); del(length,at);del(length,bt); return temp; } }
int main() { scanf("%d", &n); int **A; int **B; A = (int **)malloc(sizeof(int *) * (n + 1)); B = (int **)malloc(sizeof(int *) * (n + 1)); for (int i = 0; i < n + 1; i++) { A[i] = (int *)malloc(sizeof(int) * (n + 1)); B[i] = (int *)malloc(sizeof(int) * (n + 1)); } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) scanf("%d", &A[i][j]); } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) scanf("%d", &B[i][j]); } C = MulM(A, B, n, 0, 0, 0, 0); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) printf("%d ", C[i][j]); } }
|