HDOJ 233 Matrix 5015【矩阵快速幂】

233 Matrix

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 1355    Accepted Submission(s): 806

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

hujie   |   We have carefully selected several similar problems for you:  5390 5389 5388 5387 5386

题意:求矩阵anm的元素,第一行0,233,2333,23333,。。。。然后给n个元素代表第i行第一列,ai0 元素.....   aij
= ai-1,j+ai,j-1 ....就根据这个公式算出所有元素。然后输出anm

解题分析:

此题一看就是裸快速幂~因为m比较大。开始的时候是想直接推算出整个矩阵,后来发现推不出来。然后就得想到推下一列。就是有第一列推第二列,第二列推第三列。。。。等等。。

我们先写几个元素

a[1]

a[2]

a[3]

想到有233~得出来这个数啊。

23

a[1]

a[2]

a[3]

3

推下一列有:

23*10+3

a[1]+23*10+3

a[2]+a[1]+23*10+3

a[3]+a[2]+a[1]+23*10+3

3

然后转移矩阵A就可以推出来了:

10 0 0 0 1

10 1 0 0 1

10 1 1 0 1

10 1 1 1 1

0  0 0 0 1

然后对A求快速幂,再乘以a数列就可以了

#include <stdio.h>
#include <math.h>
#include <vector>
#include <queue>
#include <string>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>

using namespace std;

typedef long long LL;
const int Modn = 10000007;
const int MAXN = 15;
LL res[MAXN][MAXN];
int N;

void Matmul(LL X[MAXN][MAXN],LL Y[MAXN][MAXN])
{
    LL t[MAXN][MAXN]={0};
    for(int i=0;i<N;i++)
        for(int k=0;k<N;k++)
            if(X[i][k])
            for(int j=0;j<N;j++)
            t[i][j]=(t[i][j]+X[i][k]*Y[k][j]%Modn)%Modn;
    for(int i=0;i<N;i++)
        for(int j=0;j<N;j++)
            X[i][j]=t[i][j];
}

void Matrix(LL X[MAXN][MAXN],LL n)
{
    for(int i=0;i<N;i++)
        for(int j=0;j<N;j++)
        res[i][j]=(i==j);
    while(n){
        if(n&1) Matmul(res,X);
        Matmul(X,X);
        n>>=1;
    }
}

int main()
{
    LL n;
    LL m;
    while(scanf("%lld%lld",&n,&m)!=EOF){
        LL a[MAXN];
        LL A[MAXN][MAXN];
        memset(A,0,sizeof(A));
        N=n+2;
        for(int i=1;i<=n;i++)
            scanf("%lld",&a[i]);
        a[0]=23;a[n+1]=3;
        for(int i=0;i<N;i++){
            if(i!=N-1)A[i][0]=10;
            for(int j=1;j<=i;j++){
                if(i!=N-1)A[i][j]=1;
            }
            A[i][N-1]=1;
        }
        Matrix(A,m);
        LL ans=0;
        //for(int i=0;i<N;i++)
        for(int j=0;j<N;j++){
                ans=(ans+a[j]*res[N-2][j]%Modn)%Modn;
        }
        printf("%lld\n",ans);
    }
    return 0;
}

233 Matrix

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 1355    Accepted Submission(s): 806

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

hujie   |   We have carefully selected several similar problems for you:  5390 5389 5388 5387 5386

版权声明:本文为博主原创文章,转载请注明出处。

时间: 2025-01-04 06:02:42

HDOJ 233 Matrix 5015【矩阵快速幂】的相关文章

hdu 5015 233 Matrix (矩阵快速幂)

233 Matrix Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 749    Accepted Submission(s): 453 Problem Description In our daily life we often use 233 to express our feelings. Actually, we may s

ACM学习历程——HDU5015 233 Matrix(矩阵快速幂)(2014陕西网赛)

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, 233

hdu 4965 Fast Matrix Calculation(矩阵快速幂)

题目链接:hdu 4965 Fast Matrix Calculation 题目大意:给定两个矩阵A,B,分别为N*K和K*N: 矩阵C = A*B 矩阵M=CN?N 将矩阵M中的所有元素取模6,得到新矩阵M' 计算矩阵M'中所有元素的和 解题思路:因为矩阵C为N*N的矩阵,N最大为1000,就算用快速幂也超时,但是因为C = A*B, 所以CN?N=ABAB-AB=AC′N?N?1B,C' = B*A, 为K*K的矩阵,K最大为6,完全可以接受. #include <cstdio> #inc

HDU 4965 Fast Matrix Calculation (矩阵快速幂取模----矩阵相乘满足结合律)

http://acm.hdu.edu.cn/showproblem.php?pid=4965 利用相乘的可结合性先算B*A,得到6*6的矩阵,利用矩阵快速幂取模即可水过. 1 #include<iostream> 2 #include<stdio.h> 3 #include<iostream> 4 #include<stdio.h> 5 #define N 1010 6 #define M 1010 7 #define K 6 8 using namespa

hdoj 4062 Queuing 【矩阵快速幂优化递推公式】

Queuing Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3609    Accepted Submission(s): 1629 Problem Description Queues and Priority Queues are data structures which are known to most computer

HDOJ 2294 - Pendant(DP+矩阵快速幂)

题目链接 题意:有个高富帅,要送个很装逼的吊坠给他女朋友.他有k种珠子,然后要串成一个珠子个数小于等于n 的链子(k种珠子都必须要用到).输入n和k,输出他可以做出多少种不一样的项链. 思路:可以想到递推式f(x, y) = f(x – 1, y) * y + f(x – 1, y – 1) * (k – y + 1)(表示x个珠子用了y种类型),因为n过大,无法直接求出来,所以要使用矩阵快速幂. GG队友的思路 代码: #include <iostream> #include <cst

HDU4965-Fast Matrix Calculation(矩阵快速幂)

题目链接 题意:n*k的矩阵A和一个k*n的矩阵B,C = A * B.求M = (C)^(n * n)时,矩阵M中每个元素的和(每个元素都要MOD6) 思路:因为n最大到1000,所以不能直接用矩阵快速幂求AB的n*n次幂,但是可以将公式稍微转换下,M = AB * AB...* AB = A * (BA) *... * (BA) * B,这样BA的n*n -1次幂就能用矩阵快速幂求解,之后再分别乘以A,B即可. 代码: #include <iostream> #include <cs

hdu 5015 233 Matrix (矩阵高速幂)

233 Matrix Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 749    Accepted Submission(s): 453 Problem Description In our daily life we often use 233 to express our feelings. Actually, we may s

[矩阵快速幂] hdu 5015 233 Matrix

之前各种犯傻 推了好久这个东西.. 后来灵关一闪  就搞定了.. 矩阵的题目,就是构造矩阵比较难想! 题意:给出一个矩阵的第一列和第一行(下标从0开始),(0,0)位置为0, 第一行为,233,2333,23333...一次加个3, 第一列为输入的n个数. 然后从(1,1)位置开始,等于上面的数加左边的数,问(n+1,m+1)的数是多少,也就是右下角的数 思路: 把矩阵画出来: |   0     233   2333  | |  b0     b1     b2     | |  c0