水题: 费马小定理+快速幂+矩阵快速幂

(第一次用到费马小定理)

#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的更多相关文章

  1. 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]. 思路:手算一下可以发现 ...

  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... ...

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

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

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

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

  5. 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)} ...

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

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

  7. hdu 4549 矩阵快速幂

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

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

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

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

    M斐波那契数列 Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other) Total Submi ...

随机推荐

  1. Spark整合HBase,Hive

    背景: 场景需求1:使用spark直接读取HBASE表 场景需求2:使用spark直接读取HIVE表 场景需求3:使用spark读取HBASE在Hive的外表 摘要: 1.背景 2.提交脚本 内容 场 ...

  2. 查看swap占用情况

    查看swap被占用的情况 #!/bin/bash # Get current swap usage for all running processes # Erik Ljungstrom 27/05/ ...

  3. Http状态码解释

    参考:urllib与urllib2的学习总结(python2.7.X) # Table mapping response codes to messages; entries have the # f ...

  4. vue中element-ui树形控件自定义节点,注意一下

    在项目中用到菜单项编辑删除,在 element-ui自定义节点内容时, 有说明:使用render-content指定渲染函数, 得环境支持 JSX 语法,只要正确地配置了相关依赖,就可以正常运行. 一 ...

  5. 七、文件IO——I/O处理方式和文件锁

    7.1 I/O 处理方式 7.1.1 I/O处理的五种模型 阻塞I/O模型 若所调用的 I/O 函数没有完成相关的功能就会使进程挂起,直到相关数据到达才会返回.如 终端.网络设备的访问. 非阻塞模型 ...

  6. Cisco Common Service Platform Collector - Hardcoded Credentials(CVE-2019-1723)

    Cisco Common Service Platform Collector - Hardcoded Credentials 思科公共服务平台收集器-硬编码凭证(CVE-2019-1723) htt ...

  7. 第28月第21天 记事本Unicode 游戏编程中的人工智能技术

    1. Windows平台,有一个最简单的转化方法,就是使用内置的记事本小程序notepad.exe.打开文件后,点击文件菜单中的另存为命令,会跳出一个对话框,在最底部有一个编码的下拉条. 里面有四个选 ...

  8. Hbase使用MapReduce编程导出数据到HDFS

    废话少说,直接上代码! package cn.com.oozie.demo;  import java.io.IOException;  import org.apache.hadoop.conf.C ...

  9. E - Reachability from the Capital

    E - Reachability from the Capital  CodeForces - 999E 题目链接:https://vjudge.net/contest/236513#problem/ ...

  10. mysql与linux ~ 磁盘分析与调优

    一 简介 谈谈磁盘IO的问题二 目的:如何进行IO性能问题的排查 二  linux角度   一 机械硬盘基本定义       寻道时间,表示磁头在不同磁道之间移动的时间(最耗时).       旋转延 ...