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

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;
}








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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值