zoukankan      html  css  js  c++  java
  • hdu 5015 233 Matrix (矩阵高速幂)

    233 Matrix

    Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 749    Accepted Submission(s): 453


    Problem Description
    In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 23333, or 233333 ... in the same meaning. And here is the question: Suppose we have a matrix called 233 matrix. In the first line, it would be 233, 2333, 23333... (it means a0,1 = 233,a0,2 = 2333,a0,3 = 23333...) Besides, in 233 matrix, we got ai,j = ai-1,j +ai,j-1( i,j ≠ 0). Now you have known a1,0,a2,0,...,an,0, could you tell me an,m in the 233 matrix?

     

    Input
    There are multiple test cases. Please process till EOF.

    For each case, the first line contains two postive integers n,m(n ≤ 10,m ≤ 109). The second line contains n integers, a1,0,a2,0,...,an,0(0 ≤ ai,0 < 231).
     

    Output
    For each case, output an,m mod 10000007.
     

    Sample Input
    1 1 1 2 2 0 0 3 7 23 47 16
     

    Sample Output
    234 2799 72937
    Hint

    思路:

    第一列元素为:

    0

    a1

    a2

    a3

    a4

    转化为:

    23

    a1

    a2

    a3

    a4

    3

    则第二列为:

    23*10+3

    23*10+3+a1

    23*10+3+a1+a2

    23*10+3+a1+a2+a3

    23*10+3+a1+a2+a3+a4

    3

    依据前后两列的递推关系,有等式可得矩阵A的元素为   

    #include"iostream"
    #include"stdio.h"
    #include"string.h"
    #include"algorithm"
    #include"queue"
    #include"vector"
    using namespace std;
    #define N 15
    #define LL __int64
    const int mod=10000007;
    int n;
    int b[N];
    struct Mat
    {
        LL mat[N][N];
    }a,ans;
    Mat operator*(Mat a,Mat b)
    {
        int i,j,k;
        Mat c;
        memset(c.mat,0,sizeof(c.mat));
        for(i=0; i<=n+1; i++)
        {
            for(j=0; j<=n+1; j++)
            {
                c.mat[i][j]=0;
                for(k=0; k<=n+1; k++)
                {
                    if(a.mat[i][k]&&b.mat[k][j])
                    {
                        c.mat[i][j]+=a.mat[i][k]*b.mat[k][j];
                        c.mat[i][j]%=mod;
                    }
                }
            }
        }
        return c;
    }
    void mult(int k)
    {
        int i;
        memset(ans.mat,0,sizeof(ans.mat));
        for(i=0;i<=n+1;i++)
            ans.mat[i][i]=1;
        while(k)
        {
            if(k&1)
                ans=ans*a;
            k>>=1;
            a=a*a;
        }
    }
    void inti()
    {
        int i,j;
        b[0]=23;
        b[n+1]=3;
        for(i=1; i<=n; i++)
            scanf("%d",&b[i]);
        memset(a.mat,0,sizeof(a.mat));
        for(i=0; i<=n; i++)
        {
            a.mat[i][0]=10;
            a.mat[i][n+1]=1;
        }
        a.mat[n+1][n+1]=1;
        for(i=1; i<n+1; i++)
        {
            for(j=1; j<=i; j++)
            {
                a.mat[i][j]=1;
            }
        }
    }
    int main()
    {
        int i,m;
        while(scanf("%d%d",&n,&m)!=-1)
        {
            inti();
            mult(m);
            LL s=0;
            for(i=0;i<=n+1;i++)
                s=(s+(ans.mat[n][i]*b[i])%mod)%mod;
            printf("%I64d
    ",s);
        }
        return 0;
    }
    




  • 相关阅读:
    Object中的线程等待和Condition
    synchronized锁和Lock锁
    手写二叉排序树(二叉查找树、二叉搜索树)
    JDK源码之ArrayList-Iterator
    JDK源码之ArrayList
    Integer&int,自动装箱&自动拆箱
    学习Zookeeper第一课
    Thumbnailator处理图片
    线程的停止和中断
    BigInteger和BigDecimal
  • 原文地址:https://www.cnblogs.com/cxchanpin/p/7147890.html
Copyright © 2011-2022 走看看