HDU4549 M斐波那契数
M斐波那契数列
题目分析:
M斐波那契数列F[n]是一种整数数列,它的定义例如以下:
F[0] = a
F[1] = b
F[n] = F[n-1] * F[n-2] ( n > 1 )
如今给出a, b, n,你能求出F[n]的值吗?
算法分析:
经过前面几项的推导,你会发现当中a,b的个数为斐波那契数同样。而我们知道斐波那契数是到20项后就会非常大,所以要处理。而我们依据欧拉定理(费马小定理)可知道
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std; typedef long long LL;
const int MOD = 1000000007;
struct Matrix{
LL mat[2][2];
LL row,col;
Matrix(){};
Matrix(LL _r,LL _c):row(_r),col(_c){};
}; Matrix I(2,2),P(2,2); void Init(){
P.mat[0][0] = 0;
P.mat[0][1] = P.mat[1][0] = P.mat[1][1] = 1; I.mat[0][0] = I.mat[1][1] = 1;
I.mat[0][1] = I.mat[1][0] = 0;
} //矩阵相乘
Matrix mul(Matrix A,Matrix B,LL mod){
Matrix C(2,2);
memset(C.mat,0,sizeof(C.mat)); for(int i = 0;i < A.row;++i){
for(int k = 0;k < B.row;++k){
for(int j = 0;j < B.col;++j){
C.mat[i][j] = (C.mat[i][j] + A.mat[i][k] * B.mat[k][j] % mod) % mod;
}
}
}
return C;
} //fib[n] % (c - 1)
Matrix powmod(Matrix A,LL n,LL mod){
Matrix B = I;
while(n > 0){
if(n & 1) B = mul(B,A,mod);
A = mul(A,A,mod);
n >>= 1;
}
return B;
} //a ^ b % c
LL powmod(LL a,LL n,LL mod){
LL res = 1;
while(n > 0){
if(n & 1) res = (res * a) % mod;
a = a * a % mod;
n >>= 1;
}
return res;
} int main()
{
Init();
LL a,b,n;
while(~scanf("%I64d%I64d%I64d",&a,&b,&n)){
Matrix A = powmod(P,n,MOD - 1); //fib[n] % (mod -1 )
printf("%I64d\n",powmod(a,A.mat[0][0],MOD) * powmod(b,A.mat[1][0],MOD) % MOD);
}
return 0;
}
HDU4549 M斐波那契数的更多相关文章
- UVA 11582 Colossal Fibonacci Numbers! 大斐波那契数
大致题意:输入两个非负整数a,b和正整数n.计算f(a^b)%n.其中f[0]=f[1]=1, f[i+2]=f[i+1]+f[i]. 即计算大斐波那契数再取模. 一开始看到大斐波那契数,就想到了矩阵 ...
- 斐波那契数[XDU1049]
Problem 1049 - 斐波那契数 Time Limit: 1000MS Memory Limit: 65536KB Difficulty: Total Submit: 1673 Ac ...
- C++求斐波那契数
题目内容:斐波那契数定义为:f(0)=0,f(1)=1,f(n)=f(n-1)+f(n-2)(n>1且n为整数) 如果写出菲氏数列,则应该是: 0 1 1 2 3 5 8 13 21 34 …… ...
- Project Euler 104:Pandigital Fibonacci ends 两端为全数字的斐波那契数
Pandigital Fibonacci ends The Fibonacci sequence is defined by the recurrence relation: F[n] = F[n-1 ...
- DP:斐波纳契数
题目:输出第 n 个斐波纳契数(Fibonacci) 方法一.简单递归 这个就不说了,小n怡情,大n伤身啊……当n=40的时候,就明显感觉到卡了,不是一般的慢. //输出第n个 Fibonacci 数 ...
- HDU 5914 Triangle(打表——斐波那契数的应用)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5914 Problem Description Mr. Frog has n sticks, whos ...
- [Swift]LeetCode509. 斐波那契数 | Fibonacci Number
The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such th ...
- HDU 1021(斐波那契数与因子3 **)
题意是说在给定的一种满足每一项等于前两项之和的数列中,判断第 n 项的数字是否为 3 的倍数. 斐波那契数在到第四十多位的时候就会超出 int 存储范围,但是题目问的是是否为 3 的倍数,也就是模 3 ...
- POJ 3070(求斐波那契数 矩阵快速幂)
题意就是求第 n 个斐波那契数. 由于时间和内存限制,显然不能直接暴力解或者打表,想到用矩阵快速幂的做法. 代码如下: #include <cstdio> using namespace ...
随机推荐
- 判断圆和矩形是否相交C - Rectangle and Circle
Description Given a rectangle and a circle in the coordinate system(two edges of the rectangle are p ...
- 基于FPGA的DW8051移植(一)
最近 半个月都在移植8051,看到DW8051内核资料比较齐全又是新思发布的,所以就开始玩弄 可是这半个月的努力几近白费 —— 移植失败了,不知道从何着手这个内核.可能大家能找到不同的版本,我的是最初 ...
- 设置QPushButton的平面与突出(遍历控件)
#include "ui_maindialog.h" #include "maindialog.h" #include <QState> #incl ...
- python模块 mysql-python安装(在ubuntu系统下)
直接运行如下命令 sudo pip install MySQL-python 报如下错误 xxx@ubuntu:~$ sudo pip install MySQL-python Downloading ...
- docker学习笔记16:Dockerfile 指令 ADD 和 COPY介绍
一.ADD指令 ADD指令的功能是将主机构建环境(上下文)目录中的文件和目录.以及一个URL标记的文件 拷贝到镜像中. 其格式是: ADD 源路径 目标路径 如: #test FROM ubunt ...
- Cshap 使用http发起请求.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 using System; using System.I ...
- HDU 1150:Machine Schedule(二分匹配,匈牙利算法)
Machine Schedule Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- ASP.NET aspx页面中 写C#脚本; ASP.NET 指令(<%@%>);
1 <h2>Welcome</h2> <ul> <% for (int i = 0; i <= Convert.ToInt32(ViewData[&qu ...
- shell中exec解析(转)
参考:<linux命令.编辑器与shell编程> <unix环境高级编程> exec和source都属于bash内部命令(builtins commands),在bash下输入 ...
- first-,second- and third-class value
In computer science, a programming language is said to have first-class functions if it treats funct ...