hdu 2276 Kiki & Little Kiki 2 矩阵快速幂

题目链接

 

n个灯围成一圈, 1左边是n。 有两种状态, 1是亮, 0是不亮。 如果一个灯, 它左边的灯是亮的, 那么下一时刻这个灯就要改变状态, 1变为0, 0变为1。 给出初始状态和时间t, 问t时刻每个灯的状态是什么。

 

ai = (a(i-1)+ai)%2, 根据这个构建矩阵。

/*
1 0 0 0 0 0 0 0 1 
1 1 0 0 0 0 0 0 0 
0 1 1 0 0 0 0 0 0 
0 0 1 1 0 0 0 0 0 
0 0 0 1 1 0 0 0 0 
0 0 0 0 1 1 0 0 0 
0 0 0 0 0 1 1 0 0 
0 0 0 0 0 0 1 1 0 
0 0 0 0 0 0 0 1 1 
*/
// 对这个矩阵进行快速幂, 结果与初始状态相乘就好。
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int mod = 1e9+7;
const int inf = 1061109567;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
int n;
struct Matrix
{
    int a[102][102];
    Matrix() {
        mem(a);
    }
};
Matrix operator * (Matrix a, Matrix b) {
    Matrix c;
    for(int i = 0; i<n; i++) {
        for(int j = 0; j<n; j++) {
            for(int k = 0; k<n; k++) {
                c.a[i][j] += a.a[i][k]*b.a[k][j];
                c.a[i][j] %= 2;
            }
        }
    }
    return c;
}
Matrix operator ^ (Matrix a, ll b) {
    Matrix tmp;
    for(int i = 0; i<n; i++)
        tmp.a[i][i] = 1;
    while(b) {
        if(b&1)
            tmp = tmp*a;
        a = a*a;
        b>>=1;
    }
    return tmp;
}
int main()
{
    int m;
    string s;
    while(cin>>m) {
        cin>>s;
        n = s.size();
        Matrix tmp;
        for(int i = 0; i<n; i++) {
            tmp.a[i][i] = 1;
            tmp.a[i][(i-1+n)%n] = 1;
        }
        Matrix b = tmp^m;
        Matrix a;
        for(int i = 0; i<s.size(); i++) {
            a.a[i][0] = s[i]-'0';
        }
        Matrix c = b*a;
        for(int i = 0; i<s.size(); i++) {
            printf("%d", c.a[i][0]);
        }
        cout<<endl;
    }
    return 0;
}

 

posted on 2016-03-12 09:37  yohaha  阅读(244)  评论(0编辑  收藏  举报

导航