Hdu2276Kiki & Little Kiki 2矩阵

ai = (ai-1  + ai) %2 然后构造个 n*n的矩阵A,  时间K之后的状态就是  A^k  *  B(给出的字符串)

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <string.h>
typedef long long LL;
using namespace std;
int n;
struct Matrix
{
    int m[104][105];
};

Matrix Mul(Matrix a, Matrix b)
{
    Matrix  ans;
    for (int i = 0; i < n; i++){
        for (int j = 0; j < n; j++){
            ans.m[i][j] = 0;
            for (int k = 0; k < n; k++){
                ans.m[i][j] += a.m[i][k] * b.m[k][j];
                ans.m[i][j] %= 2;
            }
        }
    }
    return ans;
}

Matrix quick(Matrix a, int b)
{
    Matrix ans;
    for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
        ans.m[i][j] = (i == j);
    while (b){
        if (b & 1){
            ans = Mul(ans, a);
        }
        a = Mul(a, a);
        b >>= 1;
    }
    return ans;
}

Matrix init()
{
    Matrix ans;
    for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
        ans.m[i][j] = 0;
    ans.m[n - 1][0] = 1;
    for (int i = 0; i<n; i++)
        ans.m[i][i] = 1;
    for (int i = 0; i<n - 1; i++)
        ans.m[i][i + 1] = 1;
    return ans;
}

int main()
{
    int k;
    char str[1000];
    int a[1000];
    while (cin >> k){
        scanf("%s", str);
        n = strlen(str);
        Matrix ans = init();
        Matrix  t = quick(ans, k);
        for (int i = 0; i < n; i++)
            a[i] = str[i] - ‘0‘;
        for (int j = 0; j<n; j++){
            int sum = 0;
            for (int k = 0; k<n; k++){
                sum += a[k] * t.m[k][j];
            }
            sum %= 2;
            cout << sum;
        }
        cout << endl;
    }
    return 0;
}
时间: 2025-01-22 11:17:10

Hdu2276Kiki & Little Kiki 2矩阵的相关文章

hdu2276---Kiki &amp; 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'

hdu2276---Kiki &amp;amp; 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'

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 矩阵快速幂

题目大意:给出一个由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(矩阵快速幂)

题目链接 题意:有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 (位运算+矩阵快速幂)

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): 2265    Accepted Submission(s): 1146 Problem Description There are n lights in a circle numbered from 1 to n. The left of li