hdu 2276 Kiki & Little Kiki 2(矩阵构造乘法)

 

http://acm.hdu.edu.cn/showproblem.php?pid=2276

 

题目大意:有n盏灯,0表示不亮,1表示亮,如果 i-th的灯的左边灯是亮的,那么下一秒钟,i-th灯的状态要改变,0变成1,1变成0。问你在第t秒时,灯的状态时什么样的,输出来。

 

解题思路:

 

a1 = (a1+an)%2,a2 = (a1+a2)%2,a3 = (a2+a3)%2,……an = (an+an-1)%2

 

然后就可以构造出矩阵了,根据上面的等式

 

#include <stdio.h> #include <string.h> #define size 105 /* 例如: 10 100000001 初始表 1 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 1 1 输入表 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 矩阵为 初始表^n * 输入表,每一个元素的值为乘积值mod 2 */ int t,len; char s[size]; struct node { int Graph[size][size]; }unit,init,right; void InitData(int len) { int i,j; for (i=0;i<len;i++) { for (j=0;j<len;j++) { unit.Graph[i][j] = (i==j); init.Graph[i][j] = right.Graph[i][j] = 0; } } init.Graph[0][0] = init.Graph[0][len-1] = 1; for(i=1;i<len;i++) init.Graph[i][i-1] = init.Graph[i][i] = 1; for(i=0;i<len;i++) right.Graph[i][0] = s[i]-'0'; } node Mul(node a,node b) { int i,j,k; node c; for (i=0;i<len;i++) { for (j=0;j<len;j++) { c.Graph[i][j] = 0; for (k=0;k<len;k++) c.Graph[i][j]+=a.Graph[i][k]*b.Graph[k][j]; c.Graph[i][j]%=2; } } return c; } node Cal() { node p,q; p = unit,q = init; while (t) { if(t&1) p = Mul(p,q); t>>=1; q = Mul(q,q); } p = Mul(p,right); return p; } int main() { int i; node res; while (scanf("%d",&t)!=EOF) { scanf("%s",s); len = strlen(s); InitData(len); res = Cal(); for(i=0;i<len;i++) printf("%d",res.Graph[i][0]); printf("/n"); } return 0; }

你可能感兴趣的:(hdu 2276 Kiki & Little Kiki 2(矩阵构造乘法))