HDU 5015 - 233 Matrix (矩阵构造 矩阵快速幂)

36 篇文章 0 订阅

233 Matrix

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 644    Accepted Submission(s): 386



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 a 0,1 = 233,a 0,2 = 2333,a 0,3 = 23333...) Besides, in 233 matrix, we got a i,j = a i-1,j +a i,j-1( i,j ≠ 0). Now you have known a 1,0,a 2,0,...,a n,0, could you tell me a n,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 ≤ 10 9). The second line contains n integers, a 1,0,a 2,0,...,a n,0(0 ≤ a i,0 < 2 31).
 

Output
For each case, output a n,m mod 10000007.
 

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

Sample Output
  
  
234 2799 72937
Hint
 

Source
 

Recommend
hujie   |   We have carefully selected several similar problems for you:   5017  5016  5015  5014  5013 
 

Statistic |  Submit |  Discuss |  Note


题意:

给一个矩阵,矩阵里面的元素满足A[i][j] = A[i-1][j] + A[i][j-1]

A[0][1] = 233, A[0][2] = 2333,A[0][3] = 2333...

给出A[1][0] A[2][0] A[3][0] ... A[n][0]的值,求A[n][m]。n<=10 m<=10^9



比赛的时候没有做出来,没有接触过通过这类题目,今天学了一下,然后很自然的做出来了,

设由原矩阵第0列拓展得到的矩阵OM为(1 * (n+2) ):

23 a1 a2 a3 .... an 3

构造一个(n+1) * (n+2)的矩阵A:

10 10 10 10... 10 0

0    1   1  1  ...  1  0

0    0   1  1  ...  1  0

0    0   0  1  ...  1  0

.....

1    1   1   1  ...  1  1


则第i列的元素可以由i-1列得到:

OMi = OMi-1 * A

所以第m列的元素可以递推得到,然后用矩阵快速幂求出即可




#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <set>

using namespace std;

#define WIN
#ifdef WIN
typedef __int64 LL;
#define iform "%I64d"
#define oform "%I64d\n"
#define oform1 "%I64d"
#else
typedef long long LL;
#define iform "%lld"
#define oform "%lld\n"
#define oform1 "%lld"
#endif

#define S64I(a) scanf(iform, &(a))
#define P64I(a) printf(oform, (a))
#define P64I1(a) printf(oform1, (a))
#define REP(i, n) for(int (i)=0; (i)<n; (i)++)
#define REP1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FOR(i, s, t) for(int (i)=(s); (i)<=(t); (i)++)

const int INF = 0x3f3f3f3f;
const double eps = 1e-9;
const double PI = (4.0*atan(1.0));


const int maxn = 15;

struct Matrix {
    LL e[maxn][maxn];
    int n, m;
    Matrix(int n=0, int m=0) : n(n), m(m) { clear(); }
    void clear() {
        memset(e, 0, sizeof(e));
    }
};

Matrix MatrixMult(Matrix a, Matrix b, int MOD) {
    Matrix res(a.n, b.m);
    for(int i=0; i<res.n; i++) {
        for(int j=0; j<res.m; j++) {
            for(int k=0; k<a.m; k++) {
                res.e[i][j] = (res.e[i][j] + a.e[i][k] * b.e[k][j] ) % MOD;
            }
        }
    }
    return res;
}

Matrix MatrixPow(Matrix A, int p, int MOD) {
    Matrix res(A.n, A.m);
    for(int i=0; i<res.n; i++) res.e[i][i] = 1;
    while(p) {
        if(p & 1) res = MatrixMult(res, A, MOD);
        A = MatrixMult(A, A, MOD);
        p >>= 1;
    }
    return res;
}

int main() {
    int n, m;
    int MOD = 10000007;

    while(scanf("%d%d", &n, &m) != EOF) {
        Matrix OM(1, n+2);
        OM.e[0][0] = 23;
        OM.e[0][OM.m-1] = 3;
        for(int i=1; i<OM.m-1; i++) {
            S64I(OM.e[0][i]);
        }
        Matrix A(n+2, n+2);
        A.e[A.n-1][A.m-1] = 1;
        for(int i=0; i<A.m-1; i++) A.e[0][i] = 10, A.e[n+1][i] = 1;
        for(int i=1; i<A.m-1; i++) {
            for(int j=1; j<=i; j++) {
                A.e[j][i] = 1;
            }
        }
        A = MatrixPow(A, m, MOD);
        Matrix tM = MatrixMult(OM, A, MOD);
        LL ans = tM.e[0][n];
        P64I(ans);
    }

    return 0;
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值