HDU 5667 Sequence(矩阵快速幂)
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.
fn=⎧⎩⎨⎪⎪1,ab,abfcn−1fn−2,n=1n=2otherwise
He gives you 5 numbers n,a,b,c,p,and he will eat fn foods.But there are only p foods,so you should tell him fn 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,p is a prime number,and p≤109+7.
Output
Output one number for each case,which is fn mod p.
Sample Input
1
5 3 3 3 233
Sample Output
190
用矩阵快速幂的时候,注意对p-1取余
递推式:a[n]=c*a[n-1]+a[n-2]+1;
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <algorithm>
#include <math.h>
using namespace std;
typedef long long int LL;
struct Node
{
LL a[3][3];
}A,B,C;
LL p,n,a,b,c;
Node multiply(Node a,Node b)
{
Node c;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
c.a[i][j]=0;
for(int k=0;k<3;k++)
{
(c.a[i][j]+=(a.a[i][k]*b.a[k][j])%(p-1))%=(p-1);
}
}
}
return c;
}
Node get(Node a,LL x)
{
Node c;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
c.a[i][j]=(i==j?1:0);
for(x;x;x>>=1)
{
if(x&1) c=multiply(c,a);
a=multiply(a,a);
}
return c;
}
LL quick(LL x,LL y)
{
if(n>1&&y==0) y=p-1;
LL ans=1;
for(y;y;y>>=1)
{
if(y&1) ans=(ans*x)%p;
x=(x*x)%p;
}
return ans;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%lld%lld%lld%lld%lld",&n,&a,&b,&c,&p);
A.a[0][0]=0;A.a[1][0]=0;A.a[2][0]=1;
B.a[0][0]=c; B.a[0][1]=1; B.a[0][2]=1;
B.a[1][0]=1; B.a[1][1]=0; B.a[1][2]=0;
B.a[2][0]=0; B.a[2][1]=0; B.a[2][2]=1;
if(n==1) {cout<<1<<endl;continue;}
B=get(B,n-1);
B=multiply(B,A);
LL num=((B.a[0][0]%(p-1))*(b%(p-1)))%(p-1);
//cout<<num<<endl;
cout<<quick(a,num)<<endl;
}
return 0;
}
HDU 5667 Sequence(矩阵快速幂)的更多相关文章
- 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; ...
- HDU 5667 Sequence 矩阵快速幂
官方题解: 观察递推式我们可以发现,所有的fi都是a的幂次,所以我们可以对fi取一个以a为底的log,gi=loga fi 那么递推式变gi=b+c∗gi−1+ ...
- HDU 5667 构造矩阵快速幂
HDU 5667 构造矩阵快速幂 题目描述 解析 我们根据递推公式 设 则可得到Q的指数关系式 求Q构造矩阵 同时有公式 其中φ为欧拉函数,且当p为质数时有 代码 #include <cstdi ...
- HDU.2640 Queuing (矩阵快速幂)
HDU.2640 Queuing (矩阵快速幂) 题意分析 不妨令f为1,m为0,那么题目的意思为,求长度为n的01序列,求其中不含111或者101这样串的个数对M取模的值. 用F(n)表示串长为n的 ...
- HDU - 1005 Number Sequence 矩阵快速幂
HDU - 1005 Number Sequence Problem Description A number sequence is defined as follows:f(1) = 1, f(2 ...
- HDU 5950 - Recursive sequence - [矩阵快速幂加速递推][2016ACM/ICPC亚洲区沈阳站 Problem C]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 Farmer John likes to play mathematics games with ...
- HDU 1005 Number Sequence(矩阵快速幂,快速幂模板)
Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1 ...
- HDU - 1005 -Number Sequence(矩阵快速幂系数变式)
A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) m ...
- E - Recursive sequence HDU - 5950 (矩阵快速幂)
题目链接:https://vjudge.net/problem/HDU-5950 思路: 构造矩阵,然后利用矩阵快速幂. 1 #include <bits/stdc++.h> 2 #inc ...
- hdu-5667 Sequence(矩阵快速幂+费马小定理+快速幂)
题目链接: Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
随机推荐
- Linux下解压tar.xz
tar xvJf ***.tar.xz 注意零散文件,最好放到文件夹里
- 第7章 Iptables与Firewalld防火墙。
第7章 Iptables与Firewalld防火墙. Chapter7_听较强节奏的音乐能够让您更长时间的投入在学习中. <Linux就该这么学> 00:00/00:00 ...
- Odoo ParseError:"decoder jpeg not available" while parsing....
The reason causing this problem is the plugin PIL install error to solve this problem,try this: 1. c ...
- CSS(二):选择器
一.基本选择器 1.标签选择器 HTML标签作为标签选择器的名称,例如<h1>~<h6>.<p>等. 语法: p{font-size: 16px;} p:标签选择器 ...
- PyCharm中设置菜单字体大小
file——>setting,然后选择appearance,下图右侧红色边框中的内容即设置菜单的字体和大小
- PPT如何一页多张打印且铺满整个页面
最近由于工作需要,有些ppt材料想打印出来学习,但是ppt页数较多,ppt单页打印有些浪费纸张,而且也不能拿到外面打印店去打印,所以只能自己动手设置一页多张打印,并且最后双面打印,这样就做够节省纸张了 ...
- LoadRunner检查点实战之运行查看器
一.为什么要使用检查点 为什么要使用检查点,那就要说明一下LR如何判断脚本是否执行成功.LR判断脚本是否执行成功是根据服务器返回的状态来确定的,如果服务器返回的HTTP状态为 200 OK ,那么Vu ...
- UIWindow小记
If you choose to create a window in Interface Builder, be sure to select the Full Screen at Launch o ...
- Boost-date_time库学习
最近开了boost库的学习,就先从日期-时间库开始吧,boost的date_time库是一个很强大的时间库,用起来还是挺方便的. 以下代码只是入门级的简单学习,更详细的资料参考boost源码. C+ ...
- Git------MyEclipse中使用Git
转载:http://www.mamicode.com/info-detail-928508.html