UVA 1386-cellular Automaton
option=com_onlinejudge&itemid=8&page=show_problem&category=489&problem=4132&mosmsg= submission+received+with+id+13911770 "target=" _blank "style=" "> Topic Links
Test instructions: Given an n-lattice ring, there is now a distance of D. Each change adds the ring to a grid within the distance of D, and the result is mod m, which asks the number on the ring after the K-transform.
Idea: The matrix is very good to think, each position corresponding to a few positions around 1. The remaining position is 0, but this matrix has 500. A bit large, direct n^3 to find the matrix is not very appropriate. Then it is observed that the matrix is a cyclic matrix, and the cyclic matrix is multiplied, so it is only necessary to save one line. And then using n^2 time is enough to calculate
Code:
#include <stdio.h> #include <string.h>const int N = 505;long long N, M, D, K, a[n];struct mat {long Long v[n];m at () {memset (V, 0, sizeof (v));} Mat operator * (Mat c) {mat ans;for (int i = 0; i < n; i++) {for (int j = 0; J < N; j + +) {Ans.v[i] = ((Ans.v[i] + v[ J] * c.v[(j-i + N)% n])% m + m)% m; }} return ans; }};mat Pow_mod (Mat X, long long k) {if (k = = 1) return X;mat SB = x * X;mat ans = pow_mod (SB, k>>1); if (k&1) ans = ans * x;return ans;} int main () {while (~scanf ("%lld%lld%lld%lld", &n, &m, &d, &k)) {Mat start;for (int i = 0; I <= D; i++) s Tart.v[i] = 1;for (int i = n-1; i > n-1-D; i--) start.v[i] = 1;start = Pow_mod (start, k); for (int i = 0; i < n; i++) scanf ("%lld", &a[i]), for (int i = 0; i < n; i++) {long Long ans = 0;for (int j = 0; J < N; J + +) ans = ((ans + a[j] * start.v[(j-i + N)% n])% m + m)% m;printf ("%lld%c", ans, (i = = n-1?) ' \ n ': '); }}return 0;}
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
UVA 1386-cellular Automaton (cyclic matrix)