题目链接: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)} * b^{fib(n)} $

斐波那契数列可用矩阵快速幂求解。但是此题中n较大,fib会爆掉。这时候需要引入费马小定理优化。

证明:$a^x \% p = a^{x \%(p-1)} \%p$

1. $a^x \% p = a^{x \% (p-1) + x/(p-1)*(p-1)} \% p$

2. $a^x \% p = a^{x \% (p-1)} * a^{x/(p-1)*(p-1)} \%p$

3. $a^{x/(p-1)*(p-1)} \% p= ({a^{p-1}}) ^ {(x/(p-1))} \%p = 1^ {(x/(p-1))}$

把3式带入2式,即可证明。

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 2
using namespace std; typedef long long ll;
const ll mod=; struct mat
{
ll m[N][N]=
{
{,},
{,}
};
}; mat mul(mat a,mat b,ll p)
{
mat ans;
int i,j,k;
for(i=;i<N;i++)
for(j=;j<N;j++)
ans.m[i][j]=; for(i=;i<N;i++)
for(j=;j<N;j++)
for(k=;k<N;k++)
ans.m[i][j]=(ans.m[i][j]+a.m[i][k]*b.m[k][j])%p;
return ans;
} ll matpow(ll n,ll p)
{
if(n<) return ;
mat ans,tmp;
int i,j;
for(int i=;i<N;i++)
for(int j=;j<N;j++)
ans.m[i][j]=; ans.m[][]=;
ans.m[][]=;
while(n)
{
if(n&) ans=mul(tmp,ans,p);
tmp=mul(tmp,tmp,p);
n=n>>;
}
return ans.m[][]%p;
} ll fast_mod(ll a,ll b,ll p){
ll res=;
while(b){
if(b&) res=(res*a)%p;
a=(a*a)%p;
b>>=;
}
return res;
} int main(){
ll n,a,b; while(scanf("%lld%lld%lld",&a,&b,&n)!=EOF){
if(n==){
printf("%lld\n",a);
continue;
}
else if(n==){
printf("%lld\n",b);
continue;
}
else if(a==||b==){
printf("0\n");
continue;
}
ll m1=matpow(n-,mod-);
ll m2=matpow(n,mod-);
ll ans=(fast_mod(a,m1,mod)*fast_mod(b,m2,mod))%mod;
printf("%lld\n",ans%mod);
} return ;
}

HDU 4549 M斐波那契数列(矩阵快速幂)的更多相关文章

  1. hdu 4549 M斐波那契数列 矩阵快速幂+欧拉定理

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

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

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

  3. HDU4549 M斐波那契数列 矩阵快速幂+欧拉函数+欧拉定理

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

  4. 51nod1242 斐波那契数列 矩阵快速幂

    1242 斐波那契数列的第N项 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 #include<stdio.h> #define mod 100000000 ...

  5. POJ3070 斐波那契数列 矩阵快速幂

    题目链接:http://poj.org/problem?id=3070 题意就是让你求斐波那契数列,不过n非常大,只能用logn的矩阵快速幂来做了 刚学完矩阵快速幂刷的水题,POJ不能用万能头文件是真 ...

  6. hdu4549 M斐波那契数列 矩阵快速幂+快速幂

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

  7. 洛谷P1962 斐波那契数列(矩阵快速幂)

    题目背景 大家都知道,斐波那契数列是满足如下性质的一个数列: • f(1) = 1 • f(2) = 1 • f(n) = f(n-1) + f(n-2) (n ≥ 2 且 n 为整数) 题目描述 请 ...

  8. POJ 3070 Fibonacci【斐波那契数列/矩阵快速幂】

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17171   Accepted: 11999 Descr ...

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

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

  10. [HDU 4549] M斐波那契数列

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

随机推荐

  1. MySQL使用select查询时,在查询结果中增加一个字段并指定固定值

    假设需求是这样的: mysql> desc user; +-------+----------+------+-----+---------+----------------+ | Field ...

  2. CentOS云厂商清单

    Download CentOShttps://www.centos.org/download/ Download - CentOS Wikihttps://wiki.centos.org/Downlo ...

  3. [官网]How to use distributed transactions with SQL Server on Docker

    How to use distributed transactions with SQL Server on Docker https://docs.microsoft.com/en-us/sql/l ...

  4. Docker操作删除所有容器镜像

    借鉴博客:https://www.cnblogs.com/yanyouqiang/p/8301856.html https://blog.csdn.net/wy_97/article/details/ ...

  5. Kafka-Flume-elasticsearch

    a1.sources = kafkaSource a1.channels = memoryChannel a1.sinks = elasticsearch a1.sources.kafkaSource ...

  6. rsync: chgrp "/.hosts.NBCxBB" (in test) failed: Operation not permitted (1)

    #记一次rsync出现的错误(网上基本都是说权限问题) #这并不是权限的问题,应为实际的文件已经传过去了,但是rsync就是会报这个错误,(虽然使用是正常的,但是看着就是不爽) [root@local ...

  7. DNS_PROBE_FINISHED_NXDOMAIN & MacOS

    DNS_PROBE_FINISHED_NXDOMAIN 内网 DNS bug 8.8.8.8 8.8.4.4 # new inner Wi-Fi 10.1.3.10 10.1.3.13 Windows ...

  8. LeetCode & Online Programming Learning Platform

    leetcode LeetCode is the best platform to help you enhance your skills, expand your knowledge and pr ...

  9. TestNG之测试执行后没有生成默认测试报告(IDEA)

    使用IDEA+TestNG进行测试,没有生成 测试报告,是因为没有勾选监听器使用默认报告,具体操作如下: “Run” -> "Edit Configurations" -&g ...

  10. mybatis-spring-1.2.2.jar下载地址

    http://www.java2s.com/Code/Jar/m/Downloadmybatisspring120jar.htm