#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. 【LeetCode】Generate Parentheses 解题报告

    [题目] Given n pairs of parentheses, write a function to generate all combinations of well-formed pare ...

  2. SolidEdge 如何由装配图快速生成爆炸视图

    建立图纸精灵,组态中选择explode(没有下一步可选了)   点击完成即可绘制爆炸图

  3. Excel实用技巧-如何批量提取excel工作表名称

    Excel实用技巧-如何批量提取excel工作表名称 1. 打开Excel文件,点击“公式”栏,进而点击“定义管理器” 2. 在弹出的对话框中,点击新增按钮, 名称:“sheet”,引用位置:“=RE ...

  4. 获取连接状态数的awk数组命令

    awk -n|more zhutianpeng@ztp-OptiPlex-:~/Icpp/server$ netstat -n|more 激活Internet连接 (w/o 服务器) Proto Re ...

  5. AJAX核心XMLHTTPRequest对象

    老早就写好了总结.今天整理发表一下. XMLHttpRequest对象是AJAX的核心技术,XMLHttpRequest 是XMLHTTP 组件的对象,通过这个对象.AJAX能够像桌面应用程序一样仅仅 ...

  6. GuozhongCrawler系列教程 (5) TransactionRequest具体解释

    为了实现和维护并发抓取的属性信息提供线程安全的事务请求.TransactionRequest是一个抽象类自己不能设置Processor,却须要实现 TransactionCallBack接口.Tran ...

  7. C# does not contain a constructor that takes no parameter

    C# 中子类要重用父类的构造函数时, 一般会在子类构造函数后面调用 : base(paratype, para). 如果父类有一个參数个数为1的构造函数, 没有 0 參构造函数. 子类想要重用这个构造 ...

  8. HBase协处理器同步二级索引到Solr

    一. 背景二. 什么是HBase的协处理器三. HBase协处理器同步数据到Solr四. 添加协处理器五. 测试六. 协处理器动态加载 一. 背景 在实际生产中,HBase往往不能满足多维度分析,我们 ...

  9. 4.改变eclipse选中文字颜色

    window-preferences-general-editors-text editors-annotations-occurrences 和 window-preferences-general ...

  10. O4编译 在 PingCAP 的一些技术挑战

    在 PingCAP 的一些技术挑战 http://www.zenlife.tk/challenge-at-pingcap.md 在 PingCAP 的一些技术挑战 2018-06-02 事务优化 AC ...