Kiki & Little Kiki 2Time
limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 2213 Accepted Submission (s): 1137
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
SOURCEHDU 8th Programming Contest Site (1)
Recommendlcy | We have carefully selected several similar problems for you:1757 1588 2604 2256 2294
Test Instructions: There are n 0-1 string rings, if the left side of a number is 1 then the state of this number will be changed. The left side of the first number is the nth number. Ask the status string after M seconds
Observation: The last digit change-->?1 10-->?1 11-->?0 00-->?0 observed that the result of the last one is the result of these two numbers xor. The fact is that the result of the I-bit number is the I-bit XOR or I-1 bit.
So the first bit of the judgment is to take out the I and i-1 bits, and then perform the XOR operation. How do I remove these two? Let the two phase multiply the number equal to 1, the other bit is multiplied by the number equals 0 For example 0101000 to take out 2 3 bit is to multiply with 0110000 we have learned how to remove the I bit and the i-1 bit of two numbers and then how to determine the value of the I bit? In fact, the result of an XOR operation equals the result of adding mod 2 to the number. 0^1= (0+1)%2=1
1^1= (+)%2=0 0^0= (0+0)%2=0 1^0= (1+0)%2=1
So when we take out the I-bit i-1 bit, we can add the result of the first bit to the 2 remainder.
The first example given by the topic if:
Take 1th 2 digits multiplied by the number 1 1 0 0 0 0 0
Take 2nd 3 digits multiplied by the number 0 1 1 0 0 0 0
Take 3rd 4 digits multiplied by the number 0 0 1 1 0 0 0
Take 4th 5 digits multiplied by the number 0 0 0 1 1 0 0
Take 5th 6 digits multiplied by the number 0 0 0 0 1 1 0
Take 6th 7 digits multiplied by the number 0 0 0 0 0 1 1
Take 7th 1 digits multiplied by the number 1 0 0 0 0 0 1
If it is a second after the state of each bit is taken once is multiplied by the above matrix once, 2 seconds after the state is multiplied by the matrix two times .... The state after M seconds is multiplied by M times.
So the fast power exponent that can be transformed into a matrix is M. And then multiplying the matrix with the given string, the result is the state after M seconds.
Note the optimization when the matrix is multiplied. Otherwise time out.
AC Code
#include <stdio.h> #include <string.h> #include <algorithm> #define MAXN 100+5using namespace Std;int Mat[maxn][maxn];int res[maxn][maxn];int n,m;void matmul (int x[maxn][maxn],int y[maxn][maxn],int Mod) {int T[MAXN][MAXN ];memset (t,0,sizeof (t));/* The first one calculates the elements at each location. This calculation cannot be optimized, and will time out for an (int i=0;i<n;i++) for (int j=0;j<n;j++) for (int k=0;k<n;k++) t[i][j]= (t[i][j]+x[i][k]*y[k][ J])%mod;*//* the second line of a row calculates all elements for (int i=0;i<n;i++) for (int k=0;k<n;k++) if (x[i][k])//pruning optimization for (int j=0;j<n;j++) T[i][j]= (T[i][j]+x[i][k]*y[k][j])%mod;*///The third column of one column calculates all elements for the (int j=0;j<n;j++) for (int k=0;k<n;k++) if (y[k][j ])//pruning optimization for (int i=0;i<n;i++) t[i][j]= (T[i][j]+x[i][k]*y[k][j])%mod;for (int i=0;i<n;i++) for (int j=0;j<n;j+ +) x[i][j]=t[i][j];} void Init_res () {for (Int. i=0;i<n;i++) for (int j=0;j<n;j++) res[i][j]= (i==j);} void Matrix (int a[maxn][maxn],int n,int Mod) {init_res (); while (n) {if (n&1) Matmul (RES,A,MOD); Matmul (a,a,mod); n>>=1;}} int main () {char s[110];int Ans[110];whilE (scanf ("%d", &m)!=eof) {memset (mat,0,sizeof (MAT)); memset (res,0,sizeof (res)); scanf ("%s", s); N=strlen (s); for ( int i=0;i<n;i++) ans[i]=s[i]-' 0 '; for (int i=0;i<n-1;i++)//Give initial matrix mat[i][i]=mat[i][i+1]=1;mat[n-1][0]=mat[n-1] [N-1]=1; Matrix (mat,m,2); int temp[110]={0};for (int i=0;i<n;i++) {for (int j=0;j<n;j++) {temp[i]= (temp[i]+ans[j]*res[j][ I])%2;} for (int i=0;i<n;i++) printf ("%d", Temp[i]);p rintf ("\ n");} return 0;}
Copyright NOTICE: This article is the original blogger articles, reproduced please indicate the source.
Hdoj Kiki & Little Kiki 2 2276 "bit operation + matrix fast Power"