HDU 4549

水题: 费马小定理+快速幂+矩阵快速幂
(第一次用到费马小定理)
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL MOD = 1000000006;
const LL MOD1 = 1000000007;
struct Matrix
{
LL NUM[2][2];
Matrix operator + (const Matrix a) const
{
Matrix c;
for(int i = 0; i < 2; ++i)
{
for(int j = 0; j < 2; ++j)
{
c.NUM[i][j] = NUM[i][j] + a.NUM[i][j];
}
}
return c;
}
Matrix operator * (const Matrix a) const
{
Matrix c;
for(int i = 0; i < 2; ++i)
{
for(int j = 0; j < 2; ++j)
{
c.NUM[i][j] = 0;
for(int k = 0; k < 2; ++k)
c.NUM[i][j] = (c.NUM[i][j] + NUM[i][k] * a.NUM[k][j] % MOD) % MOD;
}
}
return c;
}
}; Matrix ppow(Matrix a, LL n)
{
Matrix ret;
for(int i =0 ; i< 2; ++i)
{
for(int j = 0; j < 2; ++j)
ret.NUM[i][j] = i==j ? 1 : 0;
}
while(n)
{
if(n & 1) ret = ret * a;
a = a * a;
n >>= 1;
}
return ret;
} LL Pow(LL a, LL n)
{
LL ret = 1;
while(n)
{
if(n & 1) ret =ret * a % MOD1;
a = a * a % MOD1;
n >>= 1;
}
return ret;
} int main()
{
LL a, b, n;
Matrix E;
E.NUM[0][0] = 1; E.NUM[0][1] = 1;
E.NUM[1][0] = 1; E.NUM[1][1] = 0;
while(cin >> a >> b >> n)
{
if(n == 0) cout << a << endl;
else if(n == 1) cout << b << endl;
else
{
n -= 1;
Matrix tmp = ppow(E,n);
LL na = tmp.NUM[0][1] , nb = tmp.NUM[0][0];
LL ans = (Pow(a,na) * Pow(b,nb))%MOD1;
cout << ans << endl;
}
}
return 0;
}
HDU 4549的更多相关文章
- HDU 4549 M斐波那契数列(矩阵幂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4549 题意:F[0]=a,F[1]=b,F[n]=F[n-1]*F[n-2]. 思路:手算一下可以发现 ...
- hdu 4549 M斐波那契数列(矩阵高速幂,高速幂降幂)
http://acm.hdu.edu.cn/showproblem.php?pid=4549 f[0] = a^1*b^0%p,f[1] = a^0*b^1%p,f[2] = a^1*b^1%p... ...
- hdu 4549 M斐波那契数列(快速幂 矩阵快速幂 费马小定理)
题目链接http://acm.hdu.edu.cn/showproblem.php?pid=4549: 题目是中文的很容易理解吧.可一开始我把题目看错了,这毛病哈哈. 一开始我看错题时,就用了一个快速 ...
- [HDU 4549] M斐波那契数列
M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Sub ...
- HDU 4549 M斐波那契数列(矩阵快速幂)
题目链接: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)} ...
- hdu 4549 M斐波拉契 (矩阵快速幂 + 费马小定理)
Problem DescriptionM斐波那契数列F[n]是一种整数数列,它的定义如下: F[0] = aF[1] = bF[n] = F[n-1] * F[n-2] ( n > 1 ) 现在 ...
- hdu 4549 矩阵快速幂
题意: M斐波那契数列F[n]是一种整数数列,它的定义如下: F[0] = aF[1] = bF[n] = F[n-1] * F[n-2] ( n > 1 ) 现在给出a, b, n,你能求出F ...
- hdu 4549 M斐波那契数列 矩阵快速幂+欧拉定理
M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Problem ...
- HDU 4549 M斐波那契数列(矩阵快速幂+费马小定理)
M斐波那契数列 Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other) Total Submi ...
随机推荐
- Openresty 学习笔记(一)opm 工具的使用
1.自1.11.2.2开始,OpenResty版本已经包含并默认安装opm.所以通常你不需要自己安装opm. 2.我们在这里只需要做一个软连接就可以了 cd /usr/local/openresty/ ...
- js监听键盘事件
用JS监听键盘按下事件(keydown event) 1.监听全局键盘按下事件,例如监听全局回车事件 1 $(document).keydown(function(event){ 2 if(eve ...
- SQL结构化查询语句
SQL结构化查询语句 SQL定义了查询所有关系型数据库的规则. 1.通用语法 SQL语句可以单行或者多行书写,以分号结尾 可以使用空格和缩进增强可读性 不区分大小写,但是关键字建议大写 3种注释 注释 ...
- Extjs.net Button点击下载jpg图片
<ext:Button ID=" AutoPostBack="false"> <DirectEvents> <Click OnEvent=& ...
- JavaSE回顾及巩固的自学之路(三)——————所有语言的都存在的基本运算
在上一篇的博客中,我回顾到Java中的关键字,标识符等知识点,而今天这篇博文将回顾Java的,哦,不,不止Java,据本人了解,几乎在所有的语言中的基础阶段,都会存在这些运算,只是语法不一样而已. 今 ...
- web-hacking
https://wizardforcel.gitbooks.io/web-hacking-101/content/1.html
- asp.net上传图片,上传图片
想必很多人工作中经常需要实现上传图片的功能. 先引用此插件 http://files.cnblogs.com/files/hmYao/jquery-form.js. 前台代码 <form dat ...
- Debian Security Advisory(Debian安全报告) DSA-4412-1 drupal7 security update
Debian Security Advisory(Debian安全报告) DSA-4412-1 drupal7 security update Package:drupal7 CVE ID:暂无 Dr ...
- [C++]数值与字符串转换问题
#include <stdio.h>//sprintf #include <stdlib.h>//atof,atol,strtod,strtol using namespace ...
- ansible学习笔记二
Ad-Hoc命令: 所谓Ad-Hoc,简而言之是"临时命令",英文中作为形容词有"特别的,临时"的含义.Ad-Hoc只是官方对Ansible命令的一种称谓. 从 ...