• poj3233Matrix Power Series


    链接

    也是矩阵经典题目  二分递归求解

    a+a^2+a^3+..+a^(k/2)+a^(k/2+1)+...+a^k = a+a^2+..+a^k/2+a^k/2(a^1+a^2+..+a^k/2)(偶数)

    a+a^2+a^3+..+a^(k/2)+a^(k/2+1)+...+a^k = a+a^2+..+a^k/2+a^k/2(a^1+a^2+..+a^k/2)+a^k。 奇数

     1 #include <iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<stdlib.h>
     6 #include<vector>
     7 #include<cmath>
     8 #include<queue>
     9 #include<set>
    10 using namespace std;
    11 #define N 1e9
    12 #define LL long long
    13 #define INF 0xfffffff
    14 const double eps = 1e-8;
    15 const double pi = acos(-1.0);
    16 const double inf = ~0u>>2;
    17 struct Mat
    18 {
    19     int mat[31][31];
    20 };
    21 int n,mod;
    22 Mat operator + (Mat a,Mat b)
    23 {
    24     Mat c;
    25     int i,j;
    26     for(i = 0 ; i < n ;i++)
    27         for(j = 0 ;j < n ;j++)
    28         {
    29             if(a.mat[i][j]+b.mat[i][j]>mod)
    30             c.mat[i][j] = (a.mat[i][j]+b.mat[i][j])%mod;
    31             else
    32             c.mat[i][j] = a.mat[i][j]+b.mat[i][j];
    33         }
    34     return c;
    35 }
    36 Mat operator * (Mat a,Mat b)
    37 {
    38     Mat c;
    39     memset(c.mat,0,sizeof(c.mat));
    40     int i,j,k;
    41     for(k =0 ; k < n ; k++)
    42     {
    43         for(i = 0 ; i < n ;i++)
    44         {
    45             if(a.mat[i][k]==0) continue;
    46             for(j = 0 ;j < n ;j++)
    47             {
    48                 if(b.mat[k][j]==0) continue;
    49                 c.mat[i][j] = (c.mat[i][j]+a.mat[i][k]*b.mat[k][j])%mod;
    50             }
    51         }
    52     }
    53     return c;
    54 }
    55 Mat operator ^(Mat a,int k)
    56 {
    57     Mat c;
    58     int i,j;
    59     for(i =0 ; i < n ;i++)
    60         for(j = 0; j < n ;j++)
    61         c.mat[i][j] = (i==j);
    62     for(; k ;k >>= 1)
    63     {
    64         if(k&1) c = c*a;
    65         a = a*a;
    66     }
    67     return c;
    68 }
    69 Mat solve(Mat x,int k)
    70 {
    71     if(k==1) return x;
    72     Mat c ;
    73     c = x^k;
    74     Mat a = solve(x,k/2);
    75     Mat b = x^(k/2);
    76     if(k&1) c = a+b*a+c;
    77     else c = a+b*a;
    78     return c;
    79 }
    80 int main()
    81 {
    82     int t;
    83     int i,j;
    84     while(scanf("%d%d%d",&n,&t,&mod)!=EOF)
    85     {
    86         Mat x;
    87         for(i = 0 ; i < n ;i++)
    88             for(j = 0; j < n ;j++)
    89             scanf("%d",&x.mat[i][j]);
    90         x = solve(x,t);
    91         for(i =0 ; i < n ;i++)
    92         {
    93             for(j =0 ; j < n-1; j++)
    94             printf("%d ",x.mat[i][j]%mod);
    95             printf("%d
    ",x.mat[i][n-1]%mod);
    96         }
    97     }
    98     return 0;
    99 }
    View Code
  • 相关阅读:
    转 IDEA 解决代码提示功能消失
    模态框居中显示
    DetachedCriteria和Criteria的使用方法
    struts2配置文件详解
    springMVC上传图片
    在linux下运行mongodb
    webSocket客服在线交谈
    接口自动化
    easyui input文本框清除值
    Spring总结
  • 原文地址:https://www.cnblogs.com/shangyu/p/3621001.html
Copyright © 2020-2023  润新知