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.

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+;
typedef long long ll;
using namespace std; struct mat
{
ll s[][];
};
ll nn,a,b,c,p;
ll ksm(ll x,ll y)
{
ll ans=;
while(y)
{
if(y&)
ans=(ans*x)%p;
y>>=;
x=x*x%p;
}
return ans;
}
mat Mul(mat x,mat y)
{
mat ans;
memset(ans.s,,sizeof(ans.s));
for(int t=;t<;t++)
{
for(int j=;j<;j++)
{
for(int k=;k<;k++)
{
ans.s[t][j]=(ans.s[t][j]+(x.s[t][k]*y.s[k][j]))%(p-);//费马小定理
}
}
}
return ans;
}
mat ans;
ll QuickPow(ll n)
{
mat res;
memset(res.s,,sizeof(res.s));
res.s[][]=c;
res.s[][]=;
res.s[][]=;
res.s[][]=;
res.s[][]=;
while(n)
{
if(n&)
{
ans=Mul(res,ans);
}
n>>=;
res=Mul(res,res);
}
return ans.s[][];
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%lld%lld%lld%lld%lld",&nn,&a,&b,&c,&p);
if(nn==)
{
printf("1\n");
}
else if(nn==)
{
printf("%lld\n",ksm(a,b));
}
else
{
ans.s[][]=b;
ans.s[][]=;
ans.s[][]=b;
ll ss=QuickPow(nn-);
printf("%lld\n",ksm(a,ss+p-));//费马小定理
}
} return ;
}

Sequence (矩阵快速幂+快速幂+费马小定理)的更多相关文章

  1. poj 3734 Blocks 快速幂+费马小定理+组合数学

    题目链接 题意:有一排砖,可以染红蓝绿黄四种不同的颜色,要求红和绿两种颜色砖的个数都是偶数,问一共有多少种方案,结果对10007取余. 题解:刚看这道题第一感觉是组合数学,正向推了一会还没等推出来队友 ...

  2. hdu4549(费马小定理 + 快速幂)

    M斐波那契数列F[n]是一种整数数列,它的定义如下: F[0] = a F[1] = b F[n] = F[n-1] * F[n-2] ( n > 1 ) 现在给出a, b, n,你能求出F[n ...

  3. hdu-5667 Sequence(矩阵快速幂+费马小定理+快速幂)

    题目链接: Sequence Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others) ...

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

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

  5. HDU 5667 Sequence【矩阵快速幂+费马小定理】

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5667 题意: Lcomyn 是个很厉害的选手,除了喜欢写17kb+的代码题,偶尔还会写数学题.他找到 ...

  6. HDU 5667 Sequence 矩阵快速幂+费马小定理

    题目不难懂.式子是一个递推式,并且不难发现f[n]都是a的整数次幂.(f[1]=a0;f[2]=ab;f[3]=ab*f[2]c*f[1]...) 我们先只看指数部分,设h[n]. 则 h[1]=0; ...

  7. hdu 4549 M斐波拉契 (矩阵快速幂 + 费马小定理)

    Problem DescriptionM斐波那契数列F[n]是一种整数数列,它的定义如下: F[0] = aF[1] = bF[n] = F[n-1] * F[n-2] ( n > 1 ) 现在 ...

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

    M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Sub ...

  9. hdu4549矩阵快速幂+费马小定理

    转移矩阵很容易求就是|0  1|,第一项是|0| |1  1|             |1| 然后直接矩阵快速幂,要用到费马小定理 :假如p是质数,且gcd(a,p)=1,那么 a(p-1)≡1(m ...

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

    M斐波那契数列 Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other) Total Submi ...

随机推荐

  1. Linux无名管道通信介绍

    Linux下无名管道一般仅用于父子进程间的通信: 测试代码如下 //file name: fifo_test.c #include <sys/prctl.h> #include " ...

  2. 2、适配器 adapter 模式 加个"适配器" 以便于复用 结构型设计模式

    1.什么是适配器模式? 适配器如同一个常见的变压器,也如同电脑的变压器和插线板之间的电源连接线,他们虽然都是3相的,但是电脑后面的插孔却不能直接插到插线板上. 如果想让额定工作电压是直流12伏特的笔记 ...

  3. for…of使用

    3.for…of使用 3.1 for…of使用 for...of 一种用于遍历数据结构的方法.它可遍历的对象包括数组,对象,字符串,set和map结构等具有iterator 接口的数据结构. 我们先来 ...

  4. 2020 Multi-University Training Contest 1 部分题解

    目录 Cookies Distinct Sub-palindromes Fibonacci Sum Finding a MEX Leading Robots Math is Simple Minimu ...

  5. java Eclipse刷新报错 Feature 'taglib' not found.

    刷新工程报错:org.eclipse.emf.ecore.xmi.FeatureNotFoundException: Feature 'taglib' not found. 错误原因:tomcat7, ...

  6. C#设计模式之15-解释器模式

    解释器模式(Interpreter Pattern) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/415 访问. 解释 ...

  7. 数据源管理 | 分布式NoSQL系统,Cassandra集群管理

    本文源码:GitHub·点这里 || GitEE·点这里 一.Cassandra简介 1.基础描述 Cassandra是一套开源分布式NoSQL数据库系统.它最初由Facebook开发,用于储存收件箱 ...

  8. K8S(11)配置中心实战-单环境交付apollo三组件

    k8s配置中心实战-交付apollo三组件 目录 k8s配置中心实战-交付apollo三组件 1 apollo简单说明 1.1 apollo最简架构图: 1.2 apollo组件部署关系 2 为app ...

  9. Linux与Windows的区别(后面了解后继续更新)

    1.转自知乎上的@Haoyuan Xing得答案: Linux是一个以开发者为中心的操作系统,Windows是以消费者为中心的操作系统.这是最根本的区别,也是Linux相对于Windows的优势/劣势 ...

  10. unity探索者之微信支付,非第三方插件

    版权声明:本文为原创文章,转载请声明http://www.cnblogs.com/unityExplorer/p/8404604.html 相比微信的登录和分享功能,微信支付sdk的接入显得相当简单, ...