HDU-2276 Kiki & little Kiki 2

Source: Internet
Author: User

Description

There are n lights in a circle numbered from 1 to n. the left of light 1 is light N, and the left of light K (1 <k <= N) is 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 is not on, turn on it) at t + 1 second (T> = 0 ), if the left of light I is on !!! Given the initiation state, please find all lights 'State after M second. (2 <=n <= 100, 1 <= m <= 10 ^ 8)

Input

The input contains one or more data sets. the first line of each data set is an integer m indicate the time, the second line will be a string T, only contains '0' and '1 ', and its length N will 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 is on, otherwise the light is off.

Output

For each data set, output all lights 'State at M seconds in one line. It only contains character '0' and '1.

Sample Input

 
1010111110100000001

Sample output

 
1111000001000010

A: A light is arranged to give you an initial state, and then there will be such an operation every second: If the left side of the light is on, it will change the state, otherwise it will not change, leftmost reference rightmost

Train of Thought: A1 = (A1 + an) % 2, a2 = (A2 + A1) % 2 ......... an = (An + an-1) % 2

Then construct a matrix like: 1 0 0 1. Bit operations are much faster, because of the particularity of this question.

1 1 0 0

0 1 1 0

0 0 1 1

# Include <iostream> # include <cstring> # include <cstdio> # include <algorithm> # include <cmath> using namespace STD; const int maxn = 105; const int mod = 2; int CNT; struct matrix {int V [maxn] [maxn]; matrix () {} matrix (int x) {Init (); for (INT I = 0; I <maxn; I ++) V [I] [I] = x;} void Init () {memset (v, 0, sizeof (v);} Matrix Operator * (matrix const & B) const {matrix C; C. init (); For (INT I = 0; I <CNT; I ++) for (in T j = 0; j <CNT; j ++) for (int K = 0; k <CNT; k ++) C. V [I] [J] ^ = (V [I] [k] & B. V [k] [J]); Return C;} matrix operator ^ (int B) {matrix A = * This, res (1); while (B) {If (B & 1) RES = res * A; A = A * A; B >>= 1;} return res ;}} A, B, TMP; int main () {int t; char STR [maxn]; int num [maxn]; while (scanf ("% d", & T )! = EOF) {scanf ("% s", STR); CNT = strlen (STR); For (INT I = 0; I <CNT; I ++) num [I] = STR [I]-'0';. init ();. V [0] [cnt-1] =. V [0] [0] = 1; for (INT I = 1; I <CNT; I ++). V [I] [I] =. V [I] [I-1] = 1; TMP = a ^ t; int ans [maxn]; memset (ANS, 0, sizeof (ANS )); for (INT I = 0; I <CNT; I ++) if (Num [I]) for (Int J = 0; j <CNT; j ++) if (TMP. V [J] [I]) ans [J] = (ANS [J] + (TMP. V [J] [I] * num [I]) % mod) % MOD; For (INT I = 0; I <CNT; I ++) printf ("% d", ANS [I]); printf ("\ n");} return 0 ;}


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.