NYOJ 300 && hdu 2276 Kiki & 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 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)

输入
The input contains no more than 1000 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.

输出
For each data set, output all lights‘ state at m seconds in one line. It only contains character ‘0‘ and ‘1.
样例输入
1
0101111
10
100000001
样例输出
1111000
001000010

题意:给出一些灯的初始状态(用0、1表示),对这些灯进行m次变换;若当前灯的前一盏灯的状态为1,则调整当前灯的状态,0变为1,1变为0;否则不变。第1盏灯的前一盏灯是最后一盏灯。问最后每盏灯的状态。

分析:通过模拟可以发现,假设有n盏灯,第i盏灯的状态为f[i],则f[i] = (f[i] + f[i-1])%2;又因为这些灯形成了环,则f[i] = (f[i] + f[(n+i-2)%n+1])%2. 这样初始状态形成一个1*n的矩阵,然后构造出如下n*n的矩阵:

1  1  0…… 0   0

0  1  1…… 0   0

……………………

1  0  0…… 0   1

每次乘以这个矩阵得出的结果就是下一个状态。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 105;
char state[N];
struct Matrix {
    Matrix() {}
    void Init(int n) {
        row = n; col = n;
        memset(mat, 0, sizeof(mat));
        for(int i = 0; i < n; i++)
            mat[i][i] = 1;
    }
    void Init(int m, int n) {
        row = m; col = n;
        memset(mat, 0, sizeof(mat));
    }
    int row, col;
    int mat[N][N];
    const Matrix &Pow(int n);
};

const Matrix &operator *(const Matrix &A, const Matrix &B) {
    Matrix res;
    res.Init(A.row, B.col);
    for(int i = 0; i < A.row; i++) {
        for(int j = 0; j < A.col; j++) {
            if(A.mat[i][j]) {
                for(int k = 0; k < B.col; k++) {
                    if(B.mat[j][k])
                        res.mat[i][k] = res.mat[i][k] ^ (A.mat[i][j] & B.mat[j][k]);
                }
            }

        }
    }
    return res;
}

const Matrix &Matrix::Pow(int n) {
    Matrix tmp, pre;
    tmp = *this;
    pre.Init(this->row);
    while(n) {
        if(n&1) pre = tmp * pre;
        tmp = tmp * tmp;
        n >>= 1;
    }
    return pre;
}

int main() {
    int m;
    Matrix A, B, ans;
    while(~scanf("%d", &m)) {
        scanf("%s",state);
        int len = strlen(state);
        A.Init(1, len);
        for(int i = 0; i < len; i++)
            A.mat[0][i] = state[i] - '0';
        B.Init(len);
        for(int i = 0; i < len; i++) {
            B.mat[i][i] = 1;
            B.mat[i][(i+1)%len] = 1;
        }
        ans = A * B.Pow(m);
        for(int i = 0; i < len; i++)
            printf("%d", ans.mat[0][i]);
        printf("\n");
    }
    return 0;
}
时间: 2024-08-07 08:34:50

NYOJ 300 && hdu 2276 Kiki & Little Kiki 2 (矩阵快速幂)的相关文章

HDU 5171 GTY&#39;s birthday gift 矩阵快速幂

点击打开链接 GTY's birthday gift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 225    Accepted Submission(s): 78 Problem Description FFZ's birthday is coming. GTY wants to give a gift to ZZF. He as

HDU 4965 Fast Matrix Calculation(矩阵快速幂)

HDU 4965 Fast Matrix Calculation 题目链接 矩阵相乘为AxBxAxB...乘nn次,可以变成Ax(BxAxBxA...)xB,中间乘n n - 1次,这样中间的矩阵一个只有6x6,就可以用矩阵快速幂搞了 代码: #include <cstdio> #include <cstring> const int N = 1005; const int M = 10; int n, m; int A[N][M], B[M][N], C[M][M], CC[N]

HDU 2256 Problem of Precision (矩阵快速幂)

HDU 2256 Problem of Precision (矩阵快速幂) ACM 题目地址:HDU 2256 Problem of Precision 题意: 给出一个式子,求值. 分析: 推起来最后那步会比较难想. 具体过程见: 表示共轭只听说过复数的和图的... 这构题痕迹好明显... 跟基友开玩笑说:如果遇到这种题,推到Xn+Yn*sqrt(6)这步时,打表最多只能打到10就爆int了,这是输出正解和Xn,说不定眼神好能发现ans = Xn * 2 - 1呢.= =... 代码: /*

hdu 4965 Fast Matrix Calculation(矩阵快速幂)2014多校训练第9场

Fast Matrix Calculation                                                                   Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem Description One day, Alice and Bob felt bored again, Bob knows Ali

hdu 1757 A Simple Math Problem 矩阵快速幂

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757 Lele now is thinking about a simple function f(x).If x < 10 f(x) = x.If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10);And ai(0<=i<=9) can only be 0 or 1 .Now, I w

HDU 2256 Problem of Precision(矩阵快速幂)+ HDU 4565

HDU 2256 题意: 计算?(2√+3√)2n?mod1024 思路: ∵f(n)=(2√+3√)2n=(5+26√)n=An+Bn?6√ ∴f(n?1)=An?1+Bn?1?6√ 又∵f(n)=(5+26√)?f(n?1) ∴f(n)=(5?An?1+12?Bn?1)+(2?An?1+5?Bn?1)?6√ 所以递推矩阵就是: (52125)?(An?1Bn?1)=(AnBn) A1=5,B1=2. 然后套矩阵快速幂模板即可求出An,Bn. 又∵(5+26√)n=An+Bn?6√ ∴(5?2

hdu 4565 So Easy! (共轭构造+矩阵快速幂)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4565 题目大意: 给出a,b,n,m,求出的值, 解题思路: 因为题目中出现了开根号,和向上取整后求余,所以用矩阵快速幂加速求解过程的时候,会产生误差,就很自然地想到了凑数,因为(a-1)^2<b<a^2,得出0<a-sqrt(b)<1,则无论n取多大,(a-sqrt(b))^n都是小于1的,(a-sqrt(b))^n 与 (a+sqrt(b))^n共轭,两者展开后会相互抵销,所以(

HDU 3292 【佩尔方程求解 &amp;&amp; 矩阵快速幂】

任意门:http://acm.hdu.edu.cn/showproblem.php?pid=3292 No more tricks, Mr Nanguo Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 587    Accepted Submission(s): 400 Problem Description Now Sailormoon

HDU 4990 Reading comprehension (找规律+矩阵快速幂)

题目链接:HDU 4990 Reading comprehension 题目给的一个程序其实就是一个公式:当N=1时 f[n]=1,当n>1时,n为奇数f[n]=2*f[n-1]+1,n为偶数f[n]=2*f[n-1]. 先不取模,计算前十个找规律.得到一个递推公式:f[n]=2*f[n-2]+f[n-1]+1 然后快速幂解决之. 给出一个神奇的网站(找数列通项):http://oeis.org/ AC代码: #include<stdio.h> #include<string.h&