Hdu2276-Kiki & little Kiki 2 (matrix power)

Source: Internet
Author: User

Question Link


Question:There are n lamps numbered from 1 to n. They are circled in a circle, that is to say, the left side of Lamp 1 is lamp n. If the light on the left of a lamp is on at T Second, the light state will be changed at t + 1 second. Enter m and initial light status. Output the status of all lights after M seconds.

Ideas:In fact, the sum of the States of each lamp is related to itself, so we can get a relational matrix. Assuming there are 6 lamps, the relational matrix can be obtained as follows:
(1, 0, 0, 0, 0, 1)
(1, 1, 0, 0, 0, 0)
(0, 1, 1, 0, 0, 0)
(0, 0, 1, 1, 0, 0)
(0, 0, 0, 1, 1, 0)
(0, 0, 0, 0, 1, 1)
In this way, we can obtain the relational matrix of N lamps, and then use the Matrix to quickly calculate the power.

PS: At the beginning, I used recursion for the fast power, but the stack was violent... Later it was changed to non-recursive.

Code:

# Include <iostream> # include <cstdio> # include <cstring> # include <cmath> # include <algorithm> using namespace STD; const int maxn = 105; struct mat {int s [maxn] [maxn]; int L; MAT (INT Len) {memset (S, 0, sizeof (s); L = Len ;} mat operator * (const mat & C) {mat ans (l); memset (ans. s, 0, sizeof (ans. s); For (INT I = 0; I <L; I ++) for (Int J = 0; j <L; j ++) {for (int K = 0; k <L; k ++) ans. s [I] [J] = (ANS. S [I] [J] + s [I] [k] * C. s [k] [J]); ans. s [I] [J] = ans. s [I] [J] % 2;} return ans ;}}; char STR [maxn]; int t; MAT pow_mod (MAT C, int K) {/* If (k = 1) return C; MAT a = pow_mod (C, K/2); MAT ans = A * A; If (K % 2) ans = ans * C; return ans; */MAT ans = C; k --; while (k) {If (K & 1) ans = ans * C; k> = 1; C = C * C;} return ans;} int main () {While (scanf ("% d", & T )! = EOF) {scanf ("% s", STR); int L = strlen (STR); mat c (l); For (INT I = 0; I <L; I ++) for (Int J = 0; j <L; j ++) {if (I = 0) C. s [I] [0] = C. s [I] [L-1] = 1; else C. s [I] [I-1] = C. s [I] [I] = 1;} mat TMP (l); For (INT I = 0; I <L; I ++) TMP. s [I] [0] = STR [I]-'0'; MAT ans = pow_mod (C, T); ans = ans * TMP; For (INT I = 0; I <L; I ++) printf ("% d", ans. s [I] [0]); printf ("\ n");} return 0 ;}


Hdu2276-Kiki & little Kiki 2 (matrix power)

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.