标签:
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数后代入即可
0 1 0 6 10 2
0 60
/* ***********************************************
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;
}
标签:
原文地址:http://blog.csdn.net/ck_boss/article/details/44230643