HDU 2971(数论,构造矩阵+矩阵乘法优化)

Tower

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

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:
1
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

 

 

 

先假设a2 = t, 题目给定了递推关系:An = 2 * t * An-1 - An-2 (n > 2),初值A1 = 1, A2 = t;题目要求Sn = An ^ 2 + An-1 ^ 2 + ... + A1 ^ 2。
  反复应用递推关系得到:
  
  然后Sn-1利用相同的方式展开,把4tAn-2An-3约去,得到:
  
  这样就比较容易得出Sn的通项:
  

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值