• HDU


    http://acm.hdu.edu.cn/showproblem.php?pid=3521

    题意

    对于矩阵A,求e^A的值。

    分析

    这个定眼一看好像很熟悉,就是泰勒展开,可惜自己的高数已经还给老师了。。。比赛时不敢直接暴力写,实际上循环到一定次数,余式对结果的影响就相当小了。循环到50几次就可以了

    #include<iostream>
    #include<cmath>
    #include<cstring>
    #include<queue>
    #include<vector>
    #include<cstdio>
    #include<algorithm>
    #include<map>
    #include<set>
    #define rep(i,e) for(int i=0;i<(e);i++)
    #define rep1(i,e) for(int i=1;i<=(e);i++)
    #define repx(i,x,e) for(int i=(x);i<=(e);i++)
    #define X first
    #define Y second
    #define PB push_back
    #define MP make_pair
    #define mset(var,val) memset(var,val,sizeof(var))
    #define scd(a) scanf("%d",&a)
    #define scdd(a,b) scanf("%d%d",&a,&b)
    #define scddd(a,b,c) scanf("%d%d%d",&a,&b,&c)
    #define pd(a) printf("%d
    ",a)
    #define scl(a) scanf("%lld",&a)
    #define scll(a,b) scanf("%lld%lld",&a,&b)
    #define sclll(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
    #define IOS ios::sync_with_stdio(false);cin.tie(0)
    
    using namespace std;
    typedef long long ll;
    template <class T>
    void test(T a){cout<<a<<endl;}
    template <class T,class T2>
    void test(T a,T2 b){cout<<a<<" "<<b<<endl;}
    template <class T,class T2,class T3>
    void test(T a,T2 b,T3 c){cout<<a<<" "<<b<<" "<<c<<endl;}
    const int N = 1e6+10;
    //const int MAXN = 210;
    const int inf = 0x3f3f3f3f;
    const ll INF = 0x3f3f3f3f3f3f3f3fll;
    const ll mod = 1000000007;
    int T;
    void testcase(){
        printf("Case #%d: ",++T);
    }
    const int MAXN = 105;
    const int MAXM = 30;
    
    struct matrix{
        double ma[MAXN][MAXN];
    }x,y,temp;
    
    int n;
    matrix multi(matrix a,matrix b){
        matrix c;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                c.ma[i][j]=0;
                for(int k=1;k<=n;k++){
                    c.ma[i][j]+=a.ma[i][k]*b.ma[k][j];
    
                }
            }
        }
        return c;
    }
    
    double P[55];
    void init(){
        P[0]=1.0;
        for(int i=1;i<55;i++){
            P[i]=i*P[i-1];
        }
    }
    void power(int exp){
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                temp.ma[i][j]=0;
                y.ma[i][j]=x.ma[i][j];
            }
            temp.ma[i][i]=1;
        }
        while(exp){
            if(exp&1){
                temp=multi(temp,y);
            }
            exp>>=1;
            y=multi(y,y);
        }
    }
    int main() {
    #ifdef LOCAL
        freopen("in.txt","r",stdin);
    #endif // LOCAL
        matrix ans;
        init();
        while(~scd(n)&&n){
            for(int i=1;i<=n;i++){
                for(int j=1;j<=n;j++){
                    scanf("%lf",&x.ma[i][j]);
                    ans.ma[i][j]=0;
                }
            }
            for(int i=0;i<55;i++){
                power(i);
                for(int j=1;j<=n;j++){
                    for(int k=1;k<=n;k++){
                        ans.ma[j][k]+=(temp.ma[j][k]/P[i]);
                    }
                }
            }
    
            for(int i=1;i<=n;i++){
                for(int j=1;j<=n;j++){
                    printf("%.2f ",ans.ma[i][j]);
                }
                puts("");
            }
        }
        return 0;
    }
  • 相关阅读:
    浏览器原理
    jQ插件编写
    [转]清理浮动的全家
    百度面试~~
    LeetCode-222. Count Complete Tree Nodes
    LeetCode-236. Lowest Common Ancestor of a Binary Tree
    LeetCode-235. Lowest Common Ancestor of a Binary Search Tree
    LeetCode-102. Binary Tree Level Order Traversal
    LeetCode-404. Sum of Left Leaves
    LeetCode-257. Binary Tree Paths
  • 原文地址:https://www.cnblogs.com/fht-litost/p/9281389.html
Copyright © 2020-2023  润新知