现在的位置: 首页 > 综合 > 正文

HDU 2971 Tower 构造矩阵

2017年11月22日 ⁄ 综合 ⁄ 共 2646字 ⁄ 字号 评论关闭

Tower

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2161    Accepted Submission(s): 517

Problem Description
Alan loves to construct the towers of building bricks. His towers consist of many cuboids with square base. All cuboids have the same height h = 1. Alan puts the consecutive cuboids one over another:


Recently in math class, the concept of volume was introduced to Alan. Consequently, he wants to compute the volume of his tower now. The lengths of cuboids bases (from top to bottom) are constructed by Alan in the following way:

1. Length a1 of the first square is one.

2. Next, Alan fixes the length a2 of the second square.

3. Next, Alan calculates the length an (n > 2) by 2*a2*(an-1)-(an-2). Do not ask why he chose such

a formula; let us just say that he is a really peculiar young fellow. For example, if Alan fixes a2 = 2, then a3 = 8 -a1 = 7; see Figure 1. If Alan fixes a2 = 1, then an = 1 holds for all n belong to N; see Figure 2.

Now Alan wonders if he can calculate the volume of tower of N consecutive building bricks. Help Alan and write the program that computes this volume. Since it can be quite large, it is enough to compute the answer modulo given natural number m.

 
Input
The input contains several test cases. The first line contains the number t (t <= 10^5) denoting the number of test cases. Then t test cases follow. Each of them is given in a separate line containing three integers a2,N,m (1 <= a2,m
<= 10^9, 2 <= N <= 10^9) separated by a single space, where a2 denotes the fixed length of second square in step 2, while N denotes the number of bricks constructed by Alan.

Output
For each test case (a2,N,m) compute the volume of tower of N consecutive bricks constructed by Alan according to steps (1-3) and output its remainder modulo m.
 
Sample Input
3 2 3 100 1 4 1000 3 3 1000000000
 
Sample Output
54 4 299
/*
HDOJ 2971 构造矩阵

an=2*a2*(an-1)-(an-2). 求S(n) 
令2*a2=X Y=-1 所以An=X*(An-1)+Y*(An-2) 

S(n)=S(n-1)+A(n)^2,
A(n)^2=X^2*A(n-1)^2+Y^2*A(n-2)^2+2*X*Y*A(n-1)*A(n-2),
A(n)*A(n-1)=X*A(n-1)^2+Y*A(n-1)*A(n-2)
矩阵构造如上图: 
如前面所说  X=2*a2 ,Y=-1 
*/

#include<iostream>
#include<stdio.h>
using namespace std;
typedef __int64 LL;
#define N 4
struct matrix{
	int m[N][N];
};

matrix mat,unit;
int n,a2,M;

void init()
{
	
	memset(unit.m,0,sizeof(unit.m));
	memset(mat.m,0,sizeof(mat.m));
	//初始当n=2时 a1=1 所以右一列为:S1=a1,a2^2,a1^2=1,a2*a1
	unit.m[0][0]=1; 
	unit.m[1][0]=((LL)a2*a2)%M; 
	unit.m[2][0]=1; 
	unit.m[3][0]=a2; 
	
	//X=2*a2 ,Y=-1  都转化为正数 
	mat.m[0][0]=mat.m[0][1]=1;
    mat.m[1][1]=(4ll*a2*a2)%M, mat.m[1][2]=1, mat.m[1][3]=((-4ll*a2)%M+M)%M;
    mat.m[2][1]=1;
    mat.m[3][1]=(2ll*a2)%M,mat.m[3][3]=M-1;
}

matrix mul(matrix a,matrix b)
{
	int i,j,k;
	matrix c;
	for(i=0;i<4;i++)
		for(j=0;j<4;j++)
		{
			c.m[i][j]=0;
			for(k=0;k<4;k++)
				c.m[i][j]=(c.m[i][j]+(LL)a.m[i][k]*b.m[k][j])%M;
		}
	return c;
}

void pow(int n)
{
	for(;n;n>>=1)
	{
		if(n&1)
			unit=mul(mat,unit);
		mat=mul(mat,mat);
	}
}

int main()
{
	int t;
	
	//freopen("test.txt","r",stdin);
	scanf("%d",&t);	
	while(t--)
	{
		scanf("%d%d%d",&a2,&n,&M);
		init();
		pow(n-1);
		printf("%d\n",unit.m[0][0]);
	}
	return 0;
} 

抱歉!评论已关闭.