西安邀请赛 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
比赛时一直在想怎么减少时间复杂度,以我朴素的代码很定会超内存。。。结果学长就过了。学长的代码果然高大上,只用了不到300k。学习!
//#include<CSpreadSheet.h>

#include<iostream>
#include<cmath>
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<string>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<ctime>
#include<bitset>
#include<cmath>
#define eps 1e-6
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define ll __int64
#define LL long long
#define lson l,m,(rt<<1)
#define rson m+1,r,(rt<<1)|1
#define M 10000007
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;

#define Maxn 15

int n,m;

struct Mar
{
    int row,col;
    ll s[Maxn][Maxn];

    void init(int a,int b)
    {
        row=a,col=b;
        memset(s,0,sizeof(s));
    }
};

Mar operator * (const Mar &a,const Mar &b)
{
    Mar res;
    res.init(a.row,b.col);

    for(int k=1;k<=a.col;k++)
    {
        for(int i=1;i<=res.row;i++)
        {
            if(a.s[i][k]==0)
                continue;
            for(int j=1;j<=res.col;j++)
            {
                if(b.s[k][j]==0)
                    continue;
                res.s[i][j]=(a.s[i][k]*b.s[k][j]+res.s[i][j])%M;
            }
        }
    }
    return res;
}

int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        if(!n&&!m)
        {
            printf("0\n");
            continue;
        }
        Mar ba;
        ba.init(n+2,n+2);

        ba.s[1][1]=10;
        ba.s[1][n+2]=3;
        for(int i=2;i<=n+1;i++)
            for(int j=1;j<=i;j++)
                ba.s[i][j]=1;
        ba.s[n+2][n+2]=1;

        Mar ans;
        ans.init(n+2,1);
        ans.s[1][1]=233;
        for(int i=2;i<=n+1;i++)
            scanf("%I64d",&ans.s[i][1]);
        ans.s[n+2][1]=1;

        int k=m;
        while(k>0)
        {
            if(k&1)
                ans=ba*ans;
            k>>=1;
            ba=ba*ba;
        }
        if(n==0)
            printf("%I64d\n",ans.s[n+1][1]/10);
        else
        printf("%I64d\n",ans.s[n+1][1]);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值