Hdu 2276 Kiki & Little Kiki 2 matrix fast Power

Source: Internet
Author: User

Kiki & Little Kiki 2Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 2300 Accepted Submission (s): 1175


Problem descriptionthere is n lights in a circle numbered from 1 to N. The left of light 1 are light n, and the Left of Light K (1< k<= N) are the light k-1. At time of 0, some of them turn on, and others turn off.
Change The state of light I (if it's on, turn off it; if it isn't on, turn in it) at t+1 second (T >= 0), if the L EFT of Light I was on!!! Given the initiation State, please find all lights ' state after M second. (2<= n <=, 1<= m<= 10^8)


Inputthe input contains one or more data sets. The first line of each data set was an integer m indicate the time, the second line would be a string T, only contains ' 0 ' a nd ' 1 ', and its length n would not exceed 100. It means all lights in the circle from 1 to N.
If the ith character of T is ' 1 ', it means the light I am on, and otherwise the light is off.


Outputfor each data set and output all lights ' state at M seconds on one line. It is only contains character ' 0 ' and ' 1.
Sample Input
1010111110100000001

Sample Output
1111000001000010

Test instructions: Tell a 01 sequence to indicate the light switch situation, 0 is off, 1 is open, now to operate the times, if a number to the left is 1 to change the state of this number, the number on the left of the first number is the rightmost number.

Analysis: The first reaction is to find the circulation section, but directly stored up will be MLE, find and then calculate will Tle, after the game only know is the fast power of proof. For each number, the operation is only related to the number of the current and to the left of it, it has nothing to do with the other number, so when constructing the matrix, it can be thought that only the current position is related to its left position, only the number of these two positions is 1, the others are 0.


#include <iostream> #include <stdio.h> #include <string> #include <cstring> #include <cmath >typedef __int64 ll;using namespace std;struct matrix{int f[102][102];}    S,t;char str[109];int mp[109][109];int Ss[109];matrix mul (Matrix A,matrix b,int len) {matrix C;    memset (c.f,0,sizeof c.f);                 for (int i=0;i<len;i++) {for (int. j=0;j<len;j++) {for (int k=0;k<len;k++) {            C.f[i][j]= (c.f[i][j]+a.f[i][k]*b.f[k][j])%2; }}} return C;}    Matrix Fun (Matrix A,ll n,int len) {matrix B;         for (int i=0;i<len;i++) for (int j=0;j<len;j++) if (i==j) b.f[i][j]=1;         else b.f[i][j]=0;             while (n) {if (n&1) B=mul (B,a,len);             A=mul (A,a,len);         n>>=1; } return B;    int main () {ll n;        while (~SCANF ("%i64d", &n)) {scanf ("%s", str);        int Len=strlen (str); Memset (S. f,0,sizeof s.f);        for (int i=0;i<len;i++)//construct matrix if (i==0 | | i==len-1) s.f[i][0]=1;        for (int j=1;j<len;j++) for (int i=0;i<len;i++) if (i==j | | i==j-1) s.f[i][j]=1;            for (int i=0;i<len;i++) ss[i]=str[i]-' 0 ';            T=fun (S, N, Len);                for (int i=0;i<len;i++) {int ans=0;                for (int j=0;j<len;j++) {ans= (ans+ss[j]*t.f[j][i])%2;            } printf ("%d", ans);    } puts (""); } return 0;}








Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hdu 2276 Kiki & Little Kiki 2 matrix fast 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.