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})$$,然后就可以线性递推了。

#include<cstring>
#include<cstdio>
#include<cstdlib>
using namespace std; typedef long long ll;
int d,N,rhl,T; struct Matrix
{
int a[4][4],n,m;
inline Matrix() { memset(a,0,sizeof(a)); }
friend inline Matrix operator *(const Matrix &x,const Matrix &y)
{
Matrix ret; ret.n = x.n; ret.m = y.m;
for (int i = 0;i < ret.n;++i)
for (int j = 0;j < ret.m;++j)
for (int k = 0;k < x.m;++k)
{
ret.a[i][j] += (ll)x.a[i][k]*(ll)y.a[k][j]%rhl;
if (ret.a[i][j] >= rhl) ret.a[i][j] -= rhl;
}
return ret;
}
}st,mul,ans; inline Matrix qsm(Matrix a,int b)
{
Matrix ret; ret.n = ret.m = a.n;
for (int i = 0;i < ret.n;++i) ret.a[i][i] = 1;
for (;b;b >>= 1,a = a*a) if (b & 1) ret = ret*a;
return ret;
} inline void work()
{
st.n = 1; st.m = 4;
st.a[0][0] = (ll)d*(ll)d%rhl; st.a[0][1] = 1;
st.a[0][2] = d; st.a[0][3] = 1; mul.n = mul.m = 4;
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;
mul.a[1][0] = 1;
mul.a[2][0] = ((ll)(-4)*(ll)d%rhl)+rhl; mul.a[2][2] = rhl-1;
mul.a[3][3] = 1; ans = st*qsm(mul,N-1);
printf("%d\n",ans.a[0][3]);
} int main()
{
freopen("2971.in","r",stdin);
freopen("2971.out","w",stdout);
scanf("%d",&T);
while (T--)
{
scanf("%d %d %d",&d,&N,&rhl);
if (N == 1) puts("1");
else if (N == 2) printf("%d\n",((ll)d*(ll)d+1LL)%rhl);
else work();
}
fclose(stdin); fclose(stdout);
return 0;
}

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. Asp.Net MVC 实用视频教程

    [北盟学习BaMn.Cn] Asp.Net MVC 第01课--创建第一个项目.avi [北盟学习BaMn.Cn] Asp.Net MVC 第02课--自己建一个controller view.avi ...

  2. UISenior之数据的本地化持久化

    数据的本地化主要分为两个方面:1.简单数据的本地持久化(NSString.NSArray.NSDictionary.NSData)2.复杂数据的本地持久化(本文以Person类为例) 简单对象的本地化 ...

  3. ReadWriteLock与ReentrantReadWriteLock

    JAVA的JUC包中的锁包括"独占锁"和"共享锁".JUC中的共享锁有:CountDownLatch.CyclicBarrier.Semaphore.Reent ...

  4. UWP入门一 7天酒店客户端源码及说明

    以前写过一个wp8的工程,说实话那会的代码很麻烦,写起来费劲,另外还没多少人下载,不过到windows 10 开始微软出了UWP架构以后一切都像以前的winform wpf一样好转起来,新建一个工程以 ...

  5. (转)OpenVPN使用HTTP代理连接服务器

    原文地址:http://www.365mini.com/page/18.htm 在一些公司或者其他受限的网络环境中,使用的是HTTP代理服务器上网.在这种情况下,使用OpenVPN客户端可能无法连接服 ...

  6. 处理 eclipse 导入报错 Invalid project description,问题

    有时候在添加工程时,会出现如图所示的错误信息, ,提示显示将要添加的工程已经存在,但是在工作空间里却找不到,这个时候,要做就是, 在导入的时候选择General->Existing Projec ...

  7. sharedPreference的奇怪bug

    一定要清楚sp的结构,而且要知道是什么类型的.类型不对,会引起很多不知道的bug,比如本来是int类型的值,如果用String的类型去匹配,会让Activity开Activity Thread,不断地 ...

  8. Gerrit 删除项目

    今天手滑把一个Gerrit上的项目epa写成了epp,想找个重命名的地方也找不到...到网络上搜索了下,发现都是改数据库的,然后就进入的数据库: $ ssh -p 29418 10.27.149.22 ...

  9. VS2010 测试 -普通单元测试

    http://www.cnblogs.com/rhythmK/archive/2012/04/20/2458832.html

  10. Android版本分布——2016年10月更新

    Code Name Version API Level Distribution frogy(冻酸奶) 2.2.x 8 0.1% gingerbread(姜饼) 2.3.3——2.3.7 10 2.0 ...