题意:模拟计算一个e的A矩阵次方的公式,要求精度到小数点后两位。
思路:公式明显能看出,多项式后面的项开始变小,最终e的A次方收敛在一个值,根据数据范围估计150之后应该可以达到精度要求。
代码:
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <map>
#include <list>
#include <set>
#include <stack>
#include <queue>
#include <string>
#include <sstream>
#define pb push_back
#define X first
#define Y second
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define pii pair<int,int>
#define qclear(a) while(!a.empty())a.pop();
#define lowbit(x) (x&-x)
#define sd(n) scanf("%d",&n)
#define sdd(n,m) scanf("%d%d",&n,&m)
#define sddd(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define mst(a,b) memset(a,b,sizeof(a))
#define cout3(x,y,z) cout<<x<<" "<<y<<" "<<z<<endl
#define cout2(x,y) cout<<x<<" "<<y<<endl
#define cout1(x) cout<<x<<endl
#define IOS std::ios::sync_with_stdio(false)
#define SRAND srand((unsigned int)(time(0)))
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
using namespace std;
const double PI=acos(-1.0);
const int INF=0x3f3f3f3f;
const ll INFF=0x3f3f3f3f3f3f3f3f;
const ll mod=998244353;
const double eps=1e-8;
const int maxn=105;
const int maxm=10005;
int len;
struct mat {
double m[maxn][maxn];
void clr() {
mst(m,0);
for(int i=0; i<len; i++) {
m[i][i]=1.0;
}
}
mat operator * (const mat b)const {
mat res;
mst(res.m,0);
for(int i=0; i<len; i++) {
for(int j=0; j<len; j++) {
for(int k=0; k<len; k++) {
res.m[i][j]+=m[i][k]*b.m[k][j];
}
}
}
return res;
}
};
void solve() {
int n;
while(sd(n)) {
if(n==0)
break;
len=n;
mat A;
double mx=-105.0;
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
scanf("%lf",&A.m[i][j]);
mx=max(mx,A.m[i][j]);
}
}
mat ans,temp;
ans.clr();
temp.clr();
for(int i=0; i<len; i++) {
for(int j=0; j<len; j++) {
temp.m[i][j]=A.m[i][j];
}
}
for(int cnt=2;cnt<150; cnt++) {
for(int i=0; i<len; i++) {
for(int j=0; j<len; j++) {
ans.m[i][j]+=A.m[i][j];
}
}
for(int i=0; i<len; i++) {
for(int j=0; j<len; j++) {
A.m[i][j]/=cnt;
}
}
A=A*temp;
}
for(int i=0; i<len; i++) {
for(int j=0; j<len; j++) {
printf("%.2f ",ans.m[i][j]);
}
printf("\n");
}
}
return ;
}
int main() {
#ifdef LOCAL
freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
#else
// freopen("","r",stdin);
// freopen("","w",stdout);
#endif
solve();
return 0;
}