hdu2276---Kiki & Little Kiki 2 (Matrix)

Source: Internet
Author: User

Problem Description
There 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 left Of light I am on!!! Given the initiation State, please find all lights ' state after M second. (2<= n <=, 1<= m<= 10^8)

Input
The 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.

Output
For each data set, the output all lights ' state is at m seconds on one line. It is only contains character ' 0 ' and ' 1.

Sample Input

1 0101111 10 100000001

Sample Output

1111000 001000010

Source
HDU 8th Programming Contest Site (1)

Recommend
LCY | We have carefully selected several similar problems for you:2256 2254 3117 2855 2971

The state of each lamp is determined by the one on its left.
Constructs a n*n coefficient matrix, each row, the corresponding position of each column is 1,
For example, a coefficient matrix of 3 numbers
1 1 0
0 1 1
1 0 1
Then the initial state of 110, which is 101 after 1s
That is, [1 1 0] to multiply the new matrix obtained by the above matrix
Then we just need the matrix to speed up the power.

/************************************************************************* > File Name:hdu2276.cpp > Auth Or:alex > Mail: [email protected] > Created time:2015 March 12 Thursday 21:28 55 seconds ******************************** ****************************************/#include <map>#include <set>#include <queue>#include <stack>#include <vector>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>using namespace STD;Const DoublePI =ACOs(-1.0);Const intINF =0x3f3f3f3f;Const DoubleEPS =1e-15;typedef Long LongLL;typedefPair <int,int> PLL;intNclassmartix{ Public:intmat[ the][ the];        Martix (); Martixoperator* (ConstMartix &b)Const; martix&operator= (ConstMartix &b);}; Martix:: Martix () {memset(Mat,0,sizeof(MAT));} Martix Martix::operator* (ConstMartix &b)Const{Martix ret; for(inti =0; I < n; ++i) { for(intj =0; J < N; ++J) { for(intK =0; K < n; ++K) {Ret.mat[i][j] + = ( ThisMAT[I][K] * b.mat[k][j]); RET.MAT[I][J]%=2; }        }    }returnRET;} martix& Martix::operator= (ConstMartix &b) { for(inti =0; I < n; ++i) { for(intj =0; J < N; ++J) { ThisMAT[I][J] = B.mat[i][j]; }    }return* This;} Martix Fastpow (Martix A,intm) {//void Debug (Martix A);Martix ret; for(inti =0; I < n; ++i) {Ret.mat[i][i] =1; } while(m) {if(M &1) {ret = ret * A; } m >>=1;    A = a * A; }returnRET;}voidDebug (Martix A) { for(inti =0; I < n; ++i) { for(intj =0; J < N; ++J) {printf("%d", A.mat[i][j]); }printf("\ n"); }}Charstr[ the];intMain () {intM while(~scanf("%d", &m)) {scanf('%s ', str);        Martix F; n =strlen(str); Martix A; for(inti =0; I < n-1; ++i) {A.mat[i][i] = a.mat[i][i +1] =1; } A.mat[n-1][0] = A.mat[n-1][n-1] =1;//Debug (A);A = Fastpow (A, m); for(inti =0; I < n; ++i) {f.mat[0][i] = str[i]-' 0 '; } f = f * A; for(inti =0; I < n; ++i) {printf("%d", f.mat[0][i]); }printf("\ n"); }return 0;}

hdu2276---Kiki &amp; Little Kiki 2 (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.