hdu 5667 BestCoder Round #80 矩阵快速幂
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.
f_n=\left{\begin{matrix} 1 ,&n=1 \ a^b,&n=2 \ a^bf_{n-1}^cf_{n-2},&otherwise \end{matrix}\right.fn=⎩⎨⎧1,ab,abfn−1cfn−2,n=1n=2otherwise
\ \ \ \ He gives you 5 numbers n,a,b,c,p,and he will eat f_nfn foods.But there are only p foods,so you should tell him f_nfn mod p.
\ \ \ \ 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≤1018,1≤a,b,c≤109,pp is a prime number,and p\le 10^9+7p≤109+7.
\ \ \ \ Output one number for each case,which is f_nfn mod p.
1
5 3 3 3 233
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 矩阵快速幂的更多相关文章
- hdu 5607 BestCoder Round #68 (矩阵快速幂)
graph Accepts: 9 Submissions: 61 Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 ...
- 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 ...
- HDU 4686 Arc of Dream 矩阵快速幂,线性同余 难度:1
http://acm.hdu.edu.cn/showproblem.php?pid=4686 当看到n为小于64位整数的数字时,就应该有个感觉,acm范畴内这应该是道矩阵快速幂 Ai,Bi的递推式题目 ...
- HDU - 4990 Reading comprehension 【矩阵快速幂】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4990 题意 初始的ans = 0 给出 n, m for i in 1 -> n 如果 i 为奇 ...
- 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 ...
- HDU 2604 Queuing( 递推关系 + 矩阵快速幂 )
链接:传送门 题意:一个队列是由字母 f 和 m 组成的,队列长度为 L,那么这个队列的排列数为 2^L 现在定义一个E-queue,即队列排列中是不含有 fmf or fff ,然后问长度为L的E- ...
- HDU 6470:Count(矩阵快速幂)
Count Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submi ...
- hdu 1575 Tr A(矩阵快速幂)
今天做的第二道矩阵快速幂题,因为是初次接触,各种奇葩错误整整调试了一下午.废话不说,入正题.该题应该属于矩阵快速幂的裸题了吧,知道快速幂原理(二进制迭代法,非递归版)后,剩下的只是处理矩阵乘法的功夫了 ...
- hdu 4565 So Easy!(矩阵+快速幂)
题目大意:就是给出a,b,n,m:让你求s(n); 解题思路:因为n很可能很大,所以一步一步的乘肯定会超时,我建议看代码之前,先看一下快速幂和矩阵快速幂,这样看起来就比较容易,这里我直接贴别人的推导, ...
随机推荐
- scrapy 爬取当当网产品分类
#spider部分import scrapy from Autopjt.items import AutopjtItem from scrapy.http import Request class A ...
- bzoj千题计划251:bzoj3672: [Noi2014]购票
http://www.lydsy.com/JudgeOnline/problem.php?id=3672 法一:线段树维护可持久化单调队列维护凸包 斜率优化DP 设dp[i] 表示i号点到根节点的最少 ...
- 微信支付get_brand_wcpay_request:fail
最近做了微信支付功能,和后端一起踩坑中,微信一直报错:get_brand_wcpay_request:fail 出现该问题的原因: 1.生成的sign签名有问题 2.支付授权目录配置有问题 在经过仔细 ...
- SAN LUN Mapping出错导致文件系统共享冲突,数据恢复成功
[用户单位] 中国联通某分公司[数据恢复故障描述] SUN 光纤存储系统,中心存储为6枚300G硬盘组成的RAID6,划分为若干LUN,MAP到不同业务的服务器上,服务器上运行SUN SOLAR ...
- HTML,文字两端对齐
text-align: justify样式的意思是文字两端对齐,但是有时候你会发现这东西不起左右,比如在div标签中的文字. 解决方法:在div中放一个空的span标签,并使用下面的样式. .just ...
- EasyUI中, datagrid用loadData方法绑定数据。
$("#dg").datagrid("loadData", { , " }, { "ck": "1", &qu ...
- JAVA类的方法调用和变量(全套)
一.类的分类: 1.普通类 2.抽象类(含有抽象方法的类) 3.静态类(不需要实例化,就可以使用的类) 二.方法的分类: 1.私有方法(只有类的内部才可以访问的方法) 2.保护方法(只有类的内部和该该 ...
- python的模块和包
==模块== python语言的组织结构层次: 包->模块->代码文件->类->函数->代码块 什么是模块呢 可以把模块理解为一个代码文件的封装,这是比类更高一级的封装层 ...
- vscode使用shell
https://stackoverflow.com/questions/42606837/how-to-use-bash-on-windows-from-visual-studio-code-inte ...
- springmvc的ModelAttribute注解
先看一个没有使用@ModelAttribute的Controller方法. @RequestMapping("/save") public String save(User use ...