HDU 4549 M斐波那契数列(矩阵快速幂+费马小定理)
M斐波那契数列
Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 43 Accepted Submission(s) : 28
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
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
Sample Input
0 1 0
6 10 2
Sample Output
0
60
Source
题解:
#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int mod=;
long long a,b;
int n;
long long pow(long long a,long long b)
{
long long res=;
while(b)
{
if (b&) res=(res*a)%mod;
a=(a*a)%mod;
b>>=;
}
return res;
}
long long mul(int n)
{
long long t[][]={,,,};//相当于pow中的a
long long ans[][]={,,,};//存最后的结果
long long tmp[][]; // 临时的
while(n)
{
if (n&)
{
for(int i=;i<;i++)
for(int j=;j<;j++)
tmp[i][j]=ans[i][j],ans[i][j]=;
for(int i=;i<;i++)
for(int j=;j<;j++)
for(int k=;k<;k++)
ans[i][j]=(ans[i][j]+tmp[i][k]*t[k][j])%(mod-);
}
for(int i=;i<;i++)
for(int j=;j<;j++)
{ tmp[i][j]=t[i][j]; t[i][j]=;}
for(int i=;i<;i++)
for(int j=;j<;j++)
for(int k=;k<;k++)
t[i][j]=(t[i][j]+tmp[i][k]*tmp[k][j])%(mod-);
n>>=;
}
return (pow(a,ans[][])*pow(b,ans[][]))%mod;
}
int main()
{
while(~scanf("%lld%lld%d",&a,&b,&n))
{
if (n==) printf("%lld\n",a%mod);
else if (n==) printf("%lld\n",b%mod);
else printf("%lld\n",mul(n));
} return ;
}
HDU 4549 M斐波那契数列(矩阵快速幂+费马小定理)的更多相关文章
- hdu 4549 M斐波拉契 (矩阵快速幂 + 费马小定理)
Problem DescriptionM斐波那契数列F[n]是一种整数数列,它的定义如下: F[0] = aF[1] = bF[n] = F[n-1] * F[n-2] ( n > 1 ) 现在 ...
- hdu 4549 M斐波那契数列 矩阵快速幂+欧拉定理
M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Problem ...
- hdu 4549 M斐波那契数列(快速幂 矩阵快速幂 费马小定理)
题目链接http://acm.hdu.edu.cn/showproblem.php?pid=4549: 题目是中文的很容易理解吧.可一开始我把题目看错了,这毛病哈哈. 一开始我看错题时,就用了一个快速 ...
- M斐波那契数列(矩阵快速幂+费马小定理)
M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Sub ...
- HDU4549 M斐波那契数列 矩阵快速幂+欧拉函数+欧拉定理
M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Sub ...
- 51nod1242 斐波那契数列 矩阵快速幂
1242 斐波那契数列的第N项 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 #include<stdio.h> #define mod 100000000 ...
- POJ3070 斐波那契数列 矩阵快速幂
题目链接:http://poj.org/problem?id=3070 题意就是让你求斐波那契数列,不过n非常大,只能用logn的矩阵快速幂来做了 刚学完矩阵快速幂刷的水题,POJ不能用万能头文件是真 ...
- hdu4549 M斐波那契数列 矩阵快速幂+快速幂
M斐波那契数列F[n]是一种整数数列,它的定义如下: F[0] = aF[1] = bF[n] = F[n-1] * F[n-2] ( n > 1 ) 现在给出a, b, n,你能求出F[n]的 ...
- 洛谷P1962 斐波那契数列(矩阵快速幂)
题目背景 大家都知道,斐波那契数列是满足如下性质的一个数列: • f(1) = 1 • f(2) = 1 • f(n) = f(n-1) + f(n-2) (n ≥ 2 且 n 为整数) 题目描述 请 ...
随机推荐
- windows 常用cmd命令
为了减少使用鼠标的频次,熟记一些常用应用的快捷键与系统本身常用的命令是必须的,以下记录一些常用的windows系统命令. 查看网络端口占用情况 :netstat -ano | findstr 8080 ...
- php面向对象类中常用的魔术方法
php面向对象类中常用的魔术方法 1.__construct():构造方法,当类被实例化new $class时被自动调用的方法,在类的继承中可以继承与覆盖该方法,例: //__construct( ...
- WebSocket使用SuperWebSocket结合WindowsService实现实时消息
SuperWebSocket在WebService中的应用 最开始使用是寄托在IIS中,发布之后测试时半个小时就会断开,所以改为WindowsService 1. 新建Windows服务项目[Test ...
- wamp5.5.12安装re dis扩展
转载地址:http://hanqunfeng.iteye.com/blog/1984387 phpredis是个人觉得最好的一个php-redis客户端,因为其提供的function与redis的命令 ...
- SQL条件!=null查不出数据
今天有一条sql需要某两个字段不能为空,当然是不能为null也不能为空字符串啦. 然后就开始写 WHERE ( order_amount != null and order_amount != '' ...
- MVC的局部视图传参的小技巧--见人才网头部导航
当我们设计一个局部视图时,当出现有类似导航的功能(如:选择左边的某个按钮跳到某个页,且顶部导航也作相印改变),如果我们选择把导航作为局部视图来处理,调用就可以做如下处理: @Html.RenderAc ...
- MVC中关于 使用后台代码 检查 用户名是否已经被清册
在 注册页面 NewUser 的 Controller中写以下代码 public ActionResult GetUserIndataByUserName() { string UserName= ...
- [pixhawk笔记]7-MAVLink添加自定义消息
前一篇学习了uORB,用于px4中各个模块的进程间通信,下来学习MAVLink,用于飞控和地面站之间的通信.教程中主要给出了使用MAVLink的发送和接收消息的方法.完整的MAVLink消息列表见该网 ...
- Centos7.5静默安装Oracle18c
环境: CentOS7.5.Oracle18c(LINUX.X64_180000_db_home.zip) 1. 安装必要的依赖包 [root@bogon ~]# yum install bc bin ...
- LeetCode——Single Element in a Sorted Array
Question Given a sorted array consisting of only integers where every element appears twice except f ...