Hdu5015 233 Matrix矩阵

每一列能由前一列推过来,构造一个(n+2)*(n+2)的矩阵B,第k列 就等于  所构造的第一列A*B^(k-1)

比如

233                            10    10    10    0           2333

a1+233            *                1      1      0    =     a1+233+2333

a1+a2+233                                1      0           a1+a2+233+a1+233+2333

3                                1      1     1      1           3

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <string.h>
typedef long long LL;
using namespace std;

const LL mod = 10000007;

LL n;
struct Matrix
{
    LL m[15][15];
};

Matrix Mul(Matrix a, Matrix b)
{
    Matrix ans;
    for (LL i = 0; i < n + 2; i++){
        for (LL j = 0; j < n + 2; j++){
            ans.m[i][j] = 0;
            for (LL k = 0; k < n + 2; k++){
                ans.m[i][j] += a.m[i][k] * b.m[k][j];
                ans.m[i][j] %= mod;
            }
        }
    }
    return ans;
}

Matrix quick(Matrix a, LL b)
{
    Matrix ans;
    for (LL i = 0; i < n + 2; i++)
    for (LL j = 0; j < n + 2; j++)
        ans.m[i][j] = (i == j);
    while (b){
        if (b & 1) ans = Mul(ans, a);
        b >>= 1;
        a = Mul(a, a);
    }
    return ans;
}

Matrix init()
{
    Matrix ans;
    for (LL i = 0; i < n + 2; i++)
    for (LL j = 0; j < n + 2; j++)
        ans.m[i][j] = 0;
    for (LL i = 0; i < n + 1; i++)
        ans.m[0][i] = 10;
    for (LL i = 1; i < n + 1; i++){
        for (LL j = i; j < n + 1; j++)
            ans.m[i][j] = 1;
    }
    for (LL i = 0; i < n + 2; i++)
        ans.m[n + 1][i] = 1;
    return ans;
}

int main()
{
    LL a[1000];
    LL k;
    while (cin >> n >> k){
        for (LL i = 1; i < n + 1; i++)
            cin >> a[i];
        a[0] = 233; a[n + 1] = 3;
        for (LL i = 1; i<n + 1; i++)
            a[i] += a[i - 1];
        Matrix gao = init();
        gao = quick(gao, k - 1);
        LL sum = 0;
        for (LL i = 0; i < n + 2; i++){
            sum += a[i] * gao.m[i][n];
            sum %= mod;
        }
        cout << sum << endl;
    }
    return 0;
}
时间: 2024-10-12 19:53:42

Hdu5015 233 Matrix矩阵的相关文章

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

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(矩阵快速幂)

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却非常大,如果

Hdu5015 (233 Matrix)

这题是今年西安网络赛的一题,当时真是想不出来,竟然在推第n行m列的通项公式...后来被学长A掉,说是矩阵快速幂,于是赶紧脑补了一下. 我现在理解的大概就是构造前一个矩阵,一般是一行的,是初始的信息,比如斐波纳契数列的(f[2] f[1]),然后成上一个方阵,这个方阵也需要根据不同的题进行构造,要求就是初始的那个矩阵在经过第二个矩阵的右乘之后可以递推到下一个式子并保持可以更新的状态. 在斐波纳契数列中,右乘的方阵是 ( 1 1 1 0) 乘之后前一个矩阵就变成 (f[3] f[2]) 要求第n项就

233 Matrix(hdu5015 矩阵)

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

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

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