HDU 4549 M斐波那契数列(矩阵幂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4549
题意:F[0]=a,F[1]=b,F[n]=F[n-1]*F[n-2]。
思路:手算一下可以发现,最后F[n]=a^x*b^y,其中x和y是连续的两项Fib。因此只要求出这两个系数x和y即可。注意这里A^x=A^(x%Phi(C)+Phi(C)) (mod C)。因此在求矩阵快速幂时模的数不是mod=1000000007,而是mod-1。
struct matrix
{
i64 a[2][2];
void init(int x)
{
clr(a,0);
if(x) a[0][0]=a[1][1]=1;
}
matrix operator*(matrix p)
{
matrix ans;
ans.init(0);
int i,j,k;
FOR0(k,2) FOR0(i,2) FOR0(j,2)
{
ans.a[i][j]+=a[i][k]*p.a[k][j]%(mod-1);
ans.a[i][j]%=(mod-1);
}
return ans;
}
matrix pow(int n)
{
matrix ans,p=*this;
ans.init(1);
while(n)
{
if(n&1) ans=ans*p;
p=p*p;
n>>=1;
}
return ans;
}
};
matrix p;
int a,b,n;
i64 Pow(i64 a,i64 b)
{
i64 ans=1;
while(b)
{
if(b&1) ans=ans*a%mod;
a=a*a%mod;
b>>=1;
}
return ans;
}
int main()
{
p.a[0][0]=p.a[1][0]=p.a[0][1]=1;
p.a[1][1]=0;
Rush(a)
{
RD(b,n);
if(n==0) PR(a);
else if(n==1) PR(b);
else
{
matrix temp=p.pow(n-2);
int x=(temp.a[0][1]+temp.a[1][1])%(mod-1);
int y=(temp.a[0][0]+temp.a[1][0])%(mod-1);
PR(Pow(a,x)*Pow(b,y)%mod);
}
}
}
HDU 4549 M斐波那契数列(矩阵幂)的更多相关文章
- hdu 4549 M斐波那契数列(快速幂 矩阵快速幂 费马小定理)
题目链接http://acm.hdu.edu.cn/showproblem.php?pid=4549: 题目是中文的很容易理解吧.可一开始我把题目看错了,这毛病哈哈. 一开始我看错题时,就用了一个快速 ...
- hdu 4549 M斐波那契数列 矩阵快速幂+欧拉定理
M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Problem ...
- [HDU 4549] M斐波那契数列
M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Sub ...
- HDU 4549 M斐波那契数列(矩阵快速幂)
题目链接:M斐波那契数列 题意:$F[0]=a,F[1]=b,F[n]=F[n-1]*F[n-2]$.给定$a,b,n$,求$F[n]$. 题解:暴力打表后发现$ F[n]=a^{fib(n-1)} ...
- HDU 4549 M斐波那契数列(矩阵快速幂+费马小定理)
M斐波那契数列 Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other) Total Submi ...
- hdu 4549 M斐波拉契 (矩阵快速幂 + 费马小定理)
Problem DescriptionM斐波那契数列F[n]是一种整数数列,它的定义如下: F[0] = aF[1] = bF[n] = F[n-1] * F[n-2] ( n > 1 ) 现在 ...
- hdu 4549 M斐波那契数列(矩阵高速幂,高速幂降幂)
http://acm.hdu.edu.cn/showproblem.php?pid=4549 f[0] = a^1*b^0%p,f[1] = a^0*b^1%p,f[2] = a^1*b^1%p... ...
- 斐波那契数列 矩阵乘法优化DP
斐波那契数列 矩阵乘法优化DP 求\(f(n) \%1000000007\),\(n\le 10^{18}\) 矩阵乘法:\(i\times k\)的矩阵\(A\)乘\(k\times j\)的矩 ...
- HDU 1316 (斐波那契数列,大数相加,大数比较大小)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1316 Recall the definition of the Fibonacci numbers: ...
随机推荐
- Andriod wifi 基本操作
从用户角度看,Android Wi-Fi模块自下向上可以看为5层:硬件驱动程序,wpa_suppplicant,JNI,WiFi API,WifiSettings应用程序. 1.wpa_supplic ...
- glog使用
How To Use Google Logging Library Glog 的基本使用方法在google code上有介绍:How To Use Google Logging Library ;最好 ...
- 【android】uiselectoer 自动化测试
转:http://blog.csdn.net/sasoritattoo/article/details/17579739 Android自动化测试主要分为Monkeyrunner.Rubotium.U ...
- 【Ajax】脑补一下 ajax 的options
问题是因为粉红色部分引起的 ,想搞明白 put delete 的应用场景,发现ajax的一些属性也没有完全用过. 参数名 类型 描述 url String (默认: 当前页地址) 发送请求的地址. t ...
- matlab实现高斯牛顿法、Levenberg–Marquardt方法
高斯牛顿法: function [ x_ans ] = GaussNewton( xi, yi, ri) % input : x = the x vector of 3 points % y = th ...
- [原]项目进阶 之 持续构建环境搭建(三)Maven环境搭建
上次的博文项目进阶 之 持续构建环境搭建(二)Nexus私服器中,我们搭建了一个Nexus的maven私服,这次我们来重点讲解一下Maven的安装和配置.这里说明一下这次的环境搭建,比较基础,但却非常 ...
- ibatis.net demo
1. download ibatis.nethttps://code.google.com/p/mybatisnet/ 2. add all dll as reference to your proj ...
- android 设置半透明
对于Button和ImageButton 还有一些View 设置半透明或者透明都是通过 android:background="#b0000000" 这是就是半透明 android ...
- IIS Express start introduction and applicationHost modification
1. First you need create a web project in VS 2. When you finish your project, click start then IIS E ...
- 管道Pipe
管道Pipe java.nio.channels包中含有一个名为Pipe(管道)的类.广义上讲,管道就是一个用来在两个实体之间单向传输数据的导管.管道的概念对于Unix(和类Unix)操作系统的用户来 ...