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
| #include <iostream> #include <cstring> #include <cstdlib> #include <algorithm> #include <queue> #include <cmath> #include <cstdio> #define mem(a, b) memset(a, b, sizeof(a)) #define PI 3.1415926535 using namespace std; typedef long long ll; const int MAXn = 55;
struct matirix { int a[MAXn][MAXn]; } r, o; int sum[MAXn], n, m, a, b, c, t; matirix cheng(matirix a, matirix b) { int x, y, z; matirix t; mem(t.a, 0); for (x = 0; x < n; x++) for (y = 0; y < n; y++) { for (z = 0; z < n; z++) { t.a[x][y] += (a.a[x][z] * b.a[z][y]) % m; t.a[x][y] %= m; } } return t; } void sort_pow(int t) { while (t) { if (t & 1) r = cheng(r, o); o = cheng(o, o); t >>= 1; } } void init() { mem(r.a,0); mem(o.a,0); int i, j; scanf("%d%d%d%d%d", &m, &a, &b, &c, &t); r.a[0][0] = r.a[n - 1][n - 1] = 1; o.a[0][0] = b; o.a[0][1] = c; o.a[n - 1][n - 2] = a; o.a[n - 1][n - 1] = b; for (i = 1; i < n - 1; i++) { o.a[i][i - 1] = a; o.a[i][i] = b; o.a[i][i + 1] = c; r.a[i][i] = 1; } sort_pow(t); for (i = 0; i < n; i++) scanf("%d", sum + i); for (i = 0; i < n; i++) { int ans = 0; for (j = 0; j < n; j++) { ans += sum[j] * r.a[i][j]; ans %= m; } if (i != 0) printf(" "); printf("%d", ans); } printf("\n"); } int main() { while (1) { scanf("%d", &n); if (n == 0) break; init(); } }
|