这道题我写了两种写法

一种利用逆元 a/b%mod=a*c%mod; (c是b的逆元)易得2的逆元就是5~~~04;

一种是矩阵快速幂 利用递推式得出结论

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int mod=1000000007;
int read(){int ans=0,f=1,c=getchar();while(c<'0'||c>'9'){if(c=='-') f=-1; c=getchar();}while(c>='0'&&c<='9'){ans=ans*10+(c-'0'); c=getchar();}return ans*f;
}
int n;
int qmod(long long a,int b){long long ans=1; a%=mod;while(b){if(b&1) ans=ans*a%mod;b>>=1;a=a*a%mod;}return ans;
}
int main()
{n=read()+1;printf("%lld\n",(qmod(3,n)-1)*(long long)500000004%mod);return 0;
}

View Code

#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long
using namespace std;
const int mod=1000000007;
int read(){int ans=0,f=1,c=getchar();while(c<'0'||c>'9'){if(c=='-') f=-1; c=getchar();}while(c>='0'&&c<='9'){ans=ans*10+(c-'0'); c=getchar();}return ans*f;
}
typedef LL mat[2][2];
int n;
void quickmod(mat a,mat b){mat c={0};for(int i=0;i<2;i++)for(int k=0;k<2;k++)for(int j=0;j<2;j++)c[i][j]=(c[i][j]+a[i][k]*b[k][j])%mod;for(int i=0;i<2;i++)for(int j=0;j<2;j++)a[i][j]=c[i][j];
}
void fastpow(int n){mat a={1,0,0,1},b={3,1,0,1};while(n){if(n&1) quickmod(a,b);n>>=1; quickmod(b,b);}printf("%d\n",(a[0][0]+a[0][1])%mod);
}
int main()
{n=read(); fastpow(n);return 0;
}

View Code

转载于:https://www.cnblogs.com/lyzuikeai/p/6942043.html

51 Nod 1013 3的幂的和 矩阵链乘法||逆元+快速幂相关推荐

  1. 数学--数论--HDU 4675 GCD of Sequence(莫比乌斯反演+卢卡斯定理求组合数+乘法逆元+快速幂取模)

    先放知识点: 莫比乌斯反演 卢卡斯定理求组合数 乘法逆元 快速幂取模 GCD of Sequence Alice is playing a game with Bob. Alice shows N i ...

  2. 矩阵的乘法和快速幂的一些理解(适用初学者)

    矩阵是线性代数的知识...后悔没好好学了... 第一部分:矩阵的基础知识 1.结合性 (AB)C=A(BC). 2.对加法的分配性 (A+B)C=AC+BC,C(A+B)=CA+CB . 3.对数乘的 ...

  3. (组合数求模=乘法逆元+快速幂) Problem Makes Problem

    题目: As I am fond of making easier problems, I discovered a problem. Actually, the problem is 'how ca ...

  4. 51 NOD 1013 3的幂的和

    做法:快速幂+求逆元取模 因为ans=((3^(n+2))/2)%P 而ans%P/2!=ans/2%P 所以由费马小定理当gcd(a,p)==1&&P为质数时,a^(p-1)≡1(m ...

  5. 2014 Super Training #7 F Power of Fibonacci --数学+逆元+快速幂

    原题:ZOJ 3774  http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3774 --------------------- ...

  6. 算法提高课-数学知识-矩阵乘法-AcWing 1303. 斐波那契前 n 项和:矩阵乘法,快速幂,线性代数

    题目分析 来源:acwing 分析: 先利用矩阵运算的性质将通项公式变成幂次形式,然后用快速幂的方法求解第 n项. 斐波那契数列的递推公式:f1=f2=1,fn=fn−2+fn−1(n≥3)f_1 = ...

  7. 矩阵乘法和快速幂的一些优化和剪枝

    矩阵相乘 一般会用O(n^3)的方法...配合剪枝[添条件,设门槛...] //O(n^3)算法 #include <iostream> #include <cstdio> # ...

  8. mysql 幂运算_算法—史上最好快速幂算法讲解

    前言 快速幂是什么?顾名思义,快速幂就是快速算底数的n次幂. 有多快?其时间复杂度为 O(log₂n), 与朴素的O(n)相比效率有了极大的提高. 用的多么?快速幂属于数论的范畴,本是ACM经典算法, ...

  9. 第十四届华中科技大学程序设计竞赛 B Beautiful Trees Cutting【组合数学/费马小定理求逆元/快速幂】...

    链接:https://www.nowcoder.com/acm/contest/106/B 来源:牛客网题目描述 It's universally acknowledged that there're ...

  10. 【BZOJ-4522】密钥破解 数论 + 模拟 ( Pollard_Rho分解 + Exgcd求逆元 + 快速幂 + 快速乘)...

    4522: [Cqoi2016]密钥破解 Time Limit: 10 Sec  Memory Limit: 512 MB Submit: 290  Solved: 148 [Submit][Stat ...

最新文章

  1. 多光谱和高光谱图像有什么区别?
  2. PHP内核介绍及扩展开发指南—高级主题
  3. Kotlin的解析(中)
  4. 获取程序代码块资源消耗
  5. oracle计费系统相关问题,用radius和oracle做认证计费系统
  6. java并发之SynchronousQueue实现原理
  7. RTP协议全解(H264码流和PS流)
  8. WinSCP+PuTTY搭配使用 ,解决Windows连接Linux系统文件传输和终端登陆
  9. pytorch查看应用指数衰减后的学习率
  10. rabbitmq+topic+java_译:5.RabbitMQ Java Client 之 Topics (主题)
  11. 【算法分析与设计】快速幂算法与快速幂取模算法
  12. 大学计算机基础教程模拟选择题,【浙江大学】大学计算机基础教程——习题与参考答案...
  13. linuxpython源文件_Python3 源码安装(Linux 版)
  14. [USACO14OPEN]Odometer【数位DP】
  15. html文件怎么用华为手机打开,如何调整华为手机中的文件默认打开方式
  16. ApacheCN 活动汇总 2019.6.28
  17. u检验中的查u界值表_《医学统计学》
  18. 计算机高级筛选在哪找,excel表格数据高级筛选在哪里-EXCEL高级筛选
  19. 任务队列:celery快速入门及django中celery的用法
  20. 【python】pandas库pd.to_excel操作写入excel文件参数整理与实例

热门文章

  1. SEO专题之三:SEO与网站开发
  2. ctrl+d与ctrl+c
  3. Android--建立能与访问者进行相互通信的本地服务
  4. OPENSTACK在RHEL7安装;admin创建虚拟机模板供demo使用
  5. MyEclipse 保护色设置
  6. 上次遗留下来的XMLUtil的问题
  7. spf13-vim安装与使用
  8. [c++]在C++中定义常量的两种方法的比较
  9. 这 8 个 Python 技巧让你的数据分析提升数倍!
  10. [PHP开发必备] -- 小巧强悍的MYSQL-Front中文版使用教程,附最新版下载地址