多写几项就可以发现每一项都是a的次数有关,然后可以构造一个矩阵,求他的快速幂,这个过程中可以用欧拉函数降幂,即中途对p-1取模。
然后还有一个细节,如果a%p==0的话,要特判为0。
/* ***********************************************
Author :Maltub
Email :xiang578@foxmail.com
Blog :htttp://www.xiang578.top
************************************************ */
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
//#include <bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<n;i++)
#define per(i,a,n) for(int i=n-1;i>=a;i--)
#define pb push_back
using namespace std;
typedef vector<int> VI;
typedef long long ll;
const ll mod=1000000007;
const int N=2048;
ll n,a,b,c,p;
struct node
{
ll mat[3][3];
}A,B;
node operator *(const node &n1,const node &n2)
{
node c;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
c.mat[i][j]=0;
for(int k=0;k<3;k++)
{
c.mat[i][j]=(c.mat[i][j]+n1.mat[i][k]*n2.mat[k][j]%(p-1))%(p-1);
}
}
}
return c;
}
node fst(node n1,ll x)
{
node c;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
c.mat[i][j]=(i==j);
}
}
while(x)
{
if(x&1) c=c*n1;
n1=n1*n1;
x>>=1;
}
return c;
}
ll pow_mod(ll x,ll y)
{
ll ans=1;
for(y;y;y>>=1)
{
if(y&1) ans=ans*x%p;
x=x*x%p;
}
return ans;
}
int main()
{
int _;
scanf("%d",&_);
while(_--)
{
cin>>n>>a>>b>>c>>p;
if(n==1)
{
printf("1\n");
continue;
}
if(a%p==0)
{
printf("0\n");
continue;
}
A.mat[0][0]=b;A.mat[1][0]=0;A.mat[2][0]=b;
B.mat[0][0]=c;B.mat[0][1]=1;B.mat[0][2]=1;
B.mat[1][0]=1;B.mat[1][1]=0;B.mat[1][2]=0;
B.mat[2][0]=0;B.mat[2][1]=0;B.mat[2][2]=1;
B=fst(B,n-2);
A=B*A;
cout<<pow_mod(a,A.mat[0][0])<<endl;
}
return 0;
}

本文介绍了一种使用矩阵快速幂结合欧拉函数解决特定数列问题的方法,并通过一个具体的编程实现案例来说明该方法的应用。文章首先指出观察数列规律的重要性,随后介绍了如何构造并利用矩阵进行快速幂运算,特别注意到了对p-1取模的过程以降低指数的复杂度。此外,还考虑了特殊情况的处理,如当基数a模p等于0的情况。

426

被折叠的 条评论
为什么被折叠?



