欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【HDU5015】233 Matrix

程序员文章站 2022-06-04 12:13:06
...

                                        233 Matrix

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

 

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

【HDU5015】233 Matrix

 

 

解析:

       明显矩阵乘法,然而推了半天没推出来,最后看了题解才明白QWQ。。。

       看到n<=10和m<=10^9 应该对矩阵有些想法,现在我们假设要求A[a][b],则A[a][b] = A[a][b-1] + A[a-1][b] = A[a][b-1] + A[a-1][b-1] + A[a-2][b] = ...

       这样相当于右图:【HDU5015】233 Matrix

       红色部分为绿色部分之和,而顶上的绿色部分很好求,左边的绿色部分(最多10个)其实就是:A[1][m-1],A[2][m-1]..A[n][m-1],即对每个1<=i<=n, A[i][m]都可由A[1][m-1],A[2][m-1]..A[n][m-1],于是建立12*12的矩阵:

10 0 0 0 0 1

10 1 0 0 0 1

10 1 1 0 0 1

10 1 1 1 0 1

10 1 1 1 1 1

 0  0 0 0 0 1

       然后就是矩阵快速幂了。

 

代码:

#include <bits/stdc++.h>
using namespace std;

const int mod=10000007;
const int Max=15;
int n,m;
int ans[Max],a[Max][Max];

inline int get_int()
{
	int x=0,f=1;
	char c;
	for(c=getchar();(!isdigit(c))&&(c!='-');c=getchar());
	if(c=='-') f=-1,c=getchar();
	for(;isdigit(c);c=getchar()) x=(x<<3)+(x<<1)+c-'0';
	return x*f;
}
inline void print(int x)
{
	if(x>9) print(x/10);
	putchar('0'+x%10);
}

inline void mul(int ans[Max],int a[Max][Max])
{
	int c[Max];
	memset(c,0,sizeof(c));
	for(int i=0;i<=n+1;i++)
	  for(int k=0;k<=n+1;k++)
	    c[i]=(c[i]%mod+1ll*ans[k]*a[i][k]%mod)%mod;
	memcpy(ans,c,sizeof(c));
}

inline void mulself(int a[Max][Max])
{
	int c[Max][Max];
	memset(c,0,sizeof(c));
	for(int i=0;i<=n+1;i++)
	  for(int j=0;j<=n+1;j++)
	    for(int k=0;k<=n+1;k++)
	      c[i][j]=(c[i][j]%mod+1ll*a[i][k]*a[k][j]%mod)%mod;
	memcpy(a,c,sizeof(c));
}

inline void solve()
{
	int b=m;
	while(b)
	{
	  if(b&1) mul(ans,a);
	  b>>=1;
	  mulself(a);
	}
}

int main()
{
	while(~scanf("%d%d",&n,&m))
	{
	  memset(ans,0,sizeof(ans)),memset(a,0,sizeof(a));
	  ans[0]=23,ans[n+1]=3;
	  for(int i=0;i<=n;i++) a[i][0]=10;
	  for(int j=1;j<=n;j++)
	    for(int i=j;i<=n;i++) a[i][j]=1;
	  for(int i=0;i<=n+1;i++) a[i][n+1]=1;
	  for(int i=1;i<=n;i++) ans[i]=get_int();
	  solve();
	  print(ans[n]),putchar('\n');
	}
	return 0;
}

 

相关标签: HDU 矩阵快速幂