hdu5015---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

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;
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值