HDU 2276解题报告

13 篇文章 0 订阅

Kiki & Little Kiki 2

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


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
 

Source
 

Recommend
lcy

       这道题是典型的dp+矩阵快速幂优化。状态转移很明显。如果一个灯左边的灯开着,就改变该灯的状态。那么这时我们就应该对只有0,1两种状态时的情况有所警觉,在这种想法之下把0可以变成1,把1可以变成0,这就要用到对2取模。

          考虑a(i,j)到a(i+1,j)的状态转移,假设a(i,j-1)表示a(i,j)的左边。当然j=1时它的左边为a(i,n),这个与其他的不同。

       则可以得到a(i+1,j)=(a(i,j)+a(i,j-1))%2。有了该式,直接矩阵快速幂即可。

      参考代码:

      

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<ctime>
#include<cstdlib>
#include<iomanip>
#include<utility>
#define pb push_back
#define mp make_pair
#define CLR(x) memset(x,0,sizeof(x))
#define _CLR(x) memset(x,-1,sizeof(x))
#define REP(i,n) for(int i=0;i<n;i++)
#define Debug(x) cout<<#x<<"="<<x<<" "<<endl
#define REP(i,l,r) for(int i=l;i<=r;i++)
#define rep(i,l,r) for(int i=l;i<r;i++)
#define RREP(i,l,r) for(int i=l;i>=r;i--)
#define rrep(i,l,r) for(int i=1;i>r;i--)
#define read(x) scanf("%d",&x)
#define put(x) printf("%d\n",x)
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<11
using namespace std;
int n,k;
char a[110];
struct mat
{
    int d[101][101];
}A,B,E;
mat multi(mat a,mat b)
{
    mat ans;
    rep(i,0,k)
    {
        rep(j,0,k)
        {
            ans.d[i][j]=0;
            rep(l,0,k)
               ans.d[i][j]=(ans.d[i][j]+a.d[i][l]*b.d[l][j])%2;
        }
    }
    return ans;
}

mat quickmulti(int n)
{
    if(n==0) return E;
    if(n==1) return A;
    mat p=E,q=A;
    while(n)
    {
        if(n&1)
        {
            n--;
            p=multi(p,q);
        }
        else
        {
            n>>=1;
            q=multi(q,q);
        }
    }
    return p;
}

int main()
{
   CLR(E.d);
   rep(i,0,100)
      E.d[i][i]=1;
   while(~read(n))
   {
       scanf(" %s",a);
       CLR(A.d);CLR(B.d);
       k=strlen(a);
       rep(i,0,k)
           B.d[i][0]=a[i]-'0';
       A.d[0][0]=A.d[0][k-1]=1;
       rep(i,1,k)
           A.d[i][i]=A.d[i][i-1]=1;
       mat ans=quickmulti(n);
       ans=multi(ans,B);
       rep(i,0,k)
          printf("%d",ans.d[i][0]);
       printf("\n");
   }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值