HDU 5015 233 Matrix(矩阵快速幂模板)

标签: kuangbin矩阵
4人阅读 评论(0) 收藏 举报
分类:

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).

Ouput

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

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<memory.h>
#include<algorithm>
using namespace std;
typedef long long LL;//一般结果都是很大的
const int N=12;//推导出来的矩阵T的大小
const int M=10000007;
LL tem[N][N];//临时数组
LL a[N][N];//矩阵T,推导出来的
LL res[N][N];//结果矩阵
LL b[N];//初始(原始的)矩阵
LL sum;
void mult(LL res[][N],LL a[][N]) {//矩阵相乘,脑袋短路竟然卡了形参传递问题
    LL tem[N][N];
    memset(tem,0,sizeof(tem));
    for(int i=0; i<N; i++) {
        for(int j=0; j<N; j++) {
            for(int k=0; k<N; k++) {
                tem[i][j]+=(res[i][k]*a[k][j])%M;
            }
        }
    }
    for(int i=0; i<N; i++) {
        for(int j=0; j<N; j++) {
            res[i][j]=tem[i][j];
        }
    }
    return;
}
void quick_mod(LL a[][N],LL n) {
    memset(res,0,sizeof(res));
    for(int i=0; i<N; i++) {
        res[i][i]=1;
    }
    while(n) {//矩阵快速幂
        if(n&1) {
            mult(res,a);
        }
        mult(a,a);
        for(int i=0;i<N;i++){
            for(int j=0;j<N;j++){
                cout<<a[i][j]<<" ";
            }
            cout<<endl;
        }
        cout<<endl;
        n>>=1;
    }
    return;
}
int main() {
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF) {
        memset(b,0,sizeof(b));
        //推导出的矩阵T
        memset(a,0,sizeof(a));
        for(int i=0; i<=10; i++) {
            a[i][0]=10;
            a[i][N-1]=1;
        }
        a[N-1][N-1]=1;
        for(int i=1; i<=10; i++) {
            for(int j=1; j<=i; j++) {
                a[i][j]=1;
            }
        }
        //初始(原始矩阵,通常是一维矩阵)
        b[0]=23;
        b[N-1]=3;
        sum=0;
        for(int i=1; i<=n; i++) {
            scanf("%lld",&b[i]);
        }
        quick_mod(a,m);//矩阵快速幂
        for(int i=0; i<N; i++) {
            sum+=(res[n][i]*b[i])%M;
        }
        printf("%lld\n",sum%M);
    }
    return 0;
}

另外附上快速幂的模板,矩阵快速幂其实就是整数快速幂推广到二维的结果

int quick_pow(int a,int b){
    int base=a;
    int res=1;
    while(b){
        if(b&1)res*=base;
        base*=base;
        b>>=1;
    }
    return res;
}
查看评论

hdu-5015-233 Matrix-矩阵

很显然矩阵的第一列为: 0 a[1] a[2] a[3] a[4] 我们转化一下,转化为 23 a[1] a[2] a[3] a[4] 3 那么由第一列转移到第二列则为 23*10+3 a[1]+23...
  • rowanhaoa
  • rowanhaoa
  • 2014-09-17 15:54:51
  • 2562

hdu 5015 233 Matrix (矩阵快速幂)

233 Matrix Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tota...
  • u011721440
  • u011721440
  • 2014-09-19 18:17:14
  • 1567

HDU 5015 233 Matrix(矩阵快速幂)

Description 一个(n+1)*(m+1)的矩阵a,a[0][0]=0,a[0][1]=233,a[0][2]=2333,…,a[i][j]=a[i-1][j]+a[i]j-1,现输入a[1...
  • V5ZSQ
  • V5ZSQ
  • 2016-03-02 09:17:28
  • 293

【HDU】5015 233 Matrix 矩阵快速幂

传送门:【HDU】5015 233 Matrix
  • u013368721
  • u013368721
  • 2014-09-14 18:13:40
  • 3706

hdu 5015 Matrix 233 矩阵快速幂

题目链接:点击打开链接
  • alpc_paul
  • alpc_paul
  • 2014-09-15 16:44:16
  • 350

233 Matrix hdu 5015(矩阵快速幂)

题意:矩阵中第一行为0,233,2333...第一列为输入的值,其他位置的值为a i,j = a i-1,j +a i,j-1( i,j ≠ 0),读题时需要注意的一点是下标是从0开始的,不要和第...
  • lxy767087094
  • lxy767087094
  • 2016-10-12 22:01:12
  • 138

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

233 Matrix HDU - 5015 In our daily life we often use 233 to express our feelings. Actually, we may ...
  • Fusheng_Yizhao
  • Fusheng_Yizhao
  • 2018-01-26 11:15:21
  • 81

233 Matrix - HDU 5015 - 矩阵快速幂

链接:  http://acm.hdu.edu.cn/showproblem.php?pid=5015题目:Problem DescriptionIn our daily life we often ...
  • xs18952904
  • xs18952904
  • 2017-08-06 15:16:08
  • 78

HDU 5015 233 Matrix (矩阵快速幂)

题目链接:HDU 5015 233 Matrix 题意:已知矩阵的第一行(可以递推出来)和第一列,其他ai,j = ai-1,j +ai,j-1,求an,m 思路:写出先几个ai,...
  • u012377575
  • u012377575
  • 2014-09-15 19:37:04
  • 529

HDU 5015 233 Matrix --矩阵快速幂

题意:给出矩阵的第0行(233,2333,23333,...)和第0列a1,a2,...an(n 解法:看到n 这样相当于右图:,红色部分为绿色部分之和,而顶上的绿色部分很好求,左边的绿色部分(最...
  • hq572241670
  • hq572241670
  • 2014-11-27 18:44:37
  • 451
    个人资料
    持之以恒
    等级:
    访问量: 599
    积分: 294
    排名: 26万+
    文章存档
    最新评论