HDU----(4549)M斐波那契数列(小费马引理+快速矩阵幂)
M斐波那契数列
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 1534 Accepted Submission(s): 435
F[0] = a
F[1] = b
F[n] = F[n-1] * F[n-2] ( n > 1 )
现在给出a, b, n,你能求出F[n]的值吗?
每组数据占一行,包含3个整数a, b, n( 0 <= a, b, n <= 10^9 )
60
//#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斐波那契数列(小费马引理+快速矩阵幂)的更多相关文章
- HDOJ 4549 M斐波那契数列 费马小定理+矩阵高速幂
MF( i ) = a ^ fib( i-1 ) * b ^ fib ( i ) ( i>=3) mod 1000000007 是质数 , 依据费马小定理 a^phi( p ) = 1 ( ...
- HDU 4549 M斐波那契数列(矩阵快速幂+费马小定理)
M斐波那契数列 Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other) Total Submi ...
- 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)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/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Problem ...
- 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... ...
- 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]. 思路:手算一下可以发现 ...
- Luogu 1349 广义斐波那契数列(递推,矩阵,快速幂)
Luogu 1349 广义斐波那契数列(递推,矩阵,快速幂) Description 广义的斐波那契数列是指形如\[A_n=p*a_{n-1}+q*a_{n-2}\]的数列.今给定数列的两系数p和q, ...
随机推荐
- CodeForces 454C Little Pony and Expected Maximum
Little Pony and Expected Maximum Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I6 ...
- 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 ...
- linux挂载共享文件夹
挂载windows共享目录或FTP: 方式一:包含密码 Shell代码 收藏代码 sudo mount //192.168.10.22/FTPServer /windows -o username=u ...
- CUBRID学习笔记 9 创建示例数据库
如果你安装的时候没有装数据库,可以后期装 其实和上文基本差不多. cd ~ mkdir CUBRID_databases cd CUBRID_databases mkdir demodb cd dem ...
- python tools: iPython Notebook
Introducing IPython Notebook IPython isn't a different programming language, it's just a set of comp ...
- HTML笔记(四) 框架
通过框架,可以在一个窗口显示多个页面.而所谓的框架,就是指每一份HTML文档. 框架结构标签<frameset> 定义如何将窗口分割为框架. frameset定义了一系列的行列. rows ...
- swipejs的bug
Github:https://github.com/thebird/Swipe 以下bug的修复方式皆来自于网上. 现在最新的版本是2.0,bug如下: 1.触摸后不会自动播放 修复方式, funct ...
- XAF Excel数据导入模块使用说明与源码
我实现了XAF项目中Excel数据的导入,使用Devexpress 新出的spreadsheet控件,可能也不新了吧:D 好,先看一下效果图:下图是Web版本的. 下面是win版: 功能说明: 支持从 ...
- Nginx基础知识————生成自签名ca 证书 使nginx 支持https
创建服务器私钥,命令会让你输入一个口令: $ openssl genrsa -des3 -out server.key 1024 创建签名请求的证书(CSR): $ openssl req -new ...
- jqurey click和blur执行时间冲突
参考资料:http://stackoverflow.com/questions/10652852/jquery-fire-click-before-blur-event