UVA 1386-cellular Automaton (cyclic matrix)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.