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 \(a_{1}\) of the first square is one.
  2. Next, Alan fixes the length \(a_{2}\) of the second square.
  3. Next, Alan calculates the length \(a_{n} (n > 2)\) by \(2 \times a2 \times (a_{n-1})-(a_{n-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 \(a_{2} = 2\), then \(a_3 = 8 -a_1 = 7\); see Figure 1. If Alan fixes \(a_2 = 1\), then \(a_n = 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 \le a_2,m \le 10^9, 2 \le N \le 10^9)\) separated by a single space, where \(a_2\) 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 \((a_2,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

SB矩阵乘法。另\(p = 2a_2\)把公式写写$$a_n^2 = p2a_{n-1}2-2pa_{n-1}a_{n-2}$$

\[S_n = S_{n-1}+a_n^2
\]

\[2pa_na_{n-1} = 2p^2a_{n-1}^2-2pa_{n-1}a_{n-2}
\]

然后我们的初始矩阵$$A = (a_n^2 \quad a_{n-1}^2 \quad a_{n-1}a_{n-2} \quad S_{n-1})$$,然后就可以线性递推了。

  1. #include<cstring>
  2. #include<cstdio>
  3. #include<cstdlib>
  4. using namespace std;
  5. typedef long long ll;
  6. int d,N,rhl,T;
  7. struct Matrix
  8. {
  9. int a[4][4],n,m;
  10. inline Matrix() { memset(a,0,sizeof(a)); }
  11. friend inline Matrix operator *(const Matrix &x,const Matrix &y)
  12. {
  13. Matrix ret; ret.n = x.n; ret.m = y.m;
  14. for (int i = 0;i < ret.n;++i)
  15. for (int j = 0;j < ret.m;++j)
  16. for (int k = 0;k < x.m;++k)
  17. {
  18. ret.a[i][j] += (ll)x.a[i][k]*(ll)y.a[k][j]%rhl;
  19. if (ret.a[i][j] >= rhl) ret.a[i][j] -= rhl;
  20. }
  21. return ret;
  22. }
  23. }st,mul,ans;
  24. inline Matrix qsm(Matrix a,int b)
  25. {
  26. Matrix ret; ret.n = ret.m = a.n;
  27. for (int i = 0;i < ret.n;++i) ret.a[i][i] = 1;
  28. for (;b;b >>= 1,a = a*a) if (b & 1) ret = ret*a;
  29. return ret;
  30. }
  31. inline void work()
  32. {
  33. st.n = 1; st.m = 4;
  34. st.a[0][0] = (ll)d*(ll)d%rhl; st.a[0][1] = 1;
  35. st.a[0][2] = d; st.a[0][3] = 1;
  36. mul.n = mul.m = 4;
  37. mul.a[0][0] = (ll)(2*d)*(ll)(2*d)%rhl; mul.a[0][1] = 1; mul.a[0][2] = (2*d)%rhl; mul.a[0][3] = 1;
  38. mul.a[1][0] = 1;
  39. mul.a[2][0] = ((ll)(-4)*(ll)d%rhl)+rhl; mul.a[2][2] = rhl-1;
  40. mul.a[3][3] = 1;
  41. ans = st*qsm(mul,N-1);
  42. printf("%d\n",ans.a[0][3]);
  43. }
  44. int main()
  45. {
  46. freopen("2971.in","r",stdin);
  47. freopen("2971.out","w",stdout);
  48. scanf("%d",&T);
  49. while (T--)
  50. {
  51. scanf("%d %d %d",&d,&N,&rhl);
  52. if (N == 1) puts("1");
  53. else if (N == 2) printf("%d\n",((ll)d*(ll)d+1LL)%rhl);
  54. else work();
  55. }
  56. fclose(stdin); fclose(stdout);
  57. return 0;
  58. }

Hdu 2971 Tower的更多相关文章

  1. hdu 5779 Tower Defence

    题意:考虑由$n$个结点构成的无向图,每条边的长度均为$1$,问有多少种构图方法使得结点$1$与任意其它节点之间的最短距离均不等于$k$(无法到达时距离等于无穷大),输出答案对$1e9+7$取模.$1 ...

  2. hdu 4779 Tower Defense (思维+组合数学)

    Tower Defense Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) ...

  3. 动态规划(树形DP):HDU 5886 Tower Defence

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA2MAAAERCAIAAAB5Jui9AAAgAElEQVR4nOy9a6wsS3YmFL/cEkh4LP

  4. hdu 4779 Tower Defense 2013杭州现场赛

    /** 题意: 有两种塔,重塔,轻塔.每种塔,能攻击他所在的一行和他所在的一列, 轻塔不 能被攻击,而重塔可以被至多一个塔攻击,也就是说重塔只能被重塔攻击.在一个n*m 的矩阵中,最少放一个塔,可放多 ...

  5. HDU 5886 Tower Defence

    树的直径. 比赛的时候想着先树$dp$处理子树上的最长链和次长链,然后再从上到下进行一次$dfs$统计答案,和$CCPC$网络赛那个树$dp$一样,肯定是可以写的,但会很烦.......后来写崩了. ...

  6. HDU 5886 Tower Defence(2016青岛网络赛 I题,树的直径 + DP)

    题目链接  2016 Qingdao Online Problem I 题意  在一棵给定的树上删掉一条边,求剩下两棵树的树的直径中较长那的那个长度的期望,答案乘上$n-1$后输出. 先把原来那棵树的 ...

  7. The Tower HDU - 6559 (解析几何)

    The Tower HDU - 6559 The Tower shows a tall tower perched on the top of a rocky mountain. Lightning ...

  8. dp --- hdu 4939 : Stupid Tower Defense

    Stupid Tower Defense Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/ ...

  9. HDU 4939 Stupid Tower Defense(dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4939 解题报告:一条长度为n的线路,路上的每个单元格可以部署三种塔来给走在这条路上的敌人造成伤害,第一 ...

随机推荐

  1. yum在线升级

    RPM优点 由於 RPM 是透过预先编译并打包成为 RPM 文件格式后,再加以安装的一种方式,并且还能够进行数据库的记载. 所以 RPM 有以下的优点: RPM 内含已经编译过的程序与配置档等数据,可 ...

  2. html笔记04:在html之中导入css两种常见方法

    1.导入式: <html> <head> <title></title> <style type="text/css"> ...

  3. 在Android应用程序使用YouTube API来嵌入视频

    在Android版YouTube播放器API使您可以将视频播放功能到你的Android应用程序.该API允许您加载和播放YouTube视频(和播放列表),并自定义和控制视频播放体验. 您可以加载或暗示 ...

  4. 基于anyrtc的sdk实现直播连麦互动

    基于anyrtc的sdk实现直播连麦互动 前言 1.由于粘贴了较大的代码,造成内容比较长,可能会花费您较长的时间. 2.项目里面没有做权限判断,所以如果发现有页面发生崩溃可能是权限没有打开,请打开权限 ...

  5. iOS之KVO和KVC

    概述 由于ObjC主要基于Smalltalk进行设计,因此它有很多类似于Ruby.Python的动态特性,例如动态类型.动态加载.动态绑定等.今天我们着重介绍ObjC中的键值编码(KVC).键值监听( ...

  6. mysqlbinlog 用法

    操作命令: show binlog events in 'binlog.000016' limit 10; reset master 删除所有的二进制日志 flush logs  产生一个新的binl ...

  7. Eclipse下使用Hadoop单机模式调试MapReduce程序

    在单机模式下Hadoop不会使用HDFS,也不会开启任何Hadoop守护进程,所有程序将在一个JVM上运行并且最多只允许拥有一个reducer 在Eclipse中新创建一个hadoop-test的Ja ...

  8. magento addFieldToFilter()方法常用的过滤条件

    记录一下Magento模型集合Model Collection中addFieldToFilter()方法常用的过滤条件.以下参数也同样适用于产品实体的addAttributeToFilter()方法. ...

  9. HTML5十五大新特性

    HTML5想必大家都很熟悉了.然而,你能准确地说出HTML5带来了哪些新特性吗?本文总结了HTML5带来的15项你必须知道的新特性. 一起来看下: 1.新的文档类型  (New Doctype) 目前 ...

  10. WebSocket 实战

    http://www.ibm.com/developerworks/cn/java/j-lo-WebSocket/ 本文介绍了 HTML5 WebSocket 的由来,运作机制及客户端和服务端的 AP ...