zoukankan      html  css  js  c++  java
  • 11.05T4 矩阵操作

    3444 -- 【七夕模拟】⑨的故事

    Description

      小L最喜欢⑨了。
      给定一个矩阵。你需要计算并输出S=A^9*(A^1+A^2+…+A^k)
      由于数字太大,你只需要输出S的每一项模2012的值就可以了
      给定一整数k<=10^8, 矩阵规模<=30*30

    Input

      第一行2个整数N,K
      第二至第N+1行每行N个整数表示矩阵第i-1行的每一个元素

    Output

      输出如题目所示

    Sample Input

    1 1
    1

    Sample Output

    1

    Hint

    【数据规模】
      50% k<=100
      100% k<=10^8, 矩阵规模<=30*30
     
     
     

    计算 S=A^1+A^2+...+A^k

    令X=A^1+A^2+...+A^(k/2)

    Ans=(A^(K/2)+E)*X

    if K is奇数 then 快速幂quickpow(A^K)

    else Ans

    重载一下运算符要清楚一点

    code:

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 using namespace std;
     5 int n;
     6 const int mod=2012;
     7 struct matrix {
     8     int a[31][31];
     9     matrix() {
    10         memset(a,0,sizeof a);
    11     }
    12 };
    13 matrix operator*(matrix a,matrix b) {
    14     matrix c;
    15     for(int i=1; i<=n; i++)
    16         for(int j=1; j<=n; j++)
    17             for(int k=1; k<=n; k++)
    18                 c.a[i][j]=(c.a[i][j]+a.a[i][k]*b.a[k][j])%mod;
    19     return c;
    20 }
    21 matrix operator+(const matrix a,const matrix b) {
    22     matrix c;
    23     for(int i=1; i<=n; i++)
    24         for(int j=1; j<=n; j++)
    25             c.a[i][j]=(a.a[i][j]+b.a[i][j])%mod;
    26     return c;
    27 }
    28 matrix operator^(matrix a,int b) {
    29     matrix c;
    30     for(int i=1; i<=n; i++)c.a[i][i]=1;
    31     for(; b; b>>=1) {
    32         if(b&1) {
    33             c=c*a;
    34         }
    35         a=a*a;
    36     }
    37     return c;
    38 }
    39 matrix base;
    40 matrix solve(int k) {
    41     if(k==1)return base;
    42     if(k%2==0){
    43         matrix l=solve(k/2);
    44         matrix r=base^(k/2);
    45         return (l*r)+l;
    46     }
    47     else{
    48         matrix l=solve(k/2);
    49         matrix r=base^(k/2);
    50         return ((l*r)+l)+(base^k);
    51     }
    52 }
    53 int main() {
    54     int k;
    55     cin>>n>>k;
    56     for(int i=1; i<=n; i++) {
    57         for(int j=1; j<=n; j++) {
    58             cin>>base.a[i][j];
    59         }
    60     }
    61     matrix other=base^9;
    62     matrix r=solve(k);
    63     other=other*r;
    64     for(int i=1;i<=n;i++){
    65         for(int j=1;j<=n;j++){
    66             cout<<other.a[i][j]<<" ";
    67         }
    68         cout<<'
    ';
    69     }
    70     return 0;
    71 }

    over

  • 相关阅读:
    类成员指针
    css图片旋转
    px转化rem
    flexible.js页面布局
    SpringBoot-静态资源映射
    Springboot-日志框架
    Springboot-配置文件
    spring的xml和注解整合
    在mysql中将JSON数组转换为行数据[转]
    php 拼音
  • 原文地址:https://www.cnblogs.com/saionjisekai/p/9911872.html
Copyright © 2011-2022 走看看