题目链接:https://vjudge.net/problem/HDU-4549

M斐波那契数列

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

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
0 1 0
6 10 2
 
Sample Output
0
60
 
Source
 
Recommend
liuyiding

题解:

1.可知f[n]中a、b的指数符合斐波那契数列,因而可用矩阵快速幂求出。

2.然而如果指数不求模,也可能会溢出。但指数又怎样求模呢?

有如下定理:当m为素数,且a、n互质时, a^n % m = a^(n%(m-1)) % m

证明:

根据费马小定理,当m为素数,且a、p互质时, a^(m-1) ≡ 1 (mod m),

a^n = a^(k*(m-1)+r) = (a^(m-1))^k * a^r,其中 r = n%(m-1),

那么 a^n % m = ( (a^(m-1))^k * a^r ) % m =  (a^(m-1))^k % m * a^r % m = 1 * a^r % m = a^(n%(m-1)) % m 。

所以:当m为素数,且a、n互质时, a^n % m = a^(n%(m-1)) % m

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = ;
const int MAXN = 1e6+; const int Size = ;
struct MA
{
LL mat[Size][Size];
void init()
{
for(int i = ; i<Size; i++)
for(int j = ; j<Size; j++)
mat[i][j] = (i==j);
}
}; MA mul(MA x, MA y)
{
MA ret;
memset(ret.mat, , sizeof(ret.mat));
for(int i = ; i<Size; i++)
for(int j = ; j<Size; j++)
for(int k = ; k<Size; k++)
ret.mat[i][j] += (1LL*x.mat[i][k]*y.mat[k][j])%(MOD-), ret.mat[i][j] %= (MOD-);
return ret;
} MA qpow(MA x, LL y)
{
MA s;
s.init();
while(y)
{
if(y&) s = mul(s, x);
x = mul(x, x);
y >>= ;
}
return s;
} LL q_pow(LL x, LL y)
{
LL s = ;
while(y)
{
if(y&) s *= x, s %= MOD;
x *= x, x %= MOD;
y >>= ;
}
return s;
} MA tmp = {
, ,
,
}; int main()
{
LL f[], n;
while(scanf("%lld%lld%lld",&f[],&f[],&n)!=EOF)
{
if(n<=)
{
printf("%lld\n", f[n]%MOD);
continue;
} MA s = tmp;
s = qpow(s, n-);
LL p1 = s.mat[][];
LL p2 = s.mat[][];
LL ans = q_pow(f[], p2)*q_pow(f[], p1)%MOD;
printf("%lld\n", ans);
}
}

HDU4549 M斐波那契数列 —— 斐波那契、费马小定理、矩阵快速幂的更多相关文章

  1. 【费马小定理+矩阵快速幂】HDU4549——M斐波那契数列

    [题目大意] M斐波那契数列F[n]是一种整数数列,它的定义如下:F[0] = aF[1] = bF[n] = F[n-1] * F[n-2] ( n > 1 )现在给出a, b, n,求出F[ ...

  2. bzoj5118: Fib数列2(费马小定理+矩阵快速幂)

    题目大意:求$fib(2^n)$ 就是求fib矩阵的(2^n)次方%p,p是质数,根据费马小定理有 注意因为模数比较大会爆LL,得写快速乘法... #include<bits/stdc++.h& ...

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

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

  4. 【bzoj5118】Fib数列2 费马小定理+矩阵乘法

    题目描述 Fib定义为Fib(0)=0,Fib(1)=1,对于n≥2,Fib(n)=Fib(n-1)+Fib(n-2) 现给出N,求Fib(2^n). 输入 本题有多组数据.第一行一个整数T,表示数据 ...

  5. Fib数列2 费马小定理+矩阵乘法

    题解: 费马小定理 a^(p-1)=1(mod p) 这里推广到矩阵也是成立的 所以我们可以对(2^n)%(p-1) 然后矩阵乘法维护就好了 模数较大使用快速乘

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

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

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

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

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

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

  9. hdu 4549 M斐波拉契 (矩阵快速幂 + 费马小定理)

    Problem DescriptionM斐波那契数列F[n]是一种整数数列,它的定义如下: F[0] = aF[1] = bF[n] = F[n-1] * F[n-2] ( n > 1 ) 现在 ...

随机推荐

  1. Boost Asio介绍--之一

    原文:http://www.tuicool.com/articles/YbeYR3 Boost Asio介绍--之一 时间 2014-03-26 17:57:39  CSDN博客 原文  http:/ ...

  2. gitlab升级、汉化、修改root密码

    1.gitlab升级 # 查看当前版本 head -1 /opt/gitlab/version-manifest.txt gitlab-ce 8.9.5 grep "^external_ur ...

  3. FileChannel指南

    推荐关注公众号:锅外的大佬 每日推送国外技术好文,帮助每位开发者更优秀地成长 原文链接:https://www.baeldung.com/java-filechannel 作者:baeldung 译者 ...

  4. solr 最佳实践

    管理页面 页面地址:http://{ip}:{port}/solr/#/ 管理页面的data-import页可以手动重建索引,configuration指定了数据源,重建索引也可以通过http请求触发 ...

  5. android wifi相关模块 命令列表 汇总

    static final int BASE =Protocol.BASE_WIFI;  131072 static final intCMD_START_SUPPLICANT  = BASE +11; ...

  6. 2016 第七届蓝桥杯 c/c++ B组省赛真题及解题报告

    2016 第七届蓝桥杯 c/c++ B组省赛真题及解题报告 勘误1:第6题第4个 if最后一个条件粗心写错了,答案应为1580. 条件应为abs(a[3]-a[7])!=1,宝宝心理苦啊.!感谢zzh ...

  7. vue2.0 + vux (五)api接口封装 及 首页 轮播图制作

    1.安装 jquery 和 whatwg-fetch (优雅的异步请求API) npm install jquery --save npm install whatwg-fetch --save 2. ...

  8. mvc已添加了具有相同键的项

    异常详细信息: System.ArgumentException: 已添加了具有相同键的项. 场景重现:在地址栏输入  http://localhost:51709/Home/Index?user[0 ...

  9. C# 将cookie写入WebBrowser

    string cookie = ""; foreach (string c in cookie.Split(';')) { string[] item = c.Split('=') ...

  10. allegro设置鼠标滚轮放大缩小

    allegro设置鼠标滚轮放大缩小 allegro16版本以增加可以通过鼠标滚轮进行PCB的放大缩小.具体方法如下: 首先在HOME路径下找到PCBENV文件夹,进入该文件夹打开ENV文件. 在ENV ...