海岛Blog

海是无垠的,岛在海中是独立的,有一片自己的天空

HDU5015 233 Matrix【矩阵快速幂】

233 Matrix

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


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 112 20 03 723 47 16
 

Sample Output
234279972937
Hint
 

Source


问题链接HDU5015 233 Matrix

问题简述:(略)

问题分析:占个位置,不解释

程序说明:(略)

题记:(略)

参考链接:(略)


AC的C++语言程序如下:

/* HDU5015 233 Matrix */

#include <iostream>
#include <string.h>
#include <stdio.h>

//#define DEBUG

using namespace std;

typedef long long LL;

const int MOD = 10000007;
const int N = 10;

int n, m;

struct Matrix
{
    LL m[N + 3][N + 3];
    Matrix()
    {
        memset(m, 0, sizeof(m));
    }
};

Matrix Matrix_Mul(Matrix a, Matrix b)
{
    Matrix c;

    for(int i=1; i<=n+2; i++)
        for(int j=1; j<=n+2; j++)
            for(int k=1; k<=n+2; k++)
                c.m[i][j] = (c.m[i][j] + a.m[i][k] * b.m[k][j]) % MOD;

    return c;
}

// 矩阵模幂
Matrix Matrix_Powmul(Matrix a, int m)
{
    Matrix c;

    for(int i=1; i<=n+2; i++)
        c.m[i][i] = 1;
    while(m) {
        if(m & 1)
            c = Matrix_Mul(c, a);
        a = Matrix_Mul(a, a);
        m >>= 1;
    }

    return c;
}

#ifdef DEBUG
void Matrix_Print(Matrix a, const char *name)
{
    printf("matrix %s\n", name);
    for(int i=1; i<=n+2; i++) {
        for(int j=1; j<=n+2; j++)
            printf("%lld ", a.m[i][j]);
        printf("\n");
    }
}
#endif

int main()
{
    while(~scanf("%d%d", &n, &m)) {
        Matrix a, b;

        a.m[1][1] = 23;
        for(int i=1; i<=n; i++)
            scanf("%lld", &a.m[i + 1][1]);
        a.m[n + 2][1] = 3;

        for(int i=1; i<=n+1; i++)
            b.m[i][1] = 10;
        for(int i=1; i<=n+2; i++)
            b.m[i][n + 2] = 1;
        for(int i=2; i<=n+1; i++)
            for(int j=2; j<=i; j++)
                b.m[i][j] = 1;

#ifdef DEBUG
        Matrix_Print(a, "a");
        Matrix_Print(b, "b");
#endif

        b = Matrix_Powmul(b, m);
        a = Matrix_Mul(b, a);

#ifdef DEBUG
        Matrix_Print(a, "a");
#endif

        printf("%lld\n", a.m[n + 1][1]);
    }

    return 0;
}



阅读更多
所属专栏: ACM题解
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭