zoukankan      html  css  js  c++  java
  • 关于 矩阵在ACM中的应用

    关于矩阵在ACM中的应用


    1、矩阵运算法则

    重点说说矩阵与矩阵的乘法,不说加减法。

    支持:

    • 结合律  (AB)C = A(BC)
    • 分配律 A(B+C) = AB + AB
    • $left( lambda A ight) B=lambda left( AB ight) =Aleft( lambda B ight) $

    2、矩阵乘法的程序实现:

    struct matrix
    {
        double a[200][200];
    } ans, base;
    
    matrix multiply(matrix x, matrix y)
    {
        matrix tmp;
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++)
            {
                tmp.a[i][j] = 0;
                for (int k = 0; k < n; k++) tmp.a[i][j] += x.a[i][k] * y.a[k][j];
            }
        return tmp;
    }

    3、矩阵的幂运算可以进行快速幂

      因为矩阵的乘法支持结合律,所以B*A*A*A = B(A*A*A) = B*$A^{3}$

    程序实现:

    void fast_mod(int k)
    {
        while (k)
        {
            if (k & 1) ans = multiply(ans, base);
            base = multiply(base, base);
            k >>= 1;
        }
    }

     

    4、将行列式的变换动作构造成一个矩阵(如 平移 旋转 翻转 等操作)

    5、将一个一次递推式 构造出 矩阵。

      例如:fibonacci数列的递推关系 f(n) = f(n-1) + f(n-2)

      我们可以将矩阵构造为:$left( egin{matrix} 0& 1\ 1& 1end{matrix} ight) left( egin{matrix} a\ bend{matrix} ight) =left( egin{matrix} b\ a+bend{matrix} ight) $


    比如今天做的一个题目  zoj 2853 Evolution

      它就是先进行构造“进化”这个动作的矩阵。然后有多少次进化就等于"初始物种状态矩阵" 乘 "进化矩阵”

    题目 见文末

    附上我的代码:

    #include<cstdio>
    #include<cstring>
    int n;
    struct matrix
    {
        double a[200][200];
    } ans, base;
    
    matrix multiply(matrix x, matrix y)
    {
        matrix tmp;
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++)
            {
                tmp.a[i][j] = 0;
                for (int k = 0; k < n; k++) tmp.a[i][j] += x.a[i][k] * y.a[k][j];
            }
        return tmp;
    }
    void fast_mod(int k)
    {
        while (k)
        {
            if (k & 1) ans = multiply(ans, base);
            base = multiply(base, base);
            k >>= 1;
        }
    }
    int main()
    {
        int m, t, xi, yi;
        double s, zi, fin;
        int num[205];
        while (~scanf("%d%d", &n, &m) && !(n == 0 && m == 0))
        {
            for (int i = 0; i < n; i++) scanf("%d", &num[i]);
            scanf("%d", &t);
            memset(base.a, 0, sizeof(base.a));
            for (int i = 0; i < n; i++) base.a[i][i] = 1;   //初始为进化成自己
            while (t--)
            {
                scanf("%d%d%lf", &xi, &yi, &zi);
                base.a[xi][yi] += zi;
                base.a[xi][xi] -= zi;
            }
            memset(ans.a, 0, sizeof(ans.a));
            for (int i = 0; i < n; i++) ans.a[i][i] = 1;
            fast_mod(m);
            fin = 0;
            for (int i = 0; i < n; i++)
                fin += num[i] * ans.a[i][n-1];
            printf("%.0f
    ", fin);
        }
        return 0;
    }
    View Code

    6、矩阵在图的邻接矩阵中的应用

      暂时还不会, = =、 以后补上。

    Evolution

    Time Limit: 5 Seconds      Memory Limit: 32768 KB

    Evolution is a long, long process with extreme complexity and involves many species. Dr. C. P. Lottery is currently investigating a simplified model of evolution: consider that we have N (2 <= N <= 200) species in the whole process of evolution, indexed from 0 to N -1, and there is exactly one ultimate species indexed as N-1. In addition, Dr. Lottery divides the whole evolution process into M (2 <= M <= 100000) sub-processes. Dr. Lottery also gives an 'evolution rate' P(i, j) for 2 species i and j, where i and j are not the same, which means that in an evolution sub-process, P(i, j) of the population of species i will transform to species j, while the other part remains unchanged.

    Given the initial population of all species, write a program for Dr. Lottery to determine the population of the ultimate species after the evolution process. Round your final result to an integer.

    Input

    The input contains multiple test cases!

    Each test case begins with a line with two integers NM. After that, there will be a line with N numbers, indicating the initial population of each species, then there will be a number T and T lines follow, each line is in format "i j P(i,j)" (0 <= P(i,j) <=1).

    A line with N = 0 and M = 0 signals the end of the input, which should not be proceed.

    Output

    For each test case, output the rounded-to-integer population of the ultimate species after the whole evolution process. Write your answer to each test case in a single line.

    Notes

    • There will be no 'circle's in the evolution process.
    • E.g. for each species i, there will never be a path i, s1, s2, ..., st, i, such that P(i,s1) <> 0, P(sx,sx+1) <> 0 and P(st, i) <> 0.
    • The initial population of each species will not exceed 100,000,000.
    • There're totally about 5 large (N >= 150) test cases in the input.

    Example

    Let's assume that P(0, 1) = P(1, 2) = 1, and at the beginning of a sub-process, the populations of 0, 1, 2 are 40, 20 and 10 respectively, then at the end of the sub-process, the populations are 0, 40 and 30 respectively.

    Sample Input

    2 3
    100 20
    1
    0 1 1.0
    4 100
    1000 2000 3000 0
    3
    0 1 0.19
    1 2 0.05
    0 2 0.67
    0 0

    Sample Output

    120
    0

  • 相关阅读:
    ARM的存储器映射与存储器重映射
    Nand Flash与Nor
    内核 任务的堆栈切换
    Linux设备模型(总结)
    file结构体中private_data指针的疑惑
    Sysfs文件系统与Linux设备模型
    认识udev
    Linux操作系统下的常见系统资源共享
    linux下的udev是干嘛的,能否说的通俗点
    udev详解
  • 原文地址:https://www.cnblogs.com/shawn-ji/p/5682488.html
Copyright © 2011-2022 走看看