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

时间:2023-11-25 17:58:26

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

60
Source

题意:不解释,中文题。

思路:费马小定理+矩阵快速幂

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4549

转载请注明出处:M斐波那契数列(矩阵快速幂+费马小定理)寻找&星空の孩子

#include<cstring>
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
#define LL __int64
const __int64 mod=; struct matrix
{
LL mat[][];
};
matrix multiply(matrix a,matrix b)
{
matrix c;
memset(c.mat,,sizeof(c.mat));
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
if(a.mat[i][j]==)continue;
for(int k=;k<;k++)
{
if(b.mat[j][k]==)continue;
c.mat[i][k]=(c.mat[i][k]+a.mat[i][j]*b.mat[j][k])%(mod-);//加法不能简写成..+=...%..
}
}
}
return c;
}
matrix matrixquicklymod(matrix x,LL m)
{
matrix res;
// memset(res.mat,0,sizeof(res.mat));
for(int i=;i<;i++)
for(int j=;j<;j++)
res.mat[i][j]=(i==j);
while(m)
{
if(m&) res=multiply(res,x);
m>>=;
x=multiply(x,x);
}
return res;
} LL qmod(LL x, LL y)
{
LL z=;
while(y)
{
if(y&) z=(z*x)%mod;
y>>=;
x=(x*x)%mod;
}
return z;
}
int main()
{
LL a,b,n,ma,mb;
while(scanf("%I64d%I64d%I64d",&a,&b,&n)!=EOF)
{
matrix ans;
ma=mb=;//幂
ans.mat[][]=;
ans.mat[][]=;
ans.mat[][]=;
ans.mat[][]=;
if(n==)
printf("%I64d\n",a%mod);
else if(n==)
printf("%I64d\n",b%mod);
else if(n==)
printf("%I64d\n",((a%mod)*(b%mod))%mod);
else
{
ans=matrixquicklymod(ans,n-);
mb=ans.mat[][]+ans.mat[][];
ma=ans.mat[][]+ans.mat[][];
ma=ma%(mod-);
mb=mb%(mod-);
a=qmod(a,ma);
b=qmod(b,mb);
printf("%I64d\n",((a%mod)*(b%mod))%mod);
}
}
return ;
}

慢慢提升吧。。。。。少年!