hdu 5015 233 Matrix(构造矩阵)

http://acm.hdu.edu.cn/showproblem.php?pid=5015

因为是个二维的递推式,当时没有想到可以这样构造矩阵。从列上看,当前这一列都是由前一列递推得到。根据这一点来构造矩阵。令b[i]代表第i列,是一个(n+2)*1的矩阵,即b[1] = [1,233......],之所以在加了两行,是要从前一个矩阵b[i-1]得到b[i]中的第二个数2333...,再构造一个转换矩阵a,它是一个(n+2)*(n+2)的矩阵,那么a^(m-1) *
b就是第m列。

/*
a矩阵:
1 0 0 0 0...
3 10 0 0 0...
3 10 1 0 0...
3 10 1 1 0...
3 10 1 1 1...
..

b矩阵:第1列

*/
#include <stdio.h>
#include <iostream>
#include <map>
#include <set>
#include <list>
#include <stack>
#include <vector>
#include <math.h>
#include <string.h>
#include <queue>
#include <string>
#include <stdlib.h>
#include <algorithm>
#define LL __int64
//#define LL long long
#define eps 1e-9
#define PI acos(-1.0)
using namespace std;
const int INF = 0x3f3f3f3f;
const int mod = 10000007;

struct matrix
{
	LL mat[15][15];
	void init()
	{
		memset(mat,0,sizeof(mat));
		for(int i = 0; i < 15; i++)
		{
			mat[i][i] = 1;
		}
	}
}a,b,res;

int n,m;

matrix mul(matrix a, matrix b)
{
	matrix ans;
	memset(ans.mat,0,sizeof(ans.mat));
	for(int i = 0; i < n+2; i++)
	{
		for(int k = 0; k < n+2; k++)
		{
			if(a.mat[i][k] == 0)
				continue;
			for(int j = 0; j < n+2; j++)
			{
				ans.mat[i][j] += (a.mat[i][k] * b.mat[k][j])%mod;
				ans.mat[i][j] %= mod;
			}
		}
	}
	return ans;
}

matrix pow(matrix a, int n)
{
	matrix ans;
	ans.init();
	while(n)
	{
		if(n&1)
			ans = mul(ans,a);
		n >>= 1;
		a = mul(a,a);
	}
	return ans;
}

int main()
{
	int x;
	while(~scanf("%d %d",&n,&m))
	{
		memset(b.mat,0,sizeof(b.mat));
		b.mat[0][0] = 1;
		b.mat[1][0] = 233;
		for(int i = 2; i < n+2; i++)
		{
			scanf("%d",&x);
			b.mat[i][0] = (b.mat[i-1][0] + x%mod)%mod;
		}

		memset(a.mat,0,sizeof(a.mat));
		a.mat[0][0] = 1;
		for(int i = 1; i < n+2; i++)
		{
			a.mat[i][0] = 3;
			a.mat[i][1] = 10;
			for(int j = 2; j <= i; j++)
				a.mat[i][j] = 1;
		}
		res = pow(a,m-1);
		LL anw = 0;
		for(int i = 0; i < n+2; i++)
		{
			anw += (res.mat[n+1][i] * b.mat[i][0])%mod;
			anw %= mod;
		}
		printf("%I64d\n",anw);
	}
	return 0;
}
时间: 2024-08-08 09:37:57

hdu 5015 233 Matrix(构造矩阵)的相关文章

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

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(西安网络赛I题)

HDU 5015 233 Matrix 题目链接 思路:矩阵快速幂,观察没一列,第一个和为左边加最上面,第二个可以拆为左边2个加最上面,第三个可以拆为为左边3个加最上面,这样其实只要把每一列和每一列右边那列的233构造出一个矩阵,进行矩阵快速幂即可 代码: #include <cstdio> #include <cstring> typedef long long ll; const int N = 15; const int MOD = 10000007; int n, m; s

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 (构造矩阵)

题意:给出矩阵的第0行(233,2333,23333,...)和第0列a1,a2,...an(n<=10,m<=10^9),给出递推式: A[i][j] = A[i-1][j] + A[i][j-1],要求A[n][m]. 数据范围:n,m(n ≤ 10,m ≤ 109). 思路:因为m ≤ 109 显然是要找到列与列的递推关系,用logn幂加速. 从递推式可以得知:a[i][j]可以由a[1...i][j-1] 递推得到,所以构造递推矩阵实现a[1...1][j-1]向a[1...1][j]

hdu 5015 233 Matrix(西安网络赛1009)【构造矩阵】

说起这题简直醉了..当时愣是没想到该怎么做,搞了好久,虽然有想过构造矩阵,但是没仔细想下去. 此题构造两个矩阵,假设a[]数组为题目给出的数据,最多有10个元素,我们可以构造一个矩阵A: a={a[1],a[2],a[3],...a[n],23,3}  大小为1*(n+2) 要得到题目需要的计算结果,那么在构造一个矩阵B,大小为(n+2)*(n+2):(假设n=3) b=   1    1    1    0    0 0    1    1    0    0 0    0    1    0

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

[矩阵快速幂] 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    

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