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 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)
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
Sample Output
Test instructions: A row of lights, give you the initial state, and then every second will have this operation: if the left side of the lamp is bright, then change the state, otherwise unchanged, the leftmost reference to the right of the
Idea: Very easy to find: A1 = (a1+an)%2, a2 = (A2 + a1)% 2 ... an = (an + an-1)%2
Then constructs a similar matrix: 1 0 0 1, bit operation is much faster, due to the particularity of this problem
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 (int 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 '; A.init (); a.v[0][cnt-1] = a.v[0][0] = 1;for (int i = 1; i < Cnt i++) A.v[i][i] = a.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]);p rintf ("\ n");} return 0;}
HDU-2276 Kiki & Little Kiki 2