Hdu 2971 Tower

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 = p^2a_{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;
}

转载于:https://www.cnblogs.com/mmlz/p/6059671.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值