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
| #include "bits/stdc++.h" using namespace std; const int MOD = 2009; const int MAT[][4] = { {1, 3, 3, 1}, {0, 1, 4, 4}, {0, 0, 1, 2}, {0, 0, 0, 1} }; const int TABLE1[] = {1, 4, 2, 1}; const int TABLE2[] = {7, 9, 3, 1}; struct Mat { int mat[4][4]; Mat() { memset(mat, 0, sizeof(mat)); } friend Mat operator * (Mat n, Mat m) { Mat res; for (int k = 0; k < 4; k++) for (int i = 0; i < 4; i++) for (int j = 0; j < 4; j++) res.mat[i][j] = (res.mat[i][j] + n.mat[i][k] * m.mat[k][j]) % MOD; return res; } } m; Mat mat_pow(Mat n, int k) { Mat res; for (int i = 0; i < 4; i++) { res.mat[i][i] = 1; } while (k) { if (k & 1) { res = res * n; } n = n * n; k >>= 1; } return res; } int main() { int n; while (scanf("%d", &n) && n) { if (n == 1) { puts("1"); continue; } if (n == 2) { puts("7"); continue; } memmove(m.mat, MAT, sizeof(m.mat)); m = mat_pow(m, n - 1 >> 1); int res = 0;
if (n & 1) { for (int i = 0; i < 4; i++) { res = (res + m.mat[0][i] * TABLE1[i]) % MOD; } } else { for (int i = 0; i < 4; i++) { res = (res + m.mat[0][i] * TABLE2[i]) % MOD; } } printf("%d\n", res); } return 0; }
|