hdu5667

Sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 728    Accepted Submission(s): 238


Problem Description
     Holion August will eat every thing he has found.

     Now there are many foods,but he does not want to eat all of them at once,so he find a sequence.

fn=1,ab,abfcn1fn2,n=1n=2otherwise

     He gives you 5 numbers n,a,b,c,p,and he will eat  fn  foods.But there are only p foods,so you should tell him  fn  mod p.
 

Input
     The first line has a number,T,means testcase.

     Each testcase has 5 numbers,including n,a,b,c,p in a line.

    1T10,1n1018,1a,b,c109 , p  is a prime number,and  p109+7 .
 

Output
     Output one number for each case,which is  fn  mod p.
 

Sample Input
      
      
1 5 3 3 3 233
 

Sample Output
      
      
190
题意:就是给你n,a,b,c,p,按照公式求第n项
构造矩阵求解;
f(n)=a^b*f(n-1)^c*f(n-2),f(2)=a^b,f(1)=1,可以看出每一项都是一个以a为底的数,我们对递推公式两边取log a,变成g(n)=b+c*g(n-1)+g(n-2),然后就可以构造矩阵了;
[ g(n-1),g(n-2),1]*A=[g(n),g(n-1),1],求矩阵A就可以了;构造矩阵详解: 点击打开链接
A=c,1,0
1,0,0
b,0,1
g(n)表示的是指数,[g(n),g(n-1),1] = [g(2),g(1),1]*A^(n-2),我们只需要g(n),在求A^(n-2)时用到快速幂,由于g(n)是指数,而且mod是素数( a^g(n) )%mod =a^(g(n)%(mod-1))%mod,费马小定理!!!!然后单独考虑n=1,2的情况,f(1)=1,f(2)=a^b,直接快速幂;
AC代码:
#include <iostream>
#include <string.h>

using namespace std;
typedef long long LL;
const int MAX=3;
struct  Matrix
{
    LL m[3][3];///必须是3*3,不然不能向下面那样赋值
};
int Mod;
Matrix I= {1,0,0,
           0,1,0,
           0,0,1
          };///单位矩阵
LL quickm(LL a,LL b,LL mod)///快速幂
{
    LL ans=1;
    while(b)
    {
        if(b&1)
            ans=(ans*a)%mod;
        a=(a*a)%mod;
         b >>=1;
    }
    return ans;
}
Matrix matrixmul(Matrix a,Matrix b) ///矩阵乘法
{
    int i,j,k;
    Matrix c;
    for (i = 0 ; i < MAX; i++)
        for (j = 0; j < MAX; j++)
        {
            c.m[i][j] = 0;
            for (k = 0; k < MAX; k++)
                c.m[i][j] += (a.m[i][k]* b.m[k][j])%(Mod-1);
            c.m[i][j] %= (Mod-1);///费马小定理,p是素数,指数对phi(p)取膜
        }
    return c;
}
Matrix quickpow(Matrix m,LL n)///矩阵快速幂
{
    Matrix b = I;
    while (n)
    {
        if (n & 1)
            b = matrixmul(b,m);
        n = n >> 1;
        m = matrixmul(m,m);
    }
    return b;
}
int main()
{
    int t;
    LL a,b,c,n,mod ;
    cin>>t;
    while(t--)
    {
        cin>>n>>a>>b>>c>>mod;
        Mod=mod;
        if(n==1)
        {
            cout<<1<<endl;
            continue;
        }
        if(n==2)
        {
            cout<<quickm(a,b,mod)<<endl;
            continue;
        }
        Matrix A= {c,1,0,
                   1,0,0,
                   1,0,1
                  };
        Matrix temp=quickpow(A,n-2);
        LL ans=(b*temp.m[0][0]+b*temp.m[2][0])%(mod-1);///[b,0,1]* ( A^(n-2))的第一列
        ans=quickm(a,ans,mod);
        cout<<ans<<endl;
    }
    return 0;
}







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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值