HDU 2276 Kiki & Little Kiki 2

来源:互联网 发布:小程序系统源码 编辑:程序博客网 时间:2024/06/07 00:08

Kiki & Little Kiki 2

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


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

题意:操作n次,每次操作将为1的灯的右边灯反转,求最后灯的状态
每次的结果取决于左边,可以简单列举几个,1 1 第二个灯的状态会变成0, 1 0 第二个灯的状态会变成1,0 0 地二个灯的状态还是0 ,从矩阵上考虑可以构造一个n*n的矩阵,对角线和对角线前一位为1,其余均为0,运算上如果每次%2效率会很低,只有0 1 两种情况就可以特殊处理了,可以把+和*的运算换位^和&的运算,因为矩阵每次都能从上一行经过变化得到,所以通过这一点能将复杂度从n^3降到n^
需要构造的矩阵为:

1 0 0 0 0 0 ......   1                 a[0]
1 1 0 0 0 0 ......   0                 a[1]
0 1 1 0 0 0 ......   0                 a[2]
0 0 0 1 1 0 ......   0        X      a[3]
   ...................                        ....
0 0 0 0 0 0 ..... 1 0                 a[n-1]

#pragma comment(linker,"/STACK:1024000000,1024000000")#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<string>#include<stack>#include<queue>#include<deque>#include<set>#include<map>#include<cmath>#include<vector>using namespace std;typedef long long ll;typedef unsigned long long ull;typedef pair<int, int> PII;#define pi acos(-1.0)#define eps 1e-10#define pf printf#define sf scanf#define lson rt<<1,l,m#define rson rt<<1|1,m+1,r#define e tree[rt]#define _s second#define _f first#define all(x) (x).begin,(x).end#define mem(i,a) memset(i,a,sizeof i)#define for0(i,a) for(int (i)=0;(i)<(a);(i)++)#define for1(i,a) for(int (i)=1;(i)<=(a);(i)++)#define mi ((l+r)>>1)#define sqr(x) ((x)*(x))const int inf=0x3f3f3f3f;char g[110];int n,a[110],ans[110][110],p[110][110],len;void multi(int a[][110],int b[][110]){    int tmp[110][110];    mem(tmp,0);    for0(i,len)        for0(j,len)            tmp[0][i]^=a[0][j]&b[j][i];    for(int i=1;i<len;i++)        for0(j,len)            a[i][j]=tmp[i][j]=tmp[i-1][(j+len-1)%len];//每一行的第n个可根据前一个递推得到    for0(i,len)a[0][i]=tmp[0][i];}void init()//矩阵初始化{    mem(ans,0);    for0(i,len)ans[i][i]=1;    mem(p,0);    for0(i,len)p[i][i]=p[i][(i+len-1)%len]=1;}void solve()//快速幂{    while(n)    {        if(n&1)multi(ans,p);        n>>=1;        multi(p,p);    }}int main(){    while(~sf("%d",&n))    {        sf("%s",g);        len=strlen(g);        for0(i,len)a[i]=g[i]-'0';        init();        solve();        for0(i,len)        {            int o=0;            for0(j,len)                o^=ans[i][j]&a[j];//计算每一个的情况            pf(i==len-1?"%d\n":"%d",o);        }    }    return 0;}


原创粉丝点击