• 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;
    }
  • 相关阅读:
    27 树莓派做直播平台
    1 视频压缩软件
    (9-4 )deepsort在ubuntu1604下配置
    利用mvn进行多环境配置
    fidder及Charles使用
    轻松搞定面试中的二叉树题目(java&python)
    数据结构与算法系列 目录
    [转]揭秘webdriver实现原理
    作用域安全的构造函数
    JavaScript函数绑定
  • 原文地址:https://www.cnblogs.com/yigexigua/p/4059772.html
Copyright © 2020-2023  润新知