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 ...
随机推荐
- 如何用java语言实现C#中的ref关键字(按引用传递参数)的效果
https://www.cnblogs.com/nnngu/p/8300164.html
- 【D3D】Directx12运行报错&win10无法添加【图形工具】
“我欢欣雀跃地打开<3D programming with Directx12>,准备接受D3D的洗礼,然后就卡在了 chapter 0 .”——Liez 100%纯小白的chapter ...
- 【python小练】0013
第 0013 题: 用 Python 写一个爬图片的程序,爬 这个链接里的日本妹子图片 :-) 科科...妹子就算了,大晚上的爬点吃的吧.食物图集:抿一口,舔一舔,扭一扭~·SCD 写个简单的爬图爬虫 ...
- sql server 横向转丛向及FOR XML PATH使用
1.开始数据结构如下: 2.转为如下图: 使用如下SQL语句: ---横向转丛向 select name '姓名', max(case when course='语文' then score end) ...
- oracle 问题查找 error ora-
Error ORA-03113: 通信通道的文件结尾进程 ID: 2232会话 ID: 1250 序列号: 这是oracle 报的错误, 可能这个03113这个编码的错误有很多. 但是要找到是什么原因 ...
- Docker 查看容器 IP 地址
查看Docker的底层信息. docker inspect 会返回一个 JSON 文件记录着 Docker 容器的配置和状态信息 docker inspect NAMES # 查看容器所有状态信息: ...
- 第25月第15天 udacity cs253
1.cs253 https://classroom.udacity.com/courses/cs253 webapp2 Install WebOb, Paste and webapp2¶ We nee ...
- JDK1.8HashMap源码解读
package java.util; import sun.misc.SharedSecrets; import java.io.IOException; import java.io.Invalid ...
- 剑指Offer-从上往下打印二叉树
题目描述 从上往下打印出二叉树的每个节点,同层节点从左至右打印. 思路 使用两个队列一个存放节点,一个存放值.先将根节点加入到队列中,然后遍历队列中的元素,遍历过程中,访问该元素的左右节点,再将左右子 ...
- Python pipenv
pipenv都包含什么? pipenv 是 Pipfile 主要倡导者.requests 作者 Kenneth Reitz 写的一个命令行工具,主要包含了Pipfile.pip.click.reque ...