HDU 2276(数论,构造二分矩阵)

Kiki & Little Kiki 2

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

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
   
   
   
   
1 0101111 10 100000001
 

 

Sample Output
   
   
   
   
1111000 001000010

 

/************************************************************************ a1=(a1+an)%2; a2=(a1+a2)%2; a3=(a2+a3)%/2.......an=(an-1+an)%2. 构造一个二分矩阵:(拿第一次测试数据举例) martix[7][7] 1 1 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 使每一列对应每个元素ai前一项和本身 m次就对这个矩阵进行m次方,用到快速幂 最后要注意,每个元素都可能会每个位置产生影响,所以 最后将得到的这个矩阵的每i列乘以每个初始状态的元素,就是最终的第i个元素 ************************************************************************/ #include <iostream> using namespace std; #define N 101 struct Mat { int martix[N][N]; }; Mat res,tp,q,tp1; int n,m; int elem[N]; Mat Martix_Mul(Mat a,Mat b) { Mat c; int i,j,k; for (i=0;i<n;i++) { for(j=0;j<n;j++) { c.martix[i][j]=0; for (k=0;k<n;k++) { c.martix[i][j]+=(a.martix[i][k]*b.martix[k][j]); c.martix[i][j]&=1; } } } return c; } void er_fun(int x) { tp1=tp; tp=res; while (x) { if(x&1) tp=Martix_Mul(tp,tp1); tp1=Martix_Mul(tp1,tp1); x>>=1; } } void Slove() { int i,j; int res; for(i=0;i<n;i++) { res=0; for (j=0;j<n;j++) { res+=elem[j]*tp.martix[j][i]; } res&=1; printf("%d",res); } printf("/n"); } int main() { char str[N]; int i; while(scanf("%d%*c",&m)!=EOF) { scanf("%s",str); n=strlen(str); for(i=0;i<n;i++) elem[i]=str[i]-'0'; memset(res.martix,0,sizeof(res.martix)); memset(tp.martix,0,sizeof(tp.martix)); for(i=0;i<n;i++) { res.martix[i][i]=1; } tp.martix[0][0]=tp.martix[n-1][0]=1; for(i=1;i<n;i++) { tp.martix[i-1][i]=tp.martix[i][i]=1; } er_fun(m); Slove(); } return 0; }

你可能感兴趣的:(HDU 2276(数论,构造二分矩阵))