hdu 2276Kiki & Little Kiki 2

Kiki & Little Kiki 2

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 9   Accepted Submission(s) : 9
Font: Times New Roman | Verdana | Georgia
Font Size: ← →

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

 

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

00-->0,01-->1,10-->1,11-->0;

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

然后就是关键构造矩阵了

/* 
例如: 
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  ,
*/ 

这两天也做了几道矩阵的题目了,要找到点感觉了。。

<code>如下:
#include<stdio.h>
#include<string.h>
#define size 105
int t,len;
char s[size];
struct Matr
{
       int map[size][size];
}unit,init,right;
//unit单位矩阵,init构造的转移矩阵,right右矩阵
void Initdata(int n)
{
     int i,j;
     for(i=0;i<n;i++)
         for(j=0;j<n;j++)
         {
             unit.map[i][j]=(i==j);//单位矩阵
             init.map[i][j]=right.map[i][j]=0;
         }
     init.map[0][0]=init.map[0][len-1]=1;
     for(i=1;i<n;i++)
     init.map[i][i-1]=init.map[i][i]=1;
     for(i=0;i<n;i++)
     right.map[i][0]=s[i]-'0';
}
Matr Mult(Matr a,Matr b)//两个矩阵的乘积
{
     int i,j,k;
     Matr c;
     for(i=0;i<len;i++)
         for(j=0;j<len;j++)
         {
            c.map[i][j]=0;
            for(k=0;k<len;k++)
            c.map[i][j]+=a.map[i][k]*b.map[k][j];
            c.map[i][j]%=2;
         }
     return c;
}

Matr Solve()
{
     Matr p,q;
     p=unit,q=init;
     while(t)//算矩阵unit的t次方
     {
          if(t&1)p=Mult(p,q);
          //if(t%2)p=Mult(p,q);
          t>>=1;
          q=Mult(q,q);
     }
     p=Mult(p,right);
     return p;
}

int main()
{
    Matr ans;
    while(scanf("%d",&t)!=EOF)
    {
         scanf("%s",s);
         len=strlen(s);
         Initdata(len);
         ans=Solve();
         for(int i=0;i<len;i++)
         printf("%d",ans.map[i][0]);
         printf("/n");
    }
    return 0;
}
        

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值