#include<cstdio>
#include<string>
#include<iostream>
#include<vector>
#include<set>
#include<map>
#include<math.h>
#include<queue>
#include<stdlib.h>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL; /*
1 0 0
1 C 1
0 1 0 */
/*费马小定理的运用【第一次,膜拜费马小定理】*/ LL n,a,b,c,p,q; struct asd{
LL a[5][5];
}; asd mul(asd a1,asd a2)
{
asd ans;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
ans.a[i][j]=0;
for(int k=0;k<3;k++)
{
ans.a[i][j]+=a1.a[i][k]*a2.a[k][j];
ans.a[i][j]%=q;
}
ans.a[i][j]%=q;
}
}
return ans;
} asd quickmul(LL g,asd z)
{
asd ans;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(i==j)
ans.a[i][j]=1;
else
ans.a[i][j]=0;
}
}
while(g)
{
if(g%2)
{
ans=mul(ans,z);
}
g/=2;
z=mul(z,z);
}
return ans;
} LL liu(LL x,LL y)
{
LL ans;
ans=1;
while(y)
{
if(y%2)
ans=ans*x%p;
y/=2;
x=x*x%p;
}
return ans;
} /***---先用矩阵快速幂算出次数,然后用快速幂算出答案-----***/
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%lld%lld%lld%lld%lld",&n,&a,&b,&c,&p);
if(n==1)
{
printf("1\n");
continue;
}
q=p-1;
if(a%p==0)
{
printf("0\n");
continue;
} asd m;
m.a[0][0]=1;m.a[0][1]=0;m.a[0][2]=0;
m.a[1][0]=1;m.a[1][1]=c;m.a[1][2]=1;
m.a[2][0]=0;m.a[2][1]=1;m.a[2][2]=0;
asd ans;
ans=quickmul(n-2,m); //m矩阵的(n-1)次,之后还要×一个特定矩阵 z;PS:因为算出了的已经包括了两个,然后所以是n-2...搞了半个小时,然后被厂长发现...瞎几把连测试都不会... // asd an;
// an.a[0][0]=1;an.a[0][1]=0;an.a[0][2]=0;
// an.a[1][0]=1;an.a[1][1]=2;an.a[1][2]=1;
// an.a[2][0]=0;an.a[2][1]=1;an.a[2][2]=0;
// an=quickmul(2,an);
// for(int i=0;i<3;i++)
// {
// for(int j=0;j<3;j++)
// printf("%d ",an.a[i][j]);
// printf("\n");
// }
// printf("%lld\n",liu(2,3)); // asd z;
// z.a[0][0]=b;
// z.a[1][0]=b;
// z.a[2][0]=0; LL pp;
pp=(ans.a[1][0]*b+ans.a[1][1]*b)%q; //直接得出 次数 LL k;
k=liu(a,pp); //快速幂得出答案
printf("%lld\n",k%p); }
return 0;
}
/*
可以这样测案例
100
1 3 3 3 233
2 3 3 3 233
3 3 3 3 233
4 3 3 3 233
5 3 3 3 233 */

hdoj5667 BestCoder Round #80 【费马小定理(膜拜)+矩阵快速幂+快速幂】的更多相关文章

  1. BZOJ 3240([Noi2013]矩阵游戏-费马小定理【矩阵推论】-%*s-快速读入)

    3240: [Noi2013]矩阵游戏 Time Limit: 10 Sec   Memory Limit: 256 MB Submit: 123   Solved: 73 [ Submit][ St ...

  2. HDU4549 M斐波那契数列 —— 斐波那契、费马小定理、矩阵快速幂

    题目链接:https://vjudge.net/problem/HDU-4549 M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others)    Memory Li ...

  3. [bzoj5118]Fib数列2_费马小定理_矩阵乘法

    Fib数列2 bzoj-5118 题目大意:求Fib($2^n$). 注释:$1\le n\le 10^{15}$. 想法:开始一看觉得一定是道神题,多好的题面啊?结果...妈的,模数是质数,费马小定 ...

  4. hdu_4869(费马小定理+快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4869 Turn the pokers Time Limit: 2000/1000 MS (Java/O ...

  5. [HDOJ5667]Sequence(矩阵快速幂,费马小定理)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5667 费马小定理: 假如p是质数,且gcd(a,p)=1,那么 a^(p-1)≡1(mod p). 即 ...

  6. 牛客Wannafly挑战赛13-BJxc军训-费马小定理、分式取模、快速幂

    参考:https://blog.csdn.net/qq_40513946/article/details/79839320 传送门:https://www.nowcoder.com/acm/conte ...

  7. Newcoder Wannafly13 B Jxy军训(费马小定理、分数在模意义下的值)

    链接:https://www.nowcoder.com/acm/contest/80/B 题目描述 在文某路学车中学高一新生军训中,Jxc正站在太阳下站着军姿,对于这样的酷热的阳光,Jxc 表示非常不 ...

  8. hdu 4549 M斐波那契数列(快速幂 矩阵快速幂 费马小定理)

    题目链接http://acm.hdu.edu.cn/showproblem.php?pid=4549: 题目是中文的很容易理解吧.可一开始我把题目看错了,这毛病哈哈. 一开始我看错题时,就用了一个快速 ...

  9. hdu 4704 Sum (整数和分解+快速幂+费马小定理降幂)

    题意: 给n(1<n<),求(s1+s2+s3+...+sn)mod(1e9+7).其中si表示n由i个数相加而成的种数,如n=4,则s1=1,s2=3.                  ...

随机推荐

  1. [HTML5] Show Different Variations of Images Depending on the Viewport Width using Art Direction

    For small viewports, we may want to show a variation of the desktop image. A very common use case of ...

  2. vue - 前置工作 - 目录功能介绍

    一个DEMOS的完整目录(由于GWF问题,我就不一一打开网站一个个去搜索并且解释了)可以去关注:https://www.cnblogs.com/ye-hcj build build.js(本文来自ht ...

  3. dm8127/8148下怎样进行dsplink的编写

    样例:从A8读入1080p的yuv420sp的数据给dsplink,在dsp 中建立一个link(做一些图像处理的工作).然后将yuv420sp的数据发送到videoM3做jpeg编码,然后在传递到A ...

  4. poj 3169 Layout(差分约束)

    Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6549   Accepted: 3168 Descriptio ...

  5. 走入asp.net mvc不归路:[6]linq常见用法

    asp.net mvc结合linq,先不说性能问题,对于增删查改的操作还是相当方便的.以下我们就来介绍一下linq在asp.net mvc的Controller中的常见用法. 1 首先来看看整个数据表 ...

  6. CocoaPods安装问题

    主要步骤: sudo gem install cocoapods pod setup 验证是否安装成功: pod search SDWebImage 常见问题: 1 sudo gem install ...

  7. android binder 机制三(匿名Service)

    什么是匿名Service?凡是没有到ServiceManager上注冊的Service,都是匿名Service. 还是拿上一篇的样例来举例,看代码: status_t MediaPlayer::set ...

  8. poj1703 Find them,Catch them 【并查集】

    做过一些的带权并查集,再来做所谓的"种类并查集",发现好像就顿悟了. 种类并查集与带权并查集实质上的区别并不大. 关键的区别就是种类并查集仅仅是带权并查集再弄个%取余操作而已.然后 ...

  9. redis03----link 链表操作

    link 链表结构 之前是操作字符串string 链表:头元素,后面一个一个的指向后面的元素.Redis内部实现了链表的结构.链表的头尾,从一个元素找到另外的元素. 链表的名字也是一个key. flu ...

  10. 大O符号

    大O 就是 时间复杂度.时间复杂度是大概的描述一个算法的用时(实际上从侧面的表达了他的效率) 最好时间复杂度,通常指在最好情形下,这个算法用时.反之,最坏情况下的就是最坏时间复杂度.通常 冒泡算法 的 ...