矩阵快速幂(233递推)

233 Matrix

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 4591    Accepted Submission(s): 2575


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:   6667  6666  6665  6664  6663 
 
这道题的主要思路还是:一列一列递推过去
以n=3为例的矩阵
a1,1    1 0 0 1 0    a1,0
a2,1    1 1 0 1 0    a2,0
a3,1    1 1 1 1 0    a3,0
2333      0 0 0 10 3    233
1       0 0 0 0 1    1
 
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <algorithm>
#include <iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include <stdio.h>
#include <string.h>
#include <vector>
#define ME(x , y) memset(x , y , sizeof(x))
#define SF(n) scanf("%d" , &n)
#define rep(i , n) for(int i = 0 ; i < n ; i ++)
#define INF  0x3f3f3f3f
#define mod 10000007
using namespace std;
typedef long long ll ;
ll n , m ;
struct node{
    ll a[19][19];
    node()
    {
        memset(a , 0 , sizeof(a));
    }
};

node mul(node A , node B)
{
    node C ;
    for(int i = 0 ; i < n + 2 ; i++)
    {
        for(int j = 0 ; j < n + 2 ; j++)
        {
            for(int k = 0 ; k < n + 2 ; k++)
            {
                C.a[i][j] = (C.a[i][j] + A.a[i][k]*B.a[k][j]) % mod;
            }
        }
    }
    return C ;
}

node pow1(node A , ll t)
{
    node ans ;
    for(int i = 0 ; i < n + 2; i++)
        ans.a[i][i] = 1 ;
    while(t)
    {
        if(t&1)
        {
            ans = mul(ans , A);
        }
        t >>= 1 ;
        A = mul(A , A);
    }
    return ans ;
}

int main()
{
    while(~scanf("%d%d" , &n , &m))
    {
        node A , B , C ;

        for(int i = 0 ; i  < n ; i++)
        {
            for(int j = 0 ; j < n + 2 ; j++)
            {
                if(j == n)
                    A.a[i][j] = 1 ;
                if(j <= i)
                    A.a[i][j] = 1 ;
            }
        }
        A.a[n][n] = 10 , A.a[n][n + 1] = 3 ;
        A.a[n + 1][n + 1] = 1 ;


        for(int i = 0 ; i < n ; i++)
        {
            for(int j = 0 ; j < n ; j++)
            {
                if(j == 0)
                    scanf("%lld" , &B.a[i][j]);
            }
        }
        B.a[n][0] = 233 ;
        B.a[n+1][0] = 1 ;

        C = mul(pow1(A , m) , B);
        printf("%lld\n" , C.a[n - 1][0]);

    }


    return 0 ;
}

猜你喜欢

转载自www.cnblogs.com/nonames/p/11360560.html