HDU 2276 Kiki & Little Kiki 2(矩阵快速幂)

来源:互联网 发布:牙签弩淘宝多少钱 编辑:程序博客网 时间:2024/04/30 06:16

Kiki & Little Kiki 2

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


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 
 

#pragma comment(linker, "/STACK:1024000000,1024000000")#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<queue>#include<stack>#include<vector>#include<set>#include<map>#define L(x) (x<<1)#define R(x) (x<<1|1)#define MID(x,y) ((x+y)>>1)#define bug printf("hihi\n")#define eps 1e-8typedef __int64 ll;using namespace std;#define INF 0x3f3f3f3f#define N 101int n;struct mat{   mat(){memset(a,0,sizeof(a));}   int a[N][N];   mat operator *(mat b)   {       mat c;       for(int i=0;i<n;i++)          for(int j=0;j<n;j++)             for(int k=0;k<n;k++)                 c.a[i][j]=(c.a[i][j]+a[i][k]*b.a[k][j])%2;       return c;   }};mat fdd(mat s,int m){    mat ss;    for(int i=0;i<n;i++) ss.a[i][i]=1;    while(m)    {        if(m&1) ss=ss*s;        s=s*s;        m>>=1;    }    return ss;}int main(){    int i,j;    char c[1000];    int m;    while(~scanf("%d",&m))    {        scanf("%s",c);        mat s;        n=strlen(c);        for(i=0;i<n;i++)            s.a[i][0]=c[i]-'0';        mat ss;        ss.a[0][0]=ss.a[0][n-1]=1;        for(i=1;i<n;i++)            ss.a[i][i]=ss.a[i][i-1]=1;        ss=fdd(ss,m);        s=ss*s;        for(i=0;i<n;i++)            printf("%d",s.a[i][0]);      printf("\n");    }    return 0;}






0 0
原创粉丝点击