码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 5015 233 Matrix(矩阵快速幂)

时间:2015-03-20 00:00:50      阅读:212      评论:0      收藏: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).
 

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
技术分享
 
由于n比较小,m比较大,我们可以想到可以一列一列的递推。而一列一列的
递推的难点就在构造23,233, 2333...我们可以想到233=23*10+3,所以在每列
后在加一个数字3,于是就可以按列来递推了。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<bitset>
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
typedef long long LL;
typedef pair<int,int>pil;
const int INF = 0x3f3f3f3f;
const int MOD=10000007;
LL n,m;
LL num[20];
struct Matrix{
    LL mat[15][15];
    void Clear()
    {
        CLEAR(mat,0);
    }
};
Matrix mult(Matrix m1,Matrix m2)
{
    Matrix ans;
    for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
        {
            ans.mat[i][j]=0;
            for(int k=0;k<n;k++)
                ans.mat[i][j]=(ans.mat[i][j]+m1.mat[i][k]*m2.mat[k][j])%MOD;
        }
    return ans;
}
Matrix Pow(Matrix m1,LL b)
{
    Matrix ans;ans.Clear();
    for(int i=0;i<n;i++)
        ans.mat[i][i]=1;
    while(b)
    {
        if(b&1)
            ans=mult(ans,m1);
        b>>=1;
        m1=mult(m1,m1);
    }
    return ans;
}

int main()
{
    while(~scanf("%lld%lld",&n,&m))
    {
        Matrix A,B;A.Clear();B.Clear();
        B.mat[0][0]=23;B.mat[n+1][0]=3;
        REPF(i,1,n)  scanf("%lld",&B.mat[i][0]);
        n+=2;//n==5
        for(int i=0;i<n-1;i++)  A.mat[i][0]=10;
        for(int i=0;i<n;i++)  A.mat[i][n-1]=1;
        for(int i=1;i<n-1;i++)
            for(int j=1;j<=i;j++)  A.mat[i][j]=1;
        A=Pow(A,m);
        B=mult(A,B);
        printf("%lld\n",B.mat[n-2][0]);
    }
    return 0;
}


HDU 5015 233 Matrix(矩阵快速幂)

标签:

原文地址:http://blog.csdn.net/u013582254/article/details/44466861

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!