题目给了个公式是相乘的
这个玩意一看就只能快速幂
毕竟10^18…递推瞬间爆炸
然后快速幂也能看出来
相乘的没法搞
那就log
完成
然后不停的tle….
在求出指数的时候…再用个费马小定理
要不然就gg
#include<stdio.h>
#include<math.h>
#include<iostream>
#include<string.h>
#include<queue>
#include<algorithm>
#include<string>
using namespace std;
long long mod,n,a,b,c,p;
long long kuaisumi(long long dishu, long long zhishu)
{
long long fanhui = 1;
while (zhishu)
{
if (zhishu & 1)fanhui *= dishu;
zhishu >>= 1;
dishu *= dishu;
fanhui %= mod;
dishu %= mod;
}
return fanhui;
}
struct juzhen
{
long long t[3][3];
};
juzhen cheng(juzhen yi, juzhen er)
{
juzhen q;
memset(q.t, 0, sizeof(q.t));
for (int qq = 0;qq <3;qq++)
{
for (int ww = 0;ww <3;ww++)
for (int ee = 0;ee < 3;ee++)
{
q.t[qq][ww] += yi.t[qq][ee] * er.t[ee][ww] % (mod - 1);
q.t[qq][ww] %= (mod - 1);
}
}
return q;
}
juzhen juzhenksm(juzhen di, long long zhishu)
{
int biaoji = 0;
juzhen fanhui;
while (zhishu)
{
if (zhishu & 1)
{
if (biaoji == 0)
{
fanhui = di;
biaoji = 1;
}
else fanhui = cheng(fanhui, di);
}
zhishu >>= 1;
di = cheng(di, di);
}
return fanhui;
}
int main()
{
int T;
cin >> T;
while (T--)
{
cin >> n >> a >> b >> c >> p;
if (n == 1)
{
cout << 1 << endl;
continue;
}
else if (n == 2)
{
cout << kuaisumi(a, b) << endl;
continue;
}
mod = p;
long long we[3][3] = { {c%mod,1,1},{1,0,0},{0,0,1} };
juzhen chushi;
for (int qqq = 0;qqq < 3;qqq++)for (int www = 0;www < 3;www++)chushi.t[qqq][www] = we[qqq][www];
juzhen jieshu = juzhenksm(chushi, n-2);
long long zhis = (jieshu.t[0][0] + jieshu.t[0][2])%(mod-1);
long long dish = kuaisumi(a, b);
cout << kuaisumi(dish, zhis) << endl;
}
return 0;
}