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

hdu5015---233 Matrix(矩阵)

时间:2015-03-19 14:54:01      阅读:168      评论: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

Source
2014 ACM/ICPC Asia Regional Xi’an Online

Recommend

记得这是去年网赛的题,那时候弱爆了不会做
m很大,但是n很小,我们一列列看,
比如前一列为a0 a1 a2 … an
当前是b0 b1 b2 …. bn

b1 = a1 + a0
b2 = a2 + a1 + a0
….

bn = an + an-1 + .. + a1 + a0
n<=10
可以构造一个 (n + 2) * (n + 2)的矩阵,然后快速幂转移状态
注意n=0的情况

/*************************************************************************
    > File Name: hdu5015.cpp
    > Author: ALex
    > Mail: zchao1995@gmail.com 
    > Created Time: 2015年03月19日 星期四 13时00分49秒
 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

const int mod = 10000007;
int n, m;

class MARTIX
{
    public:
        LL mat[15][15];
    public:
        MARTIX();
        MARTIX operator * (const MARTIX &b)const;
        MARTIX& operator = (const MARTIX &b);
};

MARTIX :: MARTIX()
{
    memset (mat, 0, sizeof(mat));
}

MARTIX MARTIX :: operator * (const MARTIX &b)const
{
    MARTIX ret;
    for (int i = 0; i < n + 2; ++i)
    {
        for (int j = 0; j < n + 2; ++j)
        {
            for (int k = 0; k < n + 2; ++k)
            {
                ret.mat[i][j] += this -> mat[i][k] * b.mat[k][j];
                ret.mat[i][j] %= mod;
            }
        }
    }
    return ret;
}

MARTIX& MARTIX :: operator = (const MARTIX &b)
{
    for (int i = 0; i < n + 2; ++i)
    {
        for (int j = 0; j < n + 2; ++j)
        {
            this -> mat[i][j] = b.mat[i][j];
        }
    }
    return *this;
}

void Debug(MARTIX A)
{
    for (int i = 0; i < n + 2; ++i)
    {
        for (int j = 0; j < n + 2; ++j)
        {
            printf("%lld ", A.mat[i][j]);
        }
        printf("\n");
    }
}

MARTIX fastpow(MARTIX A, int cnt)
{
    MARTIX ans;
    for (int i = 0; i < n + 2; ++i)
    {
        ans.mat[i][i] = 1;
    }
    while (cnt)
    {
        if (cnt & 1)
        {
            ans = ans * A;
        }
        cnt >>= 1;
        A = A * A;
    }
    return ans;
}    

int main ()
{
    while (~scanf("%d%d", &n, &m))
    {
        if (!n && !m)
        {
            printf("0\n");
            continue;
        }
        MARTIX A;
        for (int i = 0; i < n + 2; ++i)
        {
            if (i == n)
            {
                break;
            }
            for (int j = 0; j <= i; ++j)
            {
                A.mat[i][j] = 1;
            }
            A.mat[i][n] = 1;
        }
        A.mat[n][n] = 10;
        A.mat[n][n + 1] = 1;
        A.mat[n + 1][n + 1] = 1;
        MARTIX F;
        for (int i = 0; i < n; ++i)
        {
            scanf("%lld", &F.mat[i][0]);
            F.mat[i][0] %= mod;
        }
        F.mat[n][0] = 233;
        F.mat[n + 1][0] = 3;
        if (n)
        {
            A = fastpow(A, m);
            F = A * F;
            printf("%lld\n", F.mat[n - 1][0]);
        }
        else
        {
            A = fastpow(A, m - 1);
            F = A * F;
            printf("%lld\n", F.mat[n][0]);
        }
    }
    return 0;
}

hdu5015---233 Matrix(矩阵)

标签:矩阵

原文地址:http://blog.csdn.net/guard_mine/article/details/44456675

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