hdu2276---Kiki & Little Kiki 2(矩阵)

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
HDU 8th Programming Contest Site(1)

Recommend
lcy | We have carefully selected several similar problems for you: 2256 2254 3117 2855 2971

每个灯的状态都由它左边那一个决定
构造出一个n*n的系数矩阵,每一行,每一列相应位置为1,
比方对于3个数的系数矩阵
1 1 0
0 1 1
1 0 1
那么初始为110的状态,1s以后即为101
也就是[1 1 0] 去乘上面那个矩阵得到的新矩阵
那么仅仅要矩阵高速幂加速即可了

/*************************************************************************
    > File Name: hdu2276.cpp
    > Author: ALex
    > Mail: zchao1995@gmail.com 
    > Created Time: 2015年03月12日 星期四 21时28分55秒
 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

int n;

class MARTIX
{
    public:
        int mat[110][110];
        MARTIX();
        MARTIX operator * (const MARTIX &b)const;
        MARTIX& operator = (const MARTIX &b);
};

MARTIX :: MARTIX()
{
    memset (mat, 0, sizeof(mat));
}

MARTIX MARTIX :: operator * (const MARTIX &b)const
{
    MARTIX ret;
    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j < n; ++j)
        {
            for (int k = 0; k < n; ++k)
            {
                ret.mat[i][j] += (this -> mat[i][k] * b.mat[k][j]);
                ret.mat[i][j] %= 2;
            }
        }
    }
    return ret;
}


MARTIX& MARTIX :: operator = (const MARTIX &b)
{
    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j < n; ++j)
        {
            this -> mat[i][j] = b.mat[i][j];
        }
    }
    return *this;
}

MARTIX fastpow(MARTIX A, int m)
{
//  void Debug(MARTIX A);

    MARTIX ret;
    for (int i = 0; i < n; ++i)
    {
        ret.mat[i][i] = 1;
    }
    while (m)
    {
        if (m & 1)
        {
            ret = ret * A;
        }
        m >>= 1;
        A = A * A;
    }
    return ret;
}

void Debug(MARTIX A)
{
    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j < n; ++j)
        {
            printf("%d ", A.mat[i][j]);
        }
        printf("\n");
    }
}

char str[110];

int main ()
{
    int m;
    while (~scanf("%d", &m))
    {
        scanf("%s", str);
        MARTIX F;
        n = strlen(str);
        MARTIX A;
        for (int i = 0; i < n - 1; ++i)
        {
            A.mat[i][i] = A.mat[i][i + 1] = 1;
        }
        A.mat[n - 1][0] = A.mat[n - 1][n - 1] = 1;
//      Debug(A);
        A = fastpow(A, m);
        for (int i = 0; i < n; ++i)
        {
            F.mat[0][i] = str[i] - '0';
        }
        F = F * A;
        for (int i = 0; i < n; ++i)
        {
            printf("%d", F.mat[0][i]);
        }
        printf("\n");
    }
    return 0;
}
posted on 2016-02-24 16:42  gcczhongduan  阅读(119)  评论(0编辑  收藏  举报