HDU 5015 233 Matrix (数论——矩阵快速幂)

233 Matrix

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 0    Accepted Submission(s): 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 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
 


题目大意:

        对于一个n+1行,m+1列的矩阵A[n+1][m+1],已知a[0][1]=233,a[0][2]=2333,a[0][3]=23333,…………并且对于矩阵当中元素的值,有a[i][j]=a[i-1][j]+a[i][j-1](i,j!=0)。给出a[1][0],a[2][0],…………,a[n-1][0],a[n][0]以及n,m,求解a[n][m](矩阵第n行第m列元素的值)。


解题思路:

因为a[0][1]=233,a[0][2]=2333,a[0][3]=23333,…………,所以有a[0][i]=10*a[0][i-1]+3(i>1,a[0][1]=233)

如果令a[0][0]=23,那么a[0][i]=10*a[0][i-1]+3(i>0,a[0][0]=23)

则对于n+1行,m+1列的矩阵A,有


第一列(下标j=0)


第二列(下标j=1)


第三列(下标j=2)


对上述矩阵的各列适当变形得:

第一列(下标j=0)


第二列(下标j=1)


第三列(下标j=2)







所以有


根据上面的递推公式,我们可以构造矩阵得,



(其中


矩阵快速幂


代码如下:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <fstream>
#include <limits.h>
#define debug "output for debug\n"
#define pi (acos(-1.0))
#define eps (1e-6)
#define inf (1<<28)
#define sqr(x) (x) * (x)
#define mod 10000007
using namespace std;
typedef long long ll;
typedef unsigned long long ULL;
#define MAX 25
int n;
struct Matrix
{
    ll a[MAX][MAX];
    Matrix()
    {
        memset(a,0,sizeof(a));
    }
};
Matrix operator *(Matrix a,Matrix b)
{
    Matrix c;
    ll i,j,k;
    memset(c.a,0,sizeof(c.a));
    for(k=0;k<n+2;k++)
    {
        for(i=0;i<n+2;i++)
        {
            if(a.a[i][k]==0)
                continue;
            for(j=0;j<n+2;j++)
            {
                if(b.a[k][j]==0)
                    continue;
                c.a[i][j]=(c.a[i][j]+a.a[i][k]*b.a[k][j])%mod;
                c.a[i][j]%=mod;
            }
        }
    }
    return c;
}
Matrix operator ^(Matrix a,int k)
{
    Matrix c;
    ll i;
    for(i=0;i<n+2;i++)
        c.a[i][i]=1;
    while(k)
    {
        if(k&1)
            c=c*a;
        a=a*a;
        k>>=1;
    }
    return c;
}
int main()
{
    Matrix a,b,c;
    ll i,j,k,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        a.a[0][0]=23;
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a.a[i][0]);
        }
        a.a[n+1][0]=3;
        for(i=0;i<n+2;i++)
        {
            for(j=0;j<n+2;j++)
            {
                b.a[i][j]=0;
                if(j==0&&i!=(n+1))
                    b.a[i][j]=10;
                if(j==(n+1))
                    b.a[i][j]=1;
                if(j==0&&i==(n+1))
                    b.a[i][j]=0;
                if(i>=1&&i<=n&&j>=1&&j<=n)
                {
                    if(i>=j)
                        b.a[i][j]=1;
                }
            }
        }
        b=b^m;
        long long ans=0;
        for(i=0;i<n+2;i++)
            ans=(ans+b.a[n][i]*a.a[i][0])%mod;
        printf("%I64d\n",ans%mod);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值