http://acm.hdu.edu.cn/showproblem.php?pid=3521 (暴力)

/*

 *  早知比赛的时候试下了,暴力居然行的 5555555

 */

#include <iostream>

#include <cstdio>

#include <cmath>

#include <cstring>

using namespace std;

 

const int N = 105;

struct Mat{

double num[N][N];

Mat(){

for(int i = 0; i < N; i++)

for(int j = 0; j < N; j++)

num[i][j] = 0;

}

};

 

inline Mat mul(const Mat &a, const Mat &b, const int &n){

Mat ans;

for(int i = 0; i < n; i++){

for(int k = 0; k < n; k++)

if(a.num[i][k])

for(int j = 0; j < n; j++)

if(b.num[k][j])

ans.num[i][j] = (ans.num[i][j] + a.num[i][k] * b.num[k][j]);

}

return ans;

 

}

 

inline Mat mal(Mat a, Mat b, int m, int n){

int temp = m;

int tt = m;

while(m){

if(m & 1){

m--;

a = mul(a, b, n);

}

else{

m >>= 1;

b = mul(b, b, n);

}

}

for(int i = 0; i < n; i++)

for(int j = 0; j < n; j++){

while(temp > 1){

a.num[i][j] /= temp;

temp--;

}

temp = tt;

}

return a;

}

 

inline void Add(Mat &a, const Mat &b, const int &n){

for(int i = 0; i < n; i++)

for(int j = 0; j < n; j++)

a.num[i][j] = a.num[i][j] + b.num[i][j];

//return a;

}

//using namespace std;   // 打的时候不小心这里RE

Mat ans, tt;

Mat a, temp;

Mat b;

int main(){

int n;

while(scanf("%d", &n) != EOF && n){

 

for(int i = 0; i < n; i++)

for(int j = 0; j < n; j++)

ans.num[i][j] = (i == j);

 

temp = ans;

 

for(int i = 0; i < n; i++)

for(int j = 0; j < n; j++)

scanf("%lf", &b.num[i][j]);

for(int i = 1; ; i++){

a = temp;

tt = mal(a, b, i, n);

bool flag = false;

for (int i = 0; i < n; i++) {

for (int j = 0; j < n; j++) {

if (abs(tt.num[i][j]) > 1e-6) {

flag = true;

}

}

}

if (!flag) {

break;

}

 

Add(ans, tt, n);

}

for(int i = 0; i < n; i++){

for(int j = 0; j < n; j++)

if(j != n - 1)

printf("%0.2lf ", ans.num[i][j]);

else

printf("%0.2lf /n", ans.num[i][j]);

 

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值