HDU 5015 233 Matrix(矩阵快速幂)

115 篇文章 0 订阅
7 篇文章 0 订阅

233 Matrix

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

这里写图片描述

Source
2014 ACM/ICPC Asia Regional Xi’an Online

思路:刚开始看题时也以为是矩阵快速幂,但是不会构造转换矩阵,所以就放弃了这种想法。。。

矩阵是这样构造的
通过题目我们可以推出矩阵

a1     a1+233             a1+2333             a1+23333…..
a2    a1+a2+233       a1+a2+2333       a1+a2+23333…..
a3    a1+a2+a3+233 a1+a2+a3+2333 a1+a2+a3+23333…..
……

所以我构造这样一个初始矩阵

23
a1
a2
a3
….
3

转换矩阵A是这样的

10 0 0 0 ….. 1
10 1 0 0 ….. 1
10 1 1 0 ….. 1
10 1 1 1 ….. 1
…….
0  0 0 0 …… 1

那么第n行的第m个数就是A的m次方*第一列

代码:

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

#define N 15
#define mod 10000007
#define mem(a,b) memset(a,b,sizeof(a))
typedef long long LL;

struct Matrix
{
    LL mat[N][N];
};
Matrix unit_matrix;
int n,m;
int s[15];

Matrix mul(Matrix a,Matrix b)//矩阵相乘
{
    Matrix res;
    for(int i=0; i<=n+1; ++i)
        for(int j=0; j<=n+1; ++j)
        {
            res.mat[i][j]=0;
            for(int k=0; k<=n+1; ++k)
                res.mat[i][j]=(res.mat[i][j]+a.mat[i][k]*b.mat[k][j]%mod)%mod;
        }
    return res;
}

Matrix pow_matrix(Matrix a,int k)//矩阵快速幂
{
    Matrix res=unit_matrix;
    while(k)
    {
        if(k&1)
            res=mul(res,a);
        a=mul(a,a);
        k>>=1;
    }
    return res;
}

int main()
{
    Matrix arr,p;
    while(~scanf("%d%d",&n,&m))
    {
        for(int i=0; i<=n+1; ++i)//单位矩阵
            unit_matrix.mat[i][i]=1;
        for(int i=1; i<=n; ++i)//构造初始矩阵
            scanf("%d",&s[i]);
        s[0]=23,s[n+1]=3;//初始矩阵
        mem(arr.mat,0);
        for(int i=0; i<=n+1; ++i)//构造转换矩阵
        {
            arr.mat[i][0]=10,arr.mat[i][n+1]=1;
            for(int j=1; j<=i; ++j)
                arr.mat[i][j]=1;
        }
        for(int i=0; i<=n; ++i)//转换矩阵
            arr.mat[n+1][i]=0;
        p=pow_matrix(arr,m);
        int ans=0;
        for(int i=0; i<=n+1; ++i)//找到第n行第m个数
            ans=(ans+s[i]*p.mat[n][i]%mod)%mod;
        printf("%d\n",ans);
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值