Sequence (矩阵快速幂+快速幂+费马小定理)
Holion August will eat every thing he has found.
Now there are many foods,but he does not want to eat all of them at once,so he find a sequence.
fn=⎧⎩⎨⎪⎪1,ab,abfcn−1fn−2,n=1n=2otherwisefn={1,n=1ab,n=2abfn−1cfn−2,otherwise
He gives you 5 numbers n,a,b,c,p,and he will eat fnfn foods.But there are only p foods,so you should tell him fnfn mod p.
Now there are many foods,but he does not want to eat all of them at once,so he find a sequence.
fn=⎧⎩⎨⎪⎪1,ab,abfcn−1fn−2,n=1n=2otherwisefn={1,n=1ab,n=2abfn−1cfn−2,otherwise
He gives you 5 numbers n,a,b,c,p,and he will eat fnfn foods.But there are only p foods,so you should tell him fnfn mod p.
Input The first line has a number,T,means testcase.
Each testcase has 5 numbers,including n,a,b,c,p in a line.
1≤T≤10,1≤n≤1018,1≤a,b,c≤109 1≤T≤10,1≤n≤1018,1≤a,b,c≤109,pp is a prime number,and p≤109+7p≤109+7.Output Output one number for each case,which is fnfn mod p.Sample Input
1 5 3 3 3 233
Sample Output
190
代码:
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<queue> #include<stack> #include<set> #include<algorithm> #include<vector> #include<cmath> const int maxn=1e5+5; typedef long long ll; using namespace std; struct mat { ll s[3][3]; }; ll nn,a,b,c,p; ll ksm(ll x,ll y) { ll ans=1; while(y) { if(y&1) ans=(ans*x)%p; y>>=1; x=x*x%p; } return ans; } mat Mul(mat x,mat y) { mat ans; memset(ans.s,0,sizeof(ans.s)); for(int t=0;t<3;t++) { for(int j=0;j<3;j++) { for(int k=0;k<3;k++) { ans.s[t][j]=(ans.s[t][j]+(x.s[t][k]*y.s[k][j]))%(p-1);//费马小定理 } } } return ans; } mat ans; ll QuickPow(ll n) { mat res; memset(res.s,0,sizeof(res.s)); res.s[0][0]=c; res.s[0][1]=1; res.s[0][2]=1; res.s[1][0]=1; res.s[2][2]=1; while(n) { if(n&1) { ans=Mul(res,ans); } n>>=1; res=Mul(res,res); } return ans.s[0][0]; } int main() { int T; scanf("%d",&T); while(T--) { scanf("%lld%lld%lld%lld%lld",&nn,&a,&b,&c,&p); if(nn==1) { printf("1\n"); } else if(nn==2) { printf("%lld\n",ksm(a,b)); } else { ans.s[0][0]=b; ans.s[1][0]=0; ans.s[2][0]=b; ll ss=QuickPow(nn-2); printf("%lld\n",ksm(a,ss+p-1));//费马小定理 } } return 0; }
作者:李斌
-------------------------------------------
个性签名:独学而无友,则孤陋而寡闻。做一个灵魂有趣的人!
如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!
万水千山总是情,打赏一分行不行,所以如果你心情还比较高兴,也是可以扫码打赏博主,哈哈哈(っ•̀ω•́)っ✎⁾⁾!
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步