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很可能很大,所以一步一步的乘肯定会超时,我建议看代码之前,先看一下快速幂和矩阵快速幂,这样看起来就比较容易,这里我直接贴别人的推导, ...
随机推荐
- pop 一个viewController时候会有键盘闪现出来又消失
原因是alertview关闭影响了系统其他的动画导致的.要么延迟调用,要么自己做一个alertview. iOS 8.3,dismiss alert view时系统会尝试恢复之前的keyboard i ...
- DELL EqualLogic PS存储硬盘故障数据恢复成功案例分享
DELL EqualLogic PS4000采用虚拟ISCSI SAN阵列,为远程或分支办公室.部门和中小企业存储部署带来企业级功能.智能化.自动化和可靠性.以简化的管理.快速的部署及合理的价格满足了 ...
- JaveScript内置对象(JS知识点归纳八)
1)JS自身提供的方式 用于对数据进行简便的操作,根据方法可以操作的数据类型不同,形成了不同的对象--内置对象 2)数组 a)基本操作方法--对数组进行修改 从数组最后进行操作 1)数组.push ...
- Linux入门:usermod - 修改用户帐户信息
一.什么是usermod? usermod 命令通过修改系统帐户文件来修改用户账户信息usermod [options] user_name选项(options)-a|--append ##把用户追加 ...
- python入门:python包管理工具pip的安装
pip 是一个安装和管理 Python 包的工具 , 是 easy_install 的一个替换品. distribute是setuptools的取代(Setuptools包后期不再维护了),pip是e ...
- GIT入门笔记(15)- 链接到私有GitLab仓库
GitLab是利用 Ruby on Rails 一个开源的版本管理系统,实现一个自托管的Git项目仓库,可通过Web界面进行访问公开的或者私人项目.它拥有与Github类似的功能,能够浏览源代码,管理 ...
- spring-oauth-server实践:OAuth2.0 通过header 传递 access_token 验证
一.解析查找 access_token 1.OAuth2AuthenticationProcessingFilter.tokenExtractor 2.发现来源可以有两处:请求的头或者请求的参数 二. ...
- java配置环境变量,无法也行javac问题
最近换了公司,搭建开发环境的时候出了点小差错,写篇随笔记录下,下载jdk的时候要选择符合自己电脑的jdk版本,位数. 笔者之前下载的时候没注意下了个32bit的jdk,开发用eclipse的时候打不开 ...
- POJ-1679 The Unique MST---判断最小生成树是否唯一
题目链接: https://vjudge.net/problem/POJ-1679 题目大意: 给定一个无向连通网,判断最小生成树是否唯一. 思路: (1)对图中的每条边,扫描其他边,如果存在相同权值 ...
- vuex commit保存数据技巧
vuex 单向数据流,推荐的commit 改变state数据,写起来非常繁琐,因为改数据可能要写很多commit函数. 依据我的理解,单向数据流主要是为了避免数据混乱,便于调试. 说白了,就是一个数据 ...