HDU 3521 2010多校联合第9场 数学

An easy Problem

Time Limit: 24000/12000 MS (Java/Others)Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 641Accepted Submission(s): 243


Problem Description
We define e A as following:
Where A is a n×n symmetric matrix with real elements, I is an identity matrix.
Give you matrix A, your task is to calculate e A.

Input
There are several test cases;
Each test case begin with a line contains an integer n (1≤n≤100), the following n lines contain n×n symmetric matrix A. The rang of elements of A is (-100,100);
n=0 is the end of input and need not to proceed.

Output
For each test case, output n lines contain matrix e A , The value of elements of matrix e A must be accurate up to two decimal places. You may assume the range of elements of e A is (-10000,10000).

Sample Input
 
 
1 2 2 1 0 0 1 0

Sample Output
 
 
7.39 2.72 0.00 0.00 2.72

Source
不用去推公式,因为k的阶乘到后面非常大,所以后面的项对和的影响很小。所以只需要循环50次左右就可以了
这个题给的时限很宽,所以不用太在意效率的问题。
我的代码:
#include<stdio.h> struct mart { double mat[105][105]; }; mart kk,xx; int n; mart multi(mart a,mart b) { mart c; int i,j,k; for(i=1;i<=n;i++) for(j=1;j<=n;j++) { c.mat[i][j]=0; for(k=1;k<=n;k++) c.mat[i][j]=c.mat[i][j]+a.mat[i][k]*b.mat[k][j]; } return c; } void copyed(mart p) { int i,j; for(i=1;i<=n;i++) for(j=1;j<=n;j++) xx.mat[i][j]=p.mat[i][j]; } void power(int kp) { mart p,q; int i,j; for(i=1;i<=n;i++) for(j=1;j<=n;j++) { p.mat[i][j]=kk.mat[i][j]; if(i==j) q.mat[i][j]=1.0; else q.mat[i][j]=0.0; } if(kp==0) { copyed(q); return; } while(kp!=1) { if(kp&1) { kp--; q=multi(p,q); } else { kp=kp>>1; p=multi(p,p); } } p=multi(p,q); copyed(p); } double P(int n) { int i; double res=1; if(n==0) return 1.0; for(i=1;i<=n;i++) res=res*i; return res; } int main() { int i,u,v,j; mart ans,yy; double k; while(scanf("%d",&n)!=EOF) { if(n==0) break; for(i=1;i<=n;i++) for(j=1;j<=n;j++) { scanf("%lf",&kk.mat[i][j]); yy.mat[i][j]=kk.mat[i][j]; ans.mat[i][j]=0; } for(i=1;i<=50;i++) { power(i-1); k=P(i-1); for(u=1;u<=n;u++) { for(v=1;v<=n;v++) { ans.mat[u][v]=ans.mat[u][v]+xx.mat[u][v]/k; kk.mat[u][v]=yy.mat[u][v]; } } } for(i=1;i<=n;i++) { for(j=1;j<=n;j++) printf("%.2lf ",ans.mat[i][j]); printf("\n"); } } return 0; }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值