HDU 2276

http://acm.hdu.edu.cn/showproblem.php?pid=2276

矩阵乘法可以解决的一类灯泡开关问题

/*
转移关系为
now left now*
1    0    1
1    1    0
0    1    1
0    0    0

可知F[i] = (f[i] + f[(n+i-2)%n+1]) % 2 

得到n*n的关系矩阵是
1 1 0 0 ... 0
0 1 1 0 ... 0
0 0 1 1 ... 0
. . . . ... 0
. . . . ... 0
. . . . ... 0
0 0 0 0 ... 1
1 0 0 0 ... 1
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>

using namespace std;

#define MOD 2

#define Mat 105 //矩阵大小

struct mat{//矩阵结构体,a表示内容,r行c列 矩阵从1开始
    int a[Mat][Mat];
    int r, c;
    mat() {
        r = c = 0;
        memset(a, 0, sizeof(a));
    }
};  

void print(mat m) {
    //printf("%d\n", m.size);
    for(int i = 0; i < m.r; i++) {
        for(int j = 0; j < m.c; j++) printf("%d ", m.a[i][j]);
        putchar(‘\n‘);
    }
}  

mat mul(mat m1, mat m2, int mod) {
    mat ans  = mat();
    ans.r = m1.r, ans.c = m2.c;
    for(int i = 1; i <= m1.r; i++)
        for(int j = 1; j <= m2.r; j++)
            if(m1.a[i][j])
                for(int k = 1; k <= m2.c; k++)
                    ans.a[i][k] = (ans.a[i][k] + m1.a[i][j] * m2.a[j][k]) % mod;
    return ans;
} 

mat quickmul(mat m, int n, int mod) {
    mat ans = mat();
    for(int i = 1; i <= m.r; i++) ans.a[i][i] = 1;
    ans.r = m.r, ans.c = m.c;
    while(n) {
        if(n & 1) ans = mul(m, ans, mod);
        m = mul(m, m, mod);
        n >>= 1;
    }
    return ans;
}  

/*
初始化ans矩阵
mat ans = mat();
ans.r = R, ans.c = C;
ans = quickmul(ans, n, mod);
*/

char a[105];

int main() {
    int m;
    while(~scanf("%d", &m)) {
        scanf("%s", a);
        int n = strlen(a);
        mat A = mat();
        A.r = 1, A.c = n;
        for(int i = 1; i <= n; i++)
            A.a[1][i] = a[i-1] - ‘0‘;
        mat G = mat();
        G.r = G.c = n;
        for(int i = 1; i < n; i++) {
            G.a[i][i] = G.a[i][i+1] = 1;
        }
        G.a[n][1] = G.a[n][n] = 1;
        mat ans = quickmul(G, m, MOD);
        ans = mul(A, ans, MOD);
        for(int i = 1; i <= n; i++)
            printf("%d", ans.a[1][i]);
        putchar(‘\n‘);
    }
    return 0;
}

时间: 2024-11-06 11:10:20

HDU 2276的相关文章

HDU 2276 &amp; FZU 1692 (循环同构优化+矩阵快速幂)

HDU 2276 题意: 给定一个01**字符串环**(2<=字符串长度<=100)然后进行m次的变换. 定义变换的规则为:如果当前位置i的左边是1(下标为0的左边是n-1),那么i就要改变状态0->1 , 1->0 比如当前的状态为100101那么一秒过后的状态为010111. 思路: 用公式表示变化状态其实就是: ai=(a(i+n?1)%n+ai)%2 换成位操作就是: ai=a(i+n?1)%n^ ai 于是我们可以建立一个变化矩阵: ??????????1100...00

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 然后想到 (~原来)^(左边)=变化

HDU 2276 矩阵快速幂

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

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

hdu 2276(矩阵快速幂)

Kiki & Little Kiki 2 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2301    Accepted Submission(s): 1176 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,

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

HDU - 2276 Kiki &amp;amp; Little Kiki 2

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, tu

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