MF( i ) = a ^ fib( i-1 ) * b ^ fib ( i )   ( i>=3)

mod 1000000007
是质数 , 依据费马小定理  a^phi( p ) = 1 ( mod p )  这里 p 为质数 且 a 比 p小 所以 a^( p - 1 ) = 1 ( mod p )

所以对非常大的指数能够化简  a ^ k % p  == a ^ ( k %(p-1) ) % p

用矩阵高速幂求fib数后代入就可以

M斐波那契数列

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)

Total Submission(s): 1672    Accepted Submission(s): 482

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
 

/* ***********************************************
Author :CKboss
Created Time :2015年03月12日 星期四 22时44分35秒
File Name :HDOJ4549.cpp
************************************************ */ #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map> using namespace std; typedef long long int LL; const LL mod=1000000007LL;
const LL md=1000000006LL; /// getfib
LL a,b,n; struct Matrix
{
Matrix(LL a=0,LL b=0,LL c=0,LL d=0)
{
m[0][0]=a; m[0][1]=b;
m[1][0]=c; m[1][1]=d;
}
LL m[2][2];
}; Matrix MUI(Matrix& a,Matrix& b)
{
Matrix ret;
ret.m[0][0]=((a.m[0][0]*b.m[0][0])%md+(a.m[0][1]*b.m[1][0])%md)%md;
ret.m[0][1]=((a.m[0][0]*b.m[0][1])%md+(a.m[0][1]*b.m[1][1])%md)%md;
ret.m[1][0]=((a.m[1][0]*b.m[0][0])%md+(a.m[1][1]*b.m[1][0])%md)%md;
ret.m[1][1]=((a.m[1][0]*b.m[0][1])%md+(a.m[1][1]*b.m[1][1])%md)%md;
return ret;
} Matrix QUICKPOW(LL m)
{
Matrix E(1,0,0,1);
Matrix A(1,1,1,0);
while(m)
{
if(m&1LL) E=MUI(E,A);
A=MUI(A,A);
m/=2LL;
}
return E;
} void showMat(Matrix M)
{
cout<<endl;
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
cout<<M.m[i][j]<<",";
cout<<endl;
}
cout<<endl;
} /// get p_th fib number
LL getfib(LL p)
{
p--;
Matrix M1=QUICKPOW(p);
return M1.m[0][0];
} LL QUICKPOW2(LL a,LL x)
{
LL e=1LL;
while(x)
{
if(x&1LL) e=(e*a)%mod;
a=(a*a)%mod;
x/=2LL;
}
return e;
} LL solve()
{
if(n==0) return a;
else if(n==1) return b;
else if(n==2) return (a*b)%mod; ///a的fib系数 -> fib(n-1)
LL xa = getfib(n-1);
LL partA = QUICKPOW2(a,xa); ///b的fib系数 -> fib(i)
LL xb = getfib(n);
LL partB = QUICKPOW2(b,xb); return (partA*partB)%mod;
} int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout); while(cin>>a>>b>>n)
cout<<solve()<<endl; return 0;
}

HDOJ 4549 M斐波那契数列 费马小定理+矩阵高速幂的更多相关文章

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

    题目链接:https://vjudge.net/problem/HDU-4549 M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others)    Memory Li ...

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

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

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

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

  4. HDU 4549 (费马小定理+矩阵快速幂+二分快速幂)

    M斐波那契数列 Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Statu ...

  5. HDU 1005 Number Sequence【斐波那契数列/循环节找规律/矩阵快速幂/求(A * f(n - 1) + B * f(n - 2)) mod 7】

    Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  6. HDOJ 5667 Sequence//费马小定理 矩阵快速幂

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5667 题意:如题给了一个函数式,给你a,b,c,n,p的值,叫你求f(n)%p的值 思路:先对函数取以a为 ...

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

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

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

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

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

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

随机推荐

  1. ubuntu中wifi显示被硬件禁用的解决方法

    本人使用的电脑是华硕X550C,安装了ubuntu16.04版本. 联网的时候显示“wifi已经通过硬件开关禁用”.按Fn+F2无法开启wifi.通过rfkill命令无法也无法开启wifi. 经过了解 ...

  2. MAC 使用技巧及常用软件备忘

    公司转向MAC快一年, 换了MAC PRO半年时间,MAC这东西除了颜值和性能,软件真是不如WINDOWS啊,不是没有,只是好多都收费! 先介绍几个跨平台的. WIN+MAC 通用: 浏览器: CHR ...

  3. objc语言的运行时处理

    在Objective-C中,消息是通过objc_msgSend()这个runtime方法及相近的方法来实现的.这个方法需要一个target,selector,还有一些参数.理论上来说,编译器只是把消息 ...

  4. 输入N,打印如图所看到的的三角形(例:N=3,N=4,N=5)1&lt;=N&lt;=26

    package demo; public class PrintDemo { public static void main(String[] args) { print(26); } private ...

  5. IOS Appstore价格表

  6. Problem-1001:Sum Problem

    Sum Problem Sample code : #include <stdio.h> int main() { int i,n; int sum; while(scanf(" ...

  7. js var ImgObj=new Image();

    API地址: 1 https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement 下面来看看Image到底是个什么东东,我先将Ima ...

  8. 趣味 console.log

    第三方趣味console,比我的强太多了,使用这个吧: https://github.com/yy0608/console 我的console效果图: ;(function (global, fact ...

  9. js 判断浏览器内核

    function getOs()  {      var OsObject = "";     if(navigator.userAgent.indexOf("MSIE& ...

  10. chrome 非安全模式解决开发跨域问题

    这个参数可以降低chrome浏览器的安全性,禁用同源策略,利于开发人员本地调试. ps:如果是mac用户(记得 Command + Q 关闭chrome):  open -a Google\ Chro ...