您的位置:首页 > 其它

hdu 2276 Kiki & Little Kiki 2矩阵快速幂

2015-08-26 16:29 471 查看


Kiki & Little Kiki 2

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2300 Accepted Submission(s): 1175



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




题意:告诉一个01序列表示灯的开关情况,0就是关,1就是开,现在要操作次,如果一个数左边是1就改变这个数的状态,第一个数左边的数是最右边那个数。

分析:最开始第一反应是找循环节,但是直接存起来会MLE,找到后再算会TLE,赛后才知道是举证快速幂。对于每个数,操作时只与当前以及它左边的数有关,跟其他数没有任何关系,于是构造矩阵时,就能想到只有当前位置和它左边位置有关,只有这两个位置的数是1,其他都是0。

#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <cmath>
typedef __int64 ll;
using namespace std;

struct matrix
{
    int f[102][102];
}s,t;

char str[109];
int mp[109][109];
int ss[109];

matrix mul(matrix a,matrix b,int len)
{
    matrix c;
    memset(c.f,0,sizeof c.f);

    for(int i=0;i<len;i++)
    {
        for(int j=0;j<len;j++)
        {
            for(int k=0;k<len;k++)
            {
                c.f[i][j]=(c.f[i][j]+a.f[i][k]*b.f[k][j])%2;
            }
        }
    }
    return c;
}

matrix fun(matrix a,ll n,int len)
{
    matrix b;

    for(int i=0;i<len;i++)
        for(int j=0;j<len;j++)
        if(i==j) b.f[i][j]=1;
         else b.f[i][j]=0;

         while(n)
         {
             if(n&1)
                b=mul(b,a,len);
             a=mul(a,a,len);
             n>>=1;
         }
         return b;
}

int main()
{
    ll n;
    while(~scanf("%I64d",&n))
    {
        scanf("%s",str);
        int len=strlen(str);

        memset(s.f,0,sizeof s.f);
        for(int i=0;i<len;i++)//构造矩阵
            if(i==0 || i==len-1) s.f[i][0]=1;
        for(int j=1;j<len;j++)
            for(int i=0;i<len;i++)
                if(i==j || i==j-1) s.f[i][j]=1;

        for(int i=0;i<len;i++)
            ss[i]=str[i]-'0';

            t=fun(s, n ,len);

            for(int i=0;i<len;i++)
            {
                int ans=0;
                for(int j=0;j<len;j++)
                {
                    ans=(ans+ss[j]*t.f[j][i])%2;
                }
                printf("%d",ans);
            }
            puts("");

    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: