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, 2333, 23333... (it means a 0,1 = 233,a 0,2 = 2333,a 0,3 = 23333...) Besides, in 233 matrix, we got a i,j = a i-1,j +a i,j-1( i,j ≠ 0). Now you have known a 1,0,a 2,0,...,a n,0, could you tell me a n,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 ≤ 10 9). The second line contains n integers, a 1,0,a 2,0,...,a n,0(0 ≤ a i,0 < 2 31).

Output

For each case, output a n,m mod 10000007.

Sample Input

1 1
1
2 2
0 0
3 7
23 47 16

Sample Output

234
2799
72937

Hint

这个题目由于m数据范围很大,故不能直接暴力计算。此处采用矩阵乘法,由矩阵乘法可以由每一列得到下一列。然后矩阵的乘法使用快速幂加快计算。

由2333可以由233乘10加3,于是打算构造n+2行的方阵。

大致如下:

10 0 0 0 ……0 1

10 1 0 0 ……0 1

10 1 1 0 ……0 1

……

10 1 1 1 ……1 1

0   0 0 0 ……0 1

而所要求的列矩阵大致如下:

23……3

a 1,0

a 2,0

……

a n,0

3

递推的正确性可以通过计算验证

此处矩阵通过结构体,运算符重载完成。

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <string>
#define inf 0x3fffffff
#define esp 1e-10
#define N 10000007
#define LL long long

using namespace std;

struct Mat
{
    LL val[15][15];
    int len;

    Mat operator = (const Mat& a)
    {
        for (int i = 0; i < len; ++i)
            for (int j = 0; j < len; ++j)
                val[i][j] = a.val[i][j];
        len = a.len;
        return *this;
    }

    Mat operator * (const Mat& a)
    {
        Mat x;
        memset(x.val, 0, sizeof(x.val));
        x.len = len;
        for (int i = 0; i < len; ++i)
            for (int j = 0; j < len; ++j)
                for (int k = 0; k < len; ++k)
                    if (val[i][k] && a.val[k][j])
                        x.val[i][j] = (x.val[i][j] + (val[i][k]*a.val[k][j])%N)%N;
        return x;
    }

    Mat operator ^ (const int& a)
    {
        int n = a;
        Mat x, p = *this;
        memset(x.val, 0, sizeof(x.val));
        x.len = len;
        for (int i = 0; i < len; ++i)
            x.val[i][i] = 1;
        while (n)
        {
            if (n & 1)
                x = x * p;
            p = p * p;
            n >>= 1;
        }
        return x;
    }
};

int n, m;
LL a[15], ans;

void Make(Mat &p)
{
    p.len = n + 2;
    memset(p.val, 0, sizeof(p.val));
    for (int i = 0; i <= n; ++i)
        p.val[i][0] = 10;
    for (int i = 0; i <= n+1; ++i)
        p.val[i][n+1] = 1;
    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= i; ++j)
            p.val[i][j] = 1;
}

int main()
{
    //freopen("test.txt", "r", stdin);
    while (scanf("%d%d", &n, &m) != EOF)
    {
        Mat p;
        Make(p);
        p = p ^ m;
        a[0] = 23;
        a[n+1] = 3;
        for (int i = 1; i <= n; ++i)
            scanf("%I64d", &a[i]);
        ans = 0;
        for (int i = 0; i <= n+1; ++i)
            ans = (ans + (p.val[n][i]*a[i])%N)%N;
        printf("%I64d\n", ans);
    }
    return 0;
}
时间: 2024-12-27 01:22:22

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

ACM学习历程——HDU5017 Ellipsoid(模拟退火)(2014西安网赛K题)

---恢复内容开始--- Description Given a 3-dimension ellipsoid(椭球面) your task is to find the minimal distance between the original point (0,0,0) and points on the ellipsoid. The distance between two points (x 1,y 1,z 1) and (x 2,y 2,z 2) is defined as  Input

233 Matrix 矩阵快速幂

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

HDU 5015 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

hdu 5015 233 Matrix (矩阵快速幂)

题意: 有一种矩阵,它的第一行是这样一些数:a  0,0 = 0, a 0,1 = 233,a 0,2 = 2333,a 0,3 = 23333... 除此之外,在这个矩阵里, 我们有 a i,j = a i-1,j +a i,j-1( i,j ≠ 0).现在给你 a 1,0,a 2,0,...,a n,0, 你能告诉我a n,m 是多少吗? n,m(n ≤ 10,m ≤ 10 9)输出 a n,m mod 10000007. 思路:首先我们观察n和m的取值范围,会发现n非常小而m却非常大,如果

ACM学习历程—HDU 5025 Saving Tang Monk(广州赛区网赛)(bfs)

Problem Description <Journey to the West>(also <Monkey>) is one of the Four Great Classical Novels of Chinese literature. It was written by Wu Cheng'en during the Ming Dynasty. In this novel, Monkey King Sun Wukong, pig Zhu Bajie and Sha Wujin

ACM学习历程—HDU 5446 Unknown Treasure(数论)(2015长春网赛1010题)

Problem Description On the way to the next secret treasure hiding place, the mathematician discovered a cave unknown to the map. The mathematician entered the cave because it is there. Somewhere deep in the cave, she found a treasure chest with a com

FZU 1911 Construct a Matrix ( 矩阵快速幂 + 构造 )

FZU 1911 Construct a Matrix (  矩阵快速幂 + 构造 ) 题意: 需要构造一个矩阵满足如下要求: 1.矩阵是一个S(N)*S(N)的方阵 2.S(N)代表斐波那契数列的前N项和模上M 3.矩阵只能由1, 0, -1组成 4.矩阵每行每列的和不能相等 Here, the Fibonacci numbers are the numbers in the following sequence: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 1

Uva 11149 - Power of Matrix ( 矩阵快速幂 )

Uva 11149 -Power of Matrix ( 矩阵快速幂 ) #include <cstdio> #include <cstring> #define CLR( a, b ) memset( a, b, sizeof(a) ) #define MOD 10 #define MAX_SIZE 40 struct Mat { int r, c; int mat[MAX_SIZE][MAX_SIZE]; Mat( int _r = 0 , int _c = 0 ) { CLR

ACM学习历程—HDU5490 Simple Matrix (数学 &amp;&amp; 逆元 &amp;&amp; 快速幂) (2015合肥网赛07)

Problem Description As we know, sequence in the form of an=a1+(n−1)d is called arithmetic progression and sequence in the form of bn=b1qn−1(q>1,b1≠0) is called geometric progression. Huazheng wants to use these two simple sequences to generate a simp