题目意思:给出一个递推公式求出f(n);
这样的题一般认为是矩阵快速幂,但是这题给出的关系包含幂次,那么就不能简单的通过给出的公式来构造矩阵,但是可以通过幂次构造。
根据费马小定理对于a^tp%p,如果a,p互质,那么a^tp%p = (a^(tp%p-1))%p;
所以矩阵快速速幂的时候要对p-1取余;
然后最后在求a^z[n]时,要注意a^0 = 0;至于原因对着题目理解一下。
#ifdef __STRICT_ANSI__
#undef __STRICT_ANSI__
#endif
#include<iostream>
//#include<bits/stdc++.h>
#include<cstdio>
#include<string>
#include<cstring>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<ctime>
#include<algorithm>
#include<cmath>
#include<vector>
#define showtime fprintf(stderr,"time = %.15f\n",clock() / (double)CLOCKS_PER_SEC)
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
typedef long long ll;
typedef long long LL;
typedef unsigned long long ull;
#define MP make_pair
#define PII pair<int,int>
#define PFI pair<double,int>
#define PLL pair<ll,ll>
#define F first
#define S second
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
//freopen("1005.in","r",stdin);
//freopen("data.out","w",stdout);
typedef vector<ll> Vec;
typedef vector<Vec> Mat;
const double eps = 1e-7;
const double PI = acos(-1.);
const double E = 2.71828182845904523536;
const int MOD = (int)1e9+7;
const int INF = 0x3f3f3f3f;
const ll INFLL = 0x7f7f7f7f;
const int N = 5e5 + 25;
const int M = 1200000 + 5;
inline int popcnt(int x){return __builtin_popcount(x); }
inline int clz(int x){return __builtin_clz(x);} //末尾的 0,即对 lowbit 取log
inline int clz(LL x){return __builtin_clzll(x);}
inline int lg2(int x){ return !x ? -1 : 31-clz(x);}
inline int lg2(LL x){ return !x ? -1 : 63-clz(x);}
int T;
Mat I;
ll n,a,b,c,p,MYMOD;
Mat mul(Mat a,Mat b){
Mat ret(a.size(),vector<ll>(b[0].size(),0));
for(int i=0;i<a.size();i++){
for(int j=0;j<b[0].size();j++){
for(int k=0;k<a[0].size();k++){
ret[i][j] = (ret[i][j] + (a[i][k] * b[k][j]) % MYMOD) % MYMOD;
}
}
}
return ret;
}
Mat quick_mp(Mat mat,ll b){
Mat ret = I;
while(b){
if(b&1) ret = mul(ret,mat);
b >>= 1;
mat = mul(mat,mat);
}
return ret;
}
ll quick(ll a,ll b,ll MOD){
if(a ==0 ||b == 0)return 0;
ll ret = 1;
while(b){
if(b&1) ret = ret * a % MOD;
a = a * a % MOD;
b >>= 1;
}
return ret;
}
int main(){
scanf("%d",&T);
while(T --) {
cin >> n >> a >> b >> c >> p;
if(n == 1){
puts("1");
continue;
}
Mat base(3,vector<ll>(3,0));
base[0][1] = base[1][0] = base[2][2] = 1;
base[0][0] = c; base[0][2] = b;
Mat mat(3,vector<ll>(1,b));
mat[1][0] = 0;
mat[2][0] = 1;
I = Mat(3,vector<ll>(3,0)); // init初始化
for(int i = 0; i < 3; i ++) I[i][i] = 1;
ll ans;
if(n == 2){
ans = quick(a,b,p) % p;
}else{
MYMOD = p - 1;
base = quick_mp(base,n-2);
mat = mul(base,mat);
ll tp = mat[0][0];
ans = quick(a,tp,p) % p; // 快速幂
}
cout << ans << endl;
}
return 0;
}