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: [email protected]
    > 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;
}
时间: 2024-11-01 13:19:53

hdu2276---Kiki & Little Kiki 2(矩阵)的相关文章

HDU 2276 Kiki &amp; Little Kiki 2 (位运算+矩阵快速幂)

HDU 2276 Kiki & Little Kiki 2 (位运算+矩阵快速幂) ACM 题目地址:HDU 2276 Kiki & Little Kiki 2 题意: 一排灯,开关状态已知,每过一秒:第i个灯会根据刚才左边的那个灯的开关情况变化,如果左边是开的,它就会变化,如果是关的,就保持原来状态.问m秒后的状态. 第1个的左边是最后一个. 分析: 转移不好想啊... 变化是这样的: 原来 左边 变化 1 1 0 1 0 1 0 1 1 0 0 0 然后想到 (~原来)^(左边)=变化

HDOJ Kiki &amp; Little Kiki 2 2276【位运算+矩阵快速幂】

Kiki & Little Kiki 2 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2213    Accepted Submission(s): 1137 Problem Description There are n lights in a circle numbered from 1 to n. The left of li

NYOJ 300 &amp;&amp; hdu 2276 Kiki &amp; Little Kiki 2 (矩阵快速幂)

Kiki & Little Kiki 2 时间限制:5000 ms  |  内存限制:65535 KB 难度:4 描述 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 t

hdu 2276 Kiki &amp; 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 li

HDU 2276 Kiki &amp; Little Kiki 2(矩阵快速幂)

Kiki & Little Kiki 2 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2265    Accepted Submission(s): 1146 Problem Description There are n lights in a circle numbered from 1 to n. The left of li

[HDOJ2276]Kiki &amp; Little Kiki 2

Kiki & Little Kiki 2 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2175    Accepted Submission(s): 1110 Problem Description There are n lights in a circle numbered from 1 to n. The left of lig

HDU2276 - Kiki &amp; Little Kiki 2(矩阵快速幂)

题目链接 题意:有n盏灯,编号从1到n.他们绕成一圈,也就是说,1号灯的左边是n号灯.如果在第t秒的时候,某盏灯左边的灯是亮着的,那么就在第t+1秒的时候改变这盏灯的状态.输入m和初始灯的状态.输出m秒后,所有灯的状态. 思路:其实每盏灯的状态之和前一盏和自己有关,所以可以得到一个关系矩阵.假设有6盏灯,因此可以得到关系矩阵如下: (1, 0, 0, 0, 0, 1) (1, 1, 0, 0, 0, 0) (0, 1, 1, 0, 0, 0) (0, 0, 1, 1, 0, 0) (0, 0,

HDU - 2276 Kiki &amp; Little Kiki 2 矩阵快速幂

题目大意:给出一个由0,1组成的字符串,每一秒的时候,如果该位字符左边是1的话,那么该字符就要变换(由0变1,或者由1变0,第一个的左边是最后一个),问M秒后这个字符串的状态 解题思路:用0,1矩阵来表示变化,具体的请看代码,现在没法给出矩阵,后面会补的 #include<cstdio> #include<cstring> const int N = 110; char str[N]; struct Matrix{ int mat[N][N]; }A, B, tmp; int n,

HDU2276——Kiki &amp; Little Kiki 2

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2276 题目意思:给予一个01字符串,表示一串灯的明亮状态,现在每过一秒,如何这个灯的左边的灯是亮的,我们就改变他的明亮状态.(从左往右依次更新)注:第一个的左边是最后一个(即0的左边是n-1),问n秒后所有灯的明亮状态. 思路:采用矩阵快速幂啊!这个题十分有意思,采用了位运算,是真的没有想到. 1 0 0 0 0 0 1 1 1 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 1 0 0

HDU 2276 Kiki &amp; Little Kiki 2

矩阵快速幂. 0 1-> 第二个数字会变成1 0 0-> 第二个数字会变成0 1 0-> 第二个数字会变成1 1 1-> 第二个数字会变成0 根据这四个特点,就可以写转移矩阵了. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<cmath> #include<algori