acm-Kiki & Little Kiki 2

来源:互联网 发布:火炬之光2for mac汉化 编辑:程序博客网 时间:2024/04/26 08:25

Kiki & Little Kiki 2

时间限制:5000 ms  内存限制:65535 KB
难度:4
描述
There are n lights in a circle numbered from 1 to n. The left oflight 1 is light n, and the left of light k (1< k<= n) is thelight k-1.At time of 0, some of them turn on, and others turnoff. 
Change the state of light i (if it's on, turn off it; if it is noton, turn on it) at t+1 second (t >= 0), if the left of light iis on !!! Given the initiation state, please find all lights’ stateafter M second. (2<= n <= 100, 1<= M<=10^8)
输入
The input contains no more than 1000 data sets. The first line ofeach data set is an integer m indicate the time, the second linewill be a string T, only contains '0' and '1' , and its length nwill not exceed 100. It means all lights in the circle from 1 ton.
If the ith character of T is '1', it means the light i is on,otherwise the light is off.
输出
For each data set, output all lights' state at m seconds in oneline. It only contains character '0' and '1.
样例输入
1010111110100000001
样例输出
1111000001000010
来源
hdu
 
#include
#include
#include
#include
using namespace std;
const int MAXN = 105;
int n , len;
char str[MAXN];
struct Matrix{
    intmat[MAXN][MAXN];
    Matrixoperator*(const Matrix& m)const{
       Matrix tmp;
       for(int i = 0 ; i < len ; i++){
           for(int j = 0 ; j < len ; j++){
               tmp.mat[i][j] = 0;  
               for(int k = 0 ; k < len ; k++)
                   tmp.mat[i][j] ^= (mat[i][k]&m.mat[k][j]);
           }
       }
       return tmp;
    }
};
void solve(){
    len =strlen(str);
    Matrix m ,ans;
    memset(m.mat, 0 , sizeof(m.mat));
    for(int i =1 ; i < len ; i++)
       m.mat[i][i] = m.mat[i][i-1] = 1;
    m.mat[0][0]= m.mat[0][len-1] = 1;
   memset(ans.mat , 0 , sizeof(ans.mat));
    for(int i =0 ; i < len ; i++)
       ans.mat[i][i] = 1;
   while(n){
       if(n&1)
           ans = ans*m;
       n >>= 1;
       m = m*m;
    }
    for(int i =0 ; i < len ; i++){
       int x = 0;
       for(int k = 0 ; k < len ; k++)
           x ^= ans.mat[i][k]&(str[k]-'0');
       printf("%d" , x);
    }
   puts("");
}
int main(){
   while(scanf("%d%s" , &n , str) != EOF)
       solve();
    return0;
}