M斐波那契数列

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 1534    Accepted Submission(s): 435

Problem Description
M斐波那契数列F[n]是一种整数数列,它的定义如下:

F[0] = a
F[1] = b
F[n] = F[n-1] * F[n-2] ( n > 1 )

现在给出a, b, n,你能求出F[n]的值吗?

 
Input
输入包含多组测试数据;
每组数据占一行,包含3个整数a, b, n( 0 <= a, b, n <= 10^9 )
 
Output
对每组测试数据请输出一个整数F[n],由于F[n]可能很大,你只需输出F[n]对1000000007取模后的值即可,每组数据输出一行。
 
Sample Input
01 0
6 10 2
 
Sample Output
0
60
 
Source
 
F[n] = F[n-1] * F[n-2] ---》 F[n] = F[n-2]^2* F[n-3]^1----》F[n] = F[n-3]^3* F[n-4]^2----》
 F[n] = F[n-4]^5* F[n-5]^3  -----......--->F[n] = F[2]^a[n-1]* F[1]^a[n-2];    //我们可以的到处a[n]为一个斐波那契数列
但是对于这样一个式子:
      F[n] = F[2]^a[n-1]* F[1]^a[n-2];  我们依旧还是不好处理哇,毕竟n<1e9这么大,这样我们不妨引用小费马引理处理....
    首先我们应该知道小费马引理的定义: 
                           形如:  (a^b)mod c = a^(b mod (c-1) ) mod c;
    这样,我们就可以找到这样一个方法来做这道题:
                  F[n] = F[2]^a[n-1]* F[1]^a[n-2];   可以写成  F[n] = (F[2]^(a[n-1]%(mod-1))* F[1]^(a[n-2]%(mod-1)))%mod;
     可以明确的是,F[2],F[1]我们事先已经知道,所以问题在于求解a[n-1],a[n-2]由于数据巨大,为了提升效率我们可以使用矩阵快速幂来求解
     对于  a[n]=a[n-1]+a[n-2]  a[0]=a[1]=1;  这样的斐波那契数列,我们应该不难构造出它的矩阵来
      |a[n]   |   =|1,1|^(n-2)  |a[n-1]|
      |a[n-1]|     |1,0|*          |a[n-2]|
得到了 a[n],a[n-1]之后我们在使用一个快速幂求解 a^b 即可。
代码:
 //#define LOCAL
#include<iostream>
#include<cstdio>
#include<cstring>
#define LL __int64
using namespace std;
const int mod =; LL mat[][];
LL ans[][];
LL n,aa,bb; void Matrix(LL a[][],LL b[][])
{
LL cc[][]={};
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
for(int k=;k<;k++)
{
cc[i][j]=(cc[i][j]+a[i][k]*b[k][j])%(mod-);
}
}
}
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
a[i][j]=cc[i][j];
}
}
} void pow(LL w)
{
mat[][]=mat[][]=mat[][]=;
mat[][]=; while(w>)
{
if(w&) Matrix(ans,mat);
w>>=;
if(w==)break;
Matrix(mat,mat);
}
}
LL pow_int(LL a,LL b)
{
LL ans=;
while(b>)
{
if(b&){
ans*=a;
ans%=mod;
}
b>>=;
if(b==)break;
a*=a;
a%=mod;
}
return ans;
}
void input(LL w)
{
ans[][]=ans[][]=;
ans[][]=ans[][]=;
pow(w-);
LL fn_2=(ans[][]+ans[][])%(mod-);
pow();
LL fn_1=(ans[][]+ans[][])%(mod-);
printf("%I64d\n",(pow_int(aa,fn_2)*pow_int(bb,fn_1))%mod);
} int main()
{
#ifdef LOCAL
freopen("test.in","r",stdin);
#endif
while(scanf("%I64d%I64d%I64d",&aa,&bb,&n)!=EOF)
if(n==)printf("%I64d\n",aa);
else if(n==)printf("%I64d\n",bb);
else
input(n);
return ;
}

HDU----(4549)M斐波那契数列(小费马引理+快速矩阵幂)的更多相关文章

  1. HDOJ 4549 M斐波那契数列 费马小定理+矩阵高速幂

    MF( i ) = a ^ fib( i-1 ) * b ^ fib ( i )   ( i>=3) mod 1000000007 是质数 , 依据费马小定理  a^phi( p ) = 1 ( ...

  2. HDU 4549 M斐波那契数列(矩阵快速幂+费马小定理)

    M斐波那契数列 Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other) Total Submi ...

  3. hdu 4549 M斐波那契数列(快速幂 矩阵快速幂 费马小定理)

    题目链接http://acm.hdu.edu.cn/showproblem.php?pid=4549: 题目是中文的很容易理解吧.可一开始我把题目看错了,这毛病哈哈. 一开始我看错题时,就用了一个快速 ...

  4. [HDU 4549] M斐波那契数列

    M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Sub ...

  5. 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)} ...

  6. hdu 4549 M斐波那契数列 矩阵快速幂+欧拉定理

    M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Problem ...

  7. 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... ...

  8. 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]. 思路:手算一下可以发现 ...

  9. Luogu 1349 广义斐波那契数列(递推,矩阵,快速幂)

    Luogu 1349 广义斐波那契数列(递推,矩阵,快速幂) Description 广义的斐波那契数列是指形如\[A_n=p*a_{n-1}+q*a_{n-2}\]的数列.今给定数列的两系数p和q, ...

随机推荐

  1. CodeForces 454C Little Pony and Expected Maximum

    Little Pony and Expected Maximum Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I6 ...

  2. The property 'RowId' is part of the object's key information and cannot be modified.

    2016-10-20 10:19:46,667 [12] ERROR ClientApp.FormDownload - ErrorSystem.InvalidOperationException: T ...

  3. linux挂载共享文件夹

    挂载windows共享目录或FTP: 方式一:包含密码 Shell代码 收藏代码 sudo mount //192.168.10.22/FTPServer /windows -o username=u ...

  4. CUBRID学习笔记 9 创建示例数据库

    如果你安装的时候没有装数据库,可以后期装 其实和上文基本差不多. cd ~ mkdir CUBRID_databases cd CUBRID_databases mkdir demodb cd dem ...

  5. python tools: iPython Notebook

    Introducing IPython Notebook IPython isn't a different programming language, it's just a set of comp ...

  6. HTML笔记(四) 框架

    通过框架,可以在一个窗口显示多个页面.而所谓的框架,就是指每一份HTML文档. 框架结构标签<frameset> 定义如何将窗口分割为框架. frameset定义了一系列的行列. rows ...

  7. swipejs的bug

    Github:https://github.com/thebird/Swipe 以下bug的修复方式皆来自于网上. 现在最新的版本是2.0,bug如下: 1.触摸后不会自动播放 修复方式, funct ...

  8. XAF Excel数据导入模块使用说明与源码

    我实现了XAF项目中Excel数据的导入,使用Devexpress 新出的spreadsheet控件,可能也不新了吧:D 好,先看一下效果图:下图是Web版本的. 下面是win版: 功能说明: 支持从 ...

  9. Nginx基础知识————生成自签名ca 证书 使nginx 支持https

    创建服务器私钥,命令会让你输入一个口令: $ openssl genrsa -des3 -out server.key 1024 创建签名请求的证书(CSR): $ openssl req -new ...

  10. jqurey click和blur执行时间冲突

    参考资料:http://stackoverflow.com/questions/10652852/jquery-fire-click-before-blur-event