Sequence

 Accepts: 59
 Submissions: 650
 Time Limit: 2000/1000 MS (Java/Others)
 Memory Limit: 65536/65536 K (Java/Others)
Problem Description

\ \ \ \    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.

f_n=\left{\begin{matrix} 1 ,&n=1 \ a^b,&n=2 \ a^bf_{n-1}^cf_{n-2},&otherwise \end{matrix}\right.f​n​​=​⎩​⎨​⎧​​​1,​a​b​​,​a​b​​f​n−1​c​​f​n−2​​,​​​n=1​n=2​otherwise​​

\ \ \ \    He gives you 5 numbers n,a,b,c,p,and he will eat f_nf​n​​ foods.But there are only p foods,so you should tell him f_nf​n​​ 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\le T \le 10,1\le n\le 10^{18},1\le a,b,c\le 10^9    1≤T≤10,1≤n≤10​18​​,1≤a,b,c≤10​9​​,pp is a prime number,and p\le 10^9+7p≤10​9​​+7.

Output

\ \ \ \    Output one number for each case,which is f_nf​n​​ mod p.

Sample Input
1
5 3 3 3 233
Sample Output
190
/*
hdu 5667 BestCoder Round #80 矩阵快速幂 F[n] = 1 (n == 1)
F[n] = a^b (n == 2)
F[n] = a^b * F[n-1]^c *F [n-2] 最开始试了下化简公式,但是无果. 也从矩阵快速幂上面考虑过(毕竟 F[n]与 F[n-1],F[n-2]有关)
但是发现是 乘法运算不知道怎么弄了(2b了) 能够发现运算时基于a的次方的,当a的次方相乘时就变成了他们的次方相加 (好气 TAT)
于是乎 a^g[n] = a^(b + c*g[n-1] * g[n-2])
然后用类似快速幂求斐波那契数的方法即可 F[n] F[n-1] 1 C 1 0
F[n-1] F[n-2] 1 * 1 0 0
b 0 1
hhh-2016-04-18 20:36:40
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <map>
#include <algorithm>
#include <functional>
#include <math.h>
using namespace std;
#define lson (i<<1)
#define rson ((i<<1)|1)
typedef long long ll;
const int maxn = ; struct Matrix
{
ll ma[][];
Matrix()
{
memset(ma,,sizeof(ma));
}
}; Matrix mult(Matrix ta,Matrix tb, ll mod)
{
Matrix tc;
for(int i = ; i < ; i++)
{
for(int j = ; j < ; j++)
{
tc.ma[i][j] = ;
for(int k = ; k < ; k++){
tc.ma[i][j] += (ta.ma[i][k] * tb.ma[k][j])%mod;
tc.ma[i][j] %= mod;
}
}
}
return tc;
} Matrix Mat_pow(Matrix ta,ll n,ll mod)
{
Matrix t;
for(int i = ; i < ; i++)
t.ma[i][i] = ;
while(n)
{
if(n & ) t = mult(t,ta,mod);
ta = mult(ta,ta,mod);
n >>= ;
}
return t;
} ll pow_mod(ll a,ll n,ll mod)
{
ll t = ;
a %= mod ;
while(n)
{
if(n & ) t = t*a%mod;
a = a*a%mod;
n >>= ;
}
return t;
} Matrix mat;
Matrix an;
ll a,b,c;
void ini(ll mod)
{
mat.ma[][] = c,mat.ma[][] = ,mat.ma[][] = ;
mat.ma[][] = ,mat.ma[][] = ,mat.ma[][] = ;
mat.ma[][] = b,mat.ma[][] = ,mat.ma[][] = ; an.ma[][] = (b+b*c%mod)%mod,an.ma[][] = b,an.ma[][] = ;
an.ma[][] = b,an.ma[][] = ,an.ma[][] = ;
}
ll mod,n; int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%I64d%I64d%I64d%I64d%I64d",&n,&a,&b,&c,&mod);
a%=mod,b%=mod,c%=mod;
ini(mod-);
if(n == )
{
printf("1\n");
}
else if(n == )
printf("%I64d\n",pow_mod(a,b,mod));
else
{
mat = Mat_pow(mat,n-,mod-);
mat = mult(an,mat,mod-);
ll ci = mat.ma[][];
//cout << ci <<endl;
printf("%I64d\n",pow_mod(a,ci,mod));
}
}
return ;
}

hdu 5667 BestCoder Round #80 矩阵快速幂的更多相关文章

  1. hdu 5607 BestCoder Round #68 (矩阵快速幂)

    graph  Accepts: 9 Submissions: 61  Time Limit: 8000/4000 MS (Java/Others)  Memory Limit: 65536/65536 ...

  2. hdu 4686 Arc of Dream(矩阵快速幂)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=4686 题意: 其中a0 = A0ai = ai-1*AX+AYb0 = B0bi = bi-1*BX+BY ...

  3. HDU 4686 Arc of Dream 矩阵快速幂,线性同余 难度:1

    http://acm.hdu.edu.cn/showproblem.php?pid=4686 当看到n为小于64位整数的数字时,就应该有个感觉,acm范畴内这应该是道矩阵快速幂 Ai,Bi的递推式题目 ...

  4. HDU - 4990 Reading comprehension 【矩阵快速幂】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4990 题意 初始的ans = 0 给出 n, m for i in 1 -> n 如果 i 为奇 ...

  5. HDU 1005 Number Sequence:矩阵快速幂

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1005 题意: 数列{f(n)}: f(1) = 1, f(2) = 1, f(n) = ( A*f(n ...

  6. HDU 2604 Queuing( 递推关系 + 矩阵快速幂 )

    链接:传送门 题意:一个队列是由字母 f 和 m 组成的,队列长度为 L,那么这个队列的排列数为 2^L 现在定义一个E-queue,即队列排列中是不含有 fmf or fff ,然后问长度为L的E- ...

  7. HDU 6470:Count(矩阵快速幂)

    Count Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  8. hdu 1575 Tr A(矩阵快速幂)

    今天做的第二道矩阵快速幂题,因为是初次接触,各种奇葩错误整整调试了一下午.废话不说,入正题.该题应该属于矩阵快速幂的裸题了吧,知道快速幂原理(二进制迭代法,非递归版)后,剩下的只是处理矩阵乘法的功夫了 ...

  9. hdu 4565 So Easy!(矩阵+快速幂)

    题目大意:就是给出a,b,n,m:让你求s(n); 解题思路:因为n很可能很大,所以一步一步的乘肯定会超时,我建议看代码之前,先看一下快速幂和矩阵快速幂,这样看起来就比较容易,这里我直接贴别人的推导, ...

随机推荐

  1. 2017-2018-1 我爱学Java 第二周 作业

    Android Game Discussion Questions Answers 20162309邢天岳 20162311张之睿 20162312张家铖 20162313苑洪铭 20162324春旺 ...

  2. 【iOS】OC-AFNetworking 2.0 跟踪文件上传进度

    我是较新的 AFNetworking 2.0.使用下面的代码片段,我已经能够成功地将一张照片上传到我的 url.我想要跟踪的增量上载进度,但我找不到这样做 2.0 版的示例.我的应用程序是 iOS 7 ...

  3. day-7 一个简单的决策树归纳算法(ID3)python编程实现

    本文介绍如何利用决策树/判定树(decision tree)中决策树归纳算法(ID3)解决机器学习中的回归问题.文中介绍基于有监督的学习方式,如何利用年龄.收入.身份.收入.信用等级等特征值来判定用户 ...

  4. 根据抽象工厂实现的DBHelpers类

    public abstract class DBHelper { public static SqlConnection conn = new SqlConnection("server=l ...

  5. php析构方法

    析构方法说明: 1. 析构方法会自动调用 2. 析构方法主要用于销毁资源(比如释放数据库的链接,图片资源...销毁某个对象..); 析构函数会在到对象的所有的引用都被删除或者当对象被显示销毁时执行. ...

  6. SpringBoot单元测试中的事务和Session

    1.Springboot中使用junit编写单元测试,并且测试结果不影响数据库. 2.

  7. ELK学习总结(3-1)elk的基本查询

    基本查询:内置条件 组合查询:组合基本查询 过滤:查询同时,通过filter筛选数据 准备工作  GET /library/books/_mget { "ids":["1 ...

  8. OAuth2.0学习(1-13)oauth2.0 的概念:资源、权限(角色)和scope

    mkk 关于资源的解释 : https://andaily.com/blog/?cat=19 resource用于将系统提供的各类资源进行分组管理, 每一个resource对应一个resource-i ...

  9. logback打印日志时添加上下文

    尝试上述特性, 配置如下: 效果:

  10. Android智能手机上的音频浅析

    手机可以说是现在人日常生活中最离不开的电子设备了.它自诞生以来,从模拟的发展到数字的,从1G发展到目前的4G以及不久将来的5G,从最初的只有唯一的功能(打电话)发展到目前的全功能,从功能机(featu ...