HDU 2971解题报告

Tower

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


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
Hint
 

Source
 

Recommend
lcy   |   We have carefully selected several similar problems for you:   2294  3117  2254  2256  2974 

       这道题是比较基础的矩阵快速幂,但是卡了常数。实质上还是求解(A(1))^2+(A(2))^2+……+(A(n))^2的值。这个与HDU 3306的求解结果是一样的。

http://blog.csdn.net/u012294939/article/details/43984417

不过需要注意的就是矩阵中有负数,取模的时候一定要将负数的取模结果变成正数再进行计算。还有为了防止超时,要减少取模的次数。所以在快速幂之前就应该对矩阵中的数进行取模,将负数的取模结果变成正数,这样在快速幂的矩阵乘法中就只需考虑正数相乘。不需要再加mod然后再模mod了。这样便减少了取模次数。

      参考代码:

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<ctime>
#include<cstdlib>
#include<iomanip>
#include<utility>
#define pb push_back
#define mp make_pair
#define CLR(x) memset(x,0,sizeof(x))
#define _CLR(x) memset(x,-1,sizeof(x))
#define REP(i,n) for(int i=0;i<n;i++)
#define Debug(x) cout<<#x<<"="<<x<<" "<<endl
#define REP(i,l,r) for(int i=l;i<=r;i++)
#define rep(i,l,r) for(int i=l;i<r;i++)
#define RREP(i,l,r) for(int i=l;i>=r;i--)
#define rrep(i,l,r) for(int i=1;i>r;i--)
#define read(x) scanf("%d",&x)
#define put(x) printf("%d\n",x)
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<11
using namespace std;

struct mat
{
    ll d[4][4];
}A,B,E;

int t;
ll a2,n,m;

mat multi(mat &a,mat &b)
{
    mat ans;
    rep(i,0,4)
    {
        rep(j,0,4)
        {
            ans.d[i][j]=0;
            rep(k,0,4)
                if(a.d[i][k]&&b.d[k][j])
                    ans.d[i][j]=(ans.d[i][j]+a.d[i][k]*b.d[k][j])%m;
        }
    }
    return ans;
}

mat quickmulti(mat &a,ll n)
{
    mat ans=E;
    while(n)
    {
        if(n&1)
        {
            n--;
            ans=multi(ans,a);
        }
        else
        {
            n>>=1;
            a=multi(a,a);
        }
    }
    return ans;
}

int main()
{
   CLR(E.d);
   rep(i,0,4)
      E.d[i][i]=1;
   read(t);
   while(t--)
   {
       scanf("%I64d%I64d%I64d",&a2,&n,&m);
       if(n==1)
       {
           printf("1\n");
           continue;
       }
       if(n==2)
       {
           printf("%d\n",(1+a2*a2)%m);
           continue;
       }
       if(a2==1)
       {
           printf("%d\n",n%m);
           continue;
       }
       CLR(A.d);
       A.d[0][0]=1,A.d[0][1]=(4*((a2*a2)%m))%m,A.d[0][2]=((-4*a2)%m+m)%m,A.d[0][3]=1;
       A.d[1][1]=A.d[0][1],A.d[1][2]=A.d[0][2],A.d[1][3]=A.d[0][3];
       A.d[2][1]=(2*a2)%m,A.d[2][2]=-1+m;
       A.d[3][1]=1;
       CLR(B.d);
       B.d[0][0]=(1+a2*a2)%m,B.d[1][0]=(a2*a2)%m,B.d[2][0]=a2%m,B.d[3][0]=1;
       mat ans=quickmulti(A,n-2);
       ans=multi(ans,B);
       printf("%I64d\n",ans.d[0][0]);
   }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值