zoukankan      html  css  js  c++  java
  • hdu 2276 Kiki & Little Kiki 2 矩阵快速幂

    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 !!!

    只有当第i个左边的灯是亮的(第0个的左边是最后一个,它是一个环),第i个就改变状态

    d(t,i) = ( d(t-1,i) + d(t-1,i-1) )%2

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    
    typedef long long ll;
    const int Mod = 2;
    const int N = 105;
    int msize;
    
    struct Mat
    {
        int mat[N][N];
    };
    
    Mat operator *(Mat a, Mat b)
    {
        Mat c;
        memset(c.mat, 0, sizeof(c.mat));
        for(int k = 0; k < msize; ++k)
            for(int i = 0; i < msize; ++i)
                if(a.mat[i][k])
                    for(int j = 0; j < msize; ++j)
                        if(b.mat[k][j])
                            c.mat[i][j] = (a.mat[i][k] * b.mat[k][j] + c.mat[i][j])%Mod;
        return c;
    }
    
    Mat operator ^(Mat a, int k)
    {
        Mat c;
        memset(c.mat,0,sizeof(c.mat));
        for(int i = 0; i < msize; ++i)
            c.mat[i][i]=1;
        for(; k; k >>= 1)
        {
            if(k&1) c = c*a;
            a = a*a;
        }
        return c;
    }
    
    char s[N];
    
    int main()
    {
    //    freopen("in.txt", "r", stdin);
        int m;
        while(scanf("%d", &m) == 1)
        {
            scanf("%s", s);
            msize = strlen(s);
            Mat A;
            memset(A.mat, 0, sizeof(A.mat));
            for(int i = 0; i < msize; i++)
                A.mat[i][i] = A.mat[i][(i + msize - 1) % msize] = 1;
            A = A^m;
            for(int i = 0; i < msize; i++)
            {
                int ans = 0;
                for(int j = 0; j < msize; j++)
                    ans = (ans + A.mat[i][j]*(s[j]-'0'))%Mod;
                printf("%d", ans);
            }
            puts("");
        }
        return 0;
    }
  • 相关阅读:
    集合 Subset Sums
    resin config 中文(resin.xml)
    resin4 简单学习
    什么是敏捷软件测试
    10个热门IT证书
    LoadRunner监控Linux服务器
    Agile 敏捷开发
    戴明PDCA方法
    【转】什么是内存泄露? 内存泄露检测工具
    LoadRunner常见问题
  • 原文地址:https://www.cnblogs.com/pach/p/7266674.html
Copyright © 2011-2022 走看看