• 矩阵


      1 #include <iostream>
      2 #include <stdio.h>
      3 #include <string>
      4 #include <string.h>
      5 #include "biginteger.h"
      6 using namespace std;
      7 struct matrix
      8 {
      9     BigInteger g[10][10];
     10     int n, m;
     11     matrix operator=(const matrix &b)
     12     {
     13         n = b.n;
     14         m = b.m;
     15         for (int i = 0; i < n; i++)
     16         {
     17             for (int j = 0; j < m; j++)
     18             {
     19                 g[i][j] = b.g[i][j];
     20             }
     21         }
     22         return *this;
     23     }
     24 };
     25 matrix operator*(const matrix &a, const matrix &b)
     26 {
     27     matrix c;
     28     if (a.m != b.n || a.n != b.m)
     29         return c;
     30     c.n = a.n;
     31     c.m = a.m;
     32     BigInteger sum;
     33     for (int i = 0; i < a.n; i++)
     34     {
     35         for (int j = 0; j < a.m; j++)
     36         {
     37             int I;
     38             sum = 0;
     39             for (I = 0; I < a.m; I++)
     40             {
     41                 sum = sum+(a.g[i][I] * b.g[I][j]);
     42             }
     43             c.g[i][j] = sum;
     44         }
     45     }
     46     return c;
     47 }
     48 matrix pow(matrix a, int b)
     49 {
     50     matrix ans, base = a;
     51     ans.n = ans.m = a.n;
     52     for (int i = 0; i < ans.n; i++)
     53     {
     54         for (int j = 0; j < ans.m;j++)
     55         {
     56             if(i==j)
     57                 ans.g[i][j] = 1;
     58             else ans.g[i][j] = 0;
     59         }
     60     }
     61     while (b)
     62     {
     63         if (b & 1)
     64         {
     65             ans = ans * base;
     66         }
     67         base = base * base;
     68         b >>= 1;
     69     }
     70     return ans;
     71 }
     72 int main()
     73 {
     74     matrix a, b;
     75     int c;
     76     int i, j;
     77     cin >> a.n >> a.m;
     78     for (i = 0; i < a.n; i++)
     79     {
     80         for (j = 0; j < a.m; j++)
     81         {
     82             cin >> a.g[i][j];
     83         }
     84     }
     85     for (i = 0; i < a.n; i++)
     86     {
     87         for (j = 0; j < a.m; j++)
     88         {
     89             cin >> b.g[i][j];
     90         }
     91     }
     92     b.n = b.m = a.n;
     93     matrix A = a;
     94     while (cin >> c)
     95     {
     96         a = A * pow(b, c);
     97         for (i = 0; i < a.n; i++)
     98         {
     99             for (j = 0; j < a.m; j++)
    100             {
    101                 cout << a.g[i][j] << ' ';
    102             }
    103             cout << endl;
    104         }
    105     }
    106 }
  • 相关阅读:
    寻找jar包的方法
    mysql使用小技巧
    JavaSe基础知识总结
    JavaScript进阶和JQuery入门
    Day03 JavaScript入门
    Day02 CSS样式
    Day01-HTML
    Redis详解
    关于开发的包
    关于.Net WebAPI数据认证(包括登陆认证、模型认证)
  • 原文地址:https://www.cnblogs.com/jinmingyi/p/7401381.html
Copyright © 2020-2023  润新知