hdu 5015 233 Matrix 2014年西安网络赛I题

本文介绍了一种特殊的矩阵构造方法——233Matrix,并通过矩阵快速幂求解特定位置的元素值。利用12×12大小的矩阵进行辅助计算,有效地解决了数据溢出的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

233 Matrix

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


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
唉,比赛时候没能写出来。
其实是一道构造矩阵的水题。
可以构造12*12的矩阵(第一二行起辅助作用)
c=
[1 0 0 0 0 0 0 0 0 0 0 0
1 10 0 0 0 0 0 0 0 0 0 0
1 10 1 0 0 0 0 0 0 0 0 0
1 10 1 1 0 0 0 0 0 0 0 0
1 10 1 1 1 0 0 0 0 0 0 0
1 10 1 1 1 1 0 0 0 0 0 0
1 10 1 1 1 1 1 0 0 0 0 0
1 10 1 1 1 1 1 1 0 0 0 0
1 10 1 1 1 1 1 1 1 0 0 0
1 10 1 1 1 1 1 1 1 1 0 0
1 10 1 1 1 1 1 1 1 1 1 0
1 10 1 1 1 1 1 1 1 1 1 1 ]
构造第0列的矩阵(其中第一,二行起辅助作用,第三行开始为实际数据)为a=
[
3
23
a1
a2
a3
.
.
.
.
.
.
an]
由以上矩阵,c^1*a可以构造出第1列,c^2*a可以构造出第二列,c^m*a可以构造出第m列。
其中第m列中的第n行就是我们想要的最后答案.
那么如何求c^m*a%M(其中M=10000007)呢?
c^m*a%M=((c^m%M)*(a%M))%M;
c^m%M可以用矩阵快速幂很容易求得。
过程中为了避免数据溢出,要用long long 型哦!
具体AC的代码如下:
#include<iostream>
#include<algorithm>
#define LL long long
#define L 12
#define M 10000007
using namespace std;
struct Mtraix{ LL m[L][L];};
Mtraix mul(Mtraix a,Mtraix b)
{
    Mtraix c;
    fill(&c.m[0][0],&c.m[L][0],0);
    for(int i=0;i<L;i++)
        for(int j=0;j<L;j++)
            for(int k=0;k<L;k++)
            c.m[i][j]=(c.m[i][j]+(a.m[i][k]*b.m[k][j])%M)%M;
    return c;
}
Mtraix quick_mod(Mtraix a,int b)
{
    Mtraix c;
    fill(&c.m[0][0],&c.m[L][0],0);
    for(int i=0;i<L;i++)
        for(int j=0;j<L;j++)
            if(i==j) c.m[i][j]=1;
    while(b)
    {
        if(b&1) c=mul(a,c);
        b>>=1;
        a=mul(a,a);
    }
    return c;
}
int main()
{
    LL n,m;
    while(cin>>n>>m)
    {
        Mtraix c,t,a;
        fill(&c.m[0][0],&c.m[L][0],0);
        for(int i=0;i<L;i++)
            for(int j=0;j<=i;j++)
            {
                if(i==0) {c.m[i][j]=1;break;}
                if(j==1) c.m[i][j]=10;
                else c.m[i][j]=1;
            }
        fill(&a.m[0][0],&a.m[L][0],0);
        a.m[0][0]=3;a.m[1][0]=23;
        for(int i=2;i<=n+1;i++) cin>>a.m[i][0];
        t=quick_mod(c,m);
        t=mul(t,a);
        cout<<t.m[n+1][0]<<endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值