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很可能很大,所以一步一步的乘肯定会超时,我建议看代码之前,先看一下快速幂和矩阵快速幂,这样看起来就比较容易,这里我直接贴别人的推导, ...
随机推荐
- python 一致性哈希 分布式
hash_ring # -*- coding: utf-8 -*- """ hash_ring ~~~~~~~~~~~~~~ Implements consistent ...
- android之SVG制作与应用
文章解析及例子:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0825/3362.html 工具:Photoshop CC+sv ...
- 学大伟业 国庆Day2
期望得分:30+100+0=130 实际得分:30+100+20=150 忍者钩爪 (ninja.pas/c/cpp) [问题描述] 小Q是一名酷爱钩爪的忍者,最喜欢飞檐走壁的感觉,有一天小Q发现一个 ...
- 自己写编程语言-m语言
一直对技术有很强的兴趣,终于,决定要写自己的语言(m语言).那就先从最简单的开始:解释执行器. 一套完整的语言包含的肯定不止解释执行器了,还要有编译器和IDE,也就还要有语法高亮.智能提示等,不过还没 ...
- C#中的函数式编程:递归与纯函数(二)
在序言中,我们提到函数式编程的两大特征:无副作用.函数是第一公民.现在,我们先来深入第一个特征:无副作用. 无副作用是通过引用透明(Referential transparency)来定义的.如果一个 ...
- LeetCode & Q167-Two Sum II - Input array is sorted-Easy
Array Two Pointers Binary Search Description: Given an array of integers that is already sorted in a ...
- Step by Step 真正从零开始,TensorFlow详细安装入门图文教程!帮你完成那个最难的从0到1
摘要: Step by Step 真正从零开始,TensorFlow详细安装入门图文教程!帮你完成那个最难的从0到1 安装遇到问题请文末留言. 悦动智能公众号:aibbtcom AI这个概念好像突然就 ...
- AutoCAD中的扩展字典及扩展记录(C#)
在学习CAD扩展记录的过程中,遇到了一些问题,也积累了一些经验,现在给大家分享一些我的学习心得.在学习扩展字典之前需要读者了解cad的组码,也就是DxfCode.感兴趣的也可以了解一下扩展数据的相关内 ...
- 4-51单片机WIFI学习(开发板51单片机自动冷启动下载原理)
上一篇链接 http://www.cnblogs.com/yangfengwu/p/8743936.html 这一篇说一下自己板子的51单片机自动冷启动下载原理,我挥舞着键盘和鼠标,发誓要把世界写个明 ...
- httpClient解决post请求重定向的问题
import com.dadi.saas.util.HTTPUtils; import org.apache.commons.httpclient.Header; import org.apache. ...