1. 程式人生 > >POJ 3233 快速矩陣乘法

POJ 3233 快速矩陣乘法

覺得專業課學習有點落下了,而且acm這方面也沒有弄好= =蛋疼死了!!稍微寫寫部落格就開始學習吧!!

矩陣的快速冪和a^b%n類似。運用二分的思想。在這題中需要計算的是A+A^2+A^3+...+A^k的和,由於矩陣相乘有結合律,所以[email protected]#$%^&*.....

不多說了,程式碼很醜。還是拿來佔個位置吧.... 第二次寫遞迴啊!!我的神啊~~

#include<iostream>
#define MAXN 31
using namespace std;


struct Matrix
{
       unsigned long long matrix[MAXN][MAXN];
};

int n,m;

void MatrixPow( int k,Matrix &pre )
{
     int i,j,p;
     if( k<=1 )return ;
     else if( k==2 )
     {
          Matrix now1=pre;
         for( i=1;i<=n;i++ )
         for( j=1;j<=n;j++ )
         {
              pre.matrix[i][j]=0;
              for( p=1;p<=n;p++ )
                   pre.matrix[i][j]+=now1.matrix[i][p]*now1.matrix[p][j];
              pre.matrix[i][j]%=m;
         }
     }
     else if( k%2==0 )
     {
          Matrix now1=pre;
          MatrixPow( k>>1,now1 );
          MatrixPow( 2,now1 );
          pre=now1;
     }
     else
     {
         Matrix now1=pre;
         Matrix now2=pre;
         MatrixPow( k>>1,now1 );
         MatrixPow( 2,now1 );
         for( i=1;i<=n;i++ )
         for( j=1;j<=n;j++ )
         {
              pre.matrix[i][j]=0;
              for( p=1;p<=n;p++ )
                   pre.matrix[i][j]+=now1.matrix[i][p]*now2.matrix[p][j];
              pre.matrix[i][j]%=m;
         }
     }
}

void addMatrix( int k,Matrix &pre )
{
     int i,j,p;
     if( k<=1 )
     return ;
     else if( k==2 )
     {
          Matrix now1=pre;
          Matrix now2=pre;
          MatrixPow( 2,now2 );
          for( i=1;i<=n;i++ )
          for( j=1;j<=n;j++ )
          {
               pre.matrix[i][j]=now1.matrix[i][j]+now2.matrix[i][j];
               pre.matrix[i][j]%=m;
          }
     }
     else if( k%2==0 )
     {
          Matrix now1=pre;
          Matrix now2=pre;
          addMatrix( k>>1,now1 );
          MatrixPow( k>>1,now2 );
          
          for( i=1;i<=n;i++ )
          for( j=1;j<=n;j++ )
          {
               pre.matrix[i][j]=0;
               for( p=1;p<=n;p++ )
                    pre.matrix[i][j]+=now1.matrix[i][p]*now2.matrix[p][j];
               pre.matrix[i][j]%=m;
          }
          
          for( i=1;i<=n;i++ )
          for( j=1;j<=n;j++ )
          {
               pre.matrix[i][j]+=now1.matrix[i][j];
               pre.matrix[i][j]%=m;
          }
     }
     else if( k%2==1 )
     {
         Matrix now1=pre;
         Matrix now2=pre;
         addMatrix( k-1,now1 );
         MatrixPow( k,now2 );
         for( i=1;i<=n;i++ )
         for( j=1;j<=n;j++ )
         {
              pre.matrix[i][j]=now1.matrix[i][j]+now2.matrix[i][j];
              pre.matrix[i][j]%=m;
         }
     }
}

int main()
{
    int k;
    while( scanf( "%d %d %d",&n,&k,&m )!=EOF )
    {
           int i,j;
           Matrix ans;
           for( i=1;i<=n;i++ )
           for( j=1;j<=n;j++ )
           {
                scanf( "%llu",&ans.matrix[i][j] );
           }
           addMatrix( k,ans );
           
           for( i=1;i<=n;i++ )
           {
                for( j=1;j<n;j++ )
                     printf( "%llu ",ans.matrix[i][j]%m );
                printf( "%llu\n",ans.matrix[i][n]%m );     
           }
    } 
    return 0;
}


相關推薦

POJ 3233 快速矩陣乘法

覺得專業課學習有點落下了,而且acm這方面也沒有弄好= =蛋疼死了!!稍微寫寫部落格就開始學習吧!! 矩陣的快速冪和a^b%n類似。運用二分的思想。在這題中需要計算的是A+A^2+A^3+...+A^k的和,由於矩陣相乘有結合律,所以[email protecte

POJ 3233矩陣乘積和 - 快速

table sam namespace ons element bug ssi set sin 題目介紹: Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissi

ncnn 框架分析 openmp多核加速 快取 仿存 cache 快速矩陣乘法 單指令多資料指令SIMD

ncnn 框架分析 本文github連結 在ncnn中建立新層 ncnn 下載編譯使用 參考1 參考2 1. param 和 bin 檔案分析 param 7767517 # 檔案頭 魔數 75 83 # 層數量 輸入輸出blob數量

POJ 3233 Matrix Power Series (矩陣乘法+快速冪+等比二分求和)

再加上快速冪演算法和就好了 #include<iostream> #include<cstring> #include<string> #include<cstdio> #include<algorithm>

POJ 3233-Matrix Power Series( S = A + A^2 + A^3 + … + A^k 矩陣快速冪取模)

spa nta plm lines case arch lang stream 矩陣 Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 20309

Matrix Power Series POJ - 3233 (矩陣快速冪)

傳送門 題意: 題解: 附上程式碼: #include<iostream> #include<cstdio> using namespace std; typedef long long ll; const int MAXN=70; struct n

POJ 3233 Matrix Power Series 【經典矩陣快速冪+二分】

ace series printf acc align clu same pro max 任意門:http://poj.org/problem?id=3233 Matrix Power Series Time Limit: 3000MS Memory Limit

【Matrix Power Series】【POJ - 3233 】(等比矩陣+矩陣乘法

題目: Given a n × n matrix A and a positive integer k, find the sum S = A + A2 +&nb

poj 3233 矩陣乘法(分塊矩陣

題解:Sn為所求矩陣, 則 這樣, 此題就變成了求矩陣冪和矩陣乘法, 分塊矩陣乘法和普通矩陣一樣的。 code: /* adrui's submission Language : C++ Result : Accepted Love : ll Favorite

POJ 3233 Matrix Power Series 【矩陣快速冪+等比矩陣

——————————————————- Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 20125 Accept

POJ 3233 Matrix Power Series 解題報告(子矩陣構造+矩陣快速冪)

Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 14105 Accepted: 6078 Description Given a n × n m

POJ-3735-Training little cats-構造矩陣+矩陣快速冪+稀疏矩陣乘法優化

http://poj.org/problem?id=3735 題意: n只貓,三種命令: 1、第i只貓吃掉所有花生; 2、第i只貓得到一個花生; 3、交換第i,j只貓的花生; 先由k個 這些命令組成一個操作序列 然後重複操作序列m次, n,k<=100,m<=1

POJ 3233 Matrix Power Series(求矩陣冪的和——分塊矩陣快速冪 or 二分遞迴+矩陣快速冪)

Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 21451 Accepted:

POJ 3233 Matrix Power (矩陣快速冪+等比數列求和)

Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 23165 Accepted: 9651 Description Gi

POJ 3233 Matrix Power Series (矩陣快速冪+等比數列二分求和)

Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 23205 Accepted: 9669 Description Given a n × n ma

【bzoj3231】[Sdoi2008]遞歸數列 矩陣乘法+快速

style 其中 std span 處理 轉化 struct set sizeof 題目描述 一個由自然數組成的數列按下式定義: 對於i <= k:ai = bi 對於i > k: ai = c1ai-1 + c2ai-2 + ... + ckai-k

POJ 3734 Blocks (矩陣快速冪)

不同 sans rst tab pac function you space cat 題目鏈接:http://poj.org/problem?id=3734 《挑戰程序設計競賽》202頁。與單純的用遞推式與矩陣快速冪求第N項不同,設染到第i個方塊為止,紅綠都是偶數的方案數目

poj3613:Cow Relays(倍增優化+矩陣乘法floyd+快速冪)

phy rails 模板 矩陣 structure ssi 進制 size and Cow Relays Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7825

矩陣乘法矩陣鏈乘的快速冪優化

urn pan 乘法 color memset blog div for truct 一、矩陣乘法 1 struct datatype 2 { 3 int a[2][2]; 4 }; 5 datatype multiple(datatype x,dat

模板——矩陣快速冪+矩陣乘法

一個 ace 快速 應該 namespace cin ast c++ truct #include<bits/stdc++.h> using namespace std; const long long P=1e9+7; long long n,m;