HDOJ Kiki & Little Kiki 2 2276【位运算+矩阵快速幂】

来源:互联网 发布:linux删除文件夹还存在 编辑:程序博客网 时间:2024/06/07 04:59

Kiki & Little Kiki 2

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2213    Accepted Submission(s): 1137


Problem 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
 

Source
HDU 8th Programming Contest Site(1)
 

Recommend
lcy   |   We have carefully selected several similar problems for you:  1757 1588 2604 2256 2294 
 

题意:有n个0-1字符串组成的环,如果一个数字左边是1那么就将此数字的状态改变。第一个数字的左边是第n个数字。问第m秒后的状态字符串

观察: 最后一个数字变化   01 -->?1      10-->?1     11-->?0      00-->?0   观察到最后一位的结果是 这两个数字异或的结果。  事实是第i位数字的结果就是  第i位异或i-1位。

所以判断第i位就是取出第i位和i-1位,然后进行异或运算。如何取出这两位?让与这两位相乘的数字等于1 ,其他位上相乘的数字等于0   例如0101000取出2 3位  就是与0110000 相乘  我们已经学会如何取出i位和i-1位上的两个数字   然后如何判断第i位的值呢?  其实异或的运算结果就等于数字相加mod 2 的结果。0^1=(0+1)%2=1  

1^1=(1+1)%2=0   0^0=(0+0)%2=0    1^0=(1+0)%2=1

所以当我们取出i位i-1位之后可以进行相加对2取余 得到第i位的结果。

题目给的第一个样例如果:

取第1 2位则相乘的数字 1 1 0 0 0 0 0

取第2 3位则相乘的数字 0 1 1 0 0 0 0

取第3 4位则相乘的数字 0 0 1 1 0 0 0

取第4 5位则相乘的数字 0 0 0 1 1 0 0

取第5 6位则相乘的数字 0 0 0 0 1 1 0

取第6 7位则相乘的数字 0 0 0 0 0 1 1

取第7 1位则相乘的数字 1 0 0 0 0 0 1

如果是一秒后的状态就每一位都取一次  就是乘以上述矩阵一次,2秒后的状态就是乘以矩阵两次。。。。。m秒后的状态就是乘以m次。

所以可以转化成矩阵的快速幂  指数为m。然后用给定的字符串再去乘以该矩阵  得到的结果就是m秒后的状态  

注意矩阵相乘时的优化。否则超时。

AC代码

#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));/*第一种 计算每一个位置上的元素。 //这样计算没法优化,会超时for(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;*//*第二种 一行一行的计算出所有元素 for(int i=0;i<n;i++)for(int k=0;k<n;k++)if(x[i][k])  //剪枝优化 for(int j=0;j<n;j++)t[i][j]=(t[i][j]+x[i][k]*y[k][j])%Mod;*///第三种 一列一列的计算出所有元素for(int j=0;j<n;j++)for(int k=0;k<n;k++)if(y[k][j])  //剪枝优化 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++) //给出初始矩阵 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]);printf("\n");}return 0;}


0 0