Drolca

Apologize To Drolca
随笔 - 28, 文章 - 1, 评论 - 6, 引用 - 0
数据加载中……

Matrix

#include <iostream>
using namespace std;

const int maxn=32;
int n,k,m;
struct matrix
{
    
int m[maxn][maxn];
}
;
matrix mat;

matrix 
operator*(const matrix &a,const matrix &b)
{
    matrix res;
    
int i,j;
    
for(i=1;i<=n;i++)
        
for(j=1;j<=n;j++)
        
{
            res.m[i][j]
=0;
            
for(k=1;k<=n;k++)
            
{
                res.m[i][j]
+=a.m[i][k]*b.m[k][j];
                res.m[i][j]
%=m;
            }

        }

    
return res;
}


matrix 
operator+(const matrix &a,const matrix &b)
{
    matrix res;
    
int i,j;
    
for(i=1;i<=n;i++)
        
for(j=1;j<=n;j++)
            res.m[i][j]
=(a.m[i][j]+b.m[i][j])%m;
    
return res;
}


matrix power(
int k)
{
    matrix temp,res;
    
if(k==1)
        
return mat;
    
else
    
{
        temp
=power(k/2);
        res
=temp*temp;
        
if(k%2==1)
            res
=res*mat;
        
return res;
    }

}


matrix solve(
int k)
{
    matrix temp,res;
    
if(k==1)
        
return mat;
    
else
    
{
        res
=solve(k/2);
        
if(k%2==1)
        
{
            temp
=power(k/2+1);
            res
=temp*res+res+temp;
        }

        
else
        
{
            res
=power(k/2)*res+res;
        }

        
return res;
    }

}


void printf(const matrix & m)
{
    
int i,j;
    
for(i=1;i<=n;i++)
    
{
        
for(j=1;j<n;j++)
            printf(
"%d ",m.m[i][j]);
        printf(
"%d\n",m.m[i][j]);
    }

}


int main()
{
    scanf(
"%d%d%d",&n,&k,&m);
    
int i,j;
    
for(i=1;i<=n;i++)
        
for(j=1;j<=n;j++)
            scanf(
"%d",&mat.m[i][j]);
    matrix temp
=solve(k);
    printf(temp);
    
return 0;
}

posted on 2009-09-15 01:10 Drolca 阅读(70) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理