现在的位置: 首页 > 综合 > 正文

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

2018年01月20日 ⁄ 综合 ⁄ 共 1929字 ⁄ 字号 评论关闭

 

An easy Problem

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

Problem Description
We define eA 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 eA.
 

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 eA , The value of elements of matrix eA must be accurate up to two decimal places. You may assume the range of elements of eA 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;
}

抱歉!评论已关闭.