矩阵模板 51nod 算法马拉松分解问题
来源:互联网 发布:交换机端口速率与带宽 编辑:程序博客网 时间:2024/06/10 02:38
题目:点击打开链接
一个矩阵乘法问题,(1+sqrt(2))的n次方!
#include<bits/stdc++.h> using namespace std;#define maxn (1000000007)struct Mat{ long long mat[2][2];};Mat init(Mat x){ x.mat[0][0]=1; x.mat[0][1]=2; x.mat[1][0]=1; x.mat[1][1]=1; return x;}Mat operator+(Mat a,Mat b){ Mat c; int i,j; for (i=0;i<2;i++) { for (j=0;j<2;j++) c.mat[i][j] = a.mat[i][j]+b.mat[i][j]; } return c;}Mat operator*(Mat a,Mat b){ int i,j,k; Mat c; for (i=0;i<2;i++) { for (j=0;j<2;j++) { c.mat[i][j] = 0; for (k=0;k<2;k++) { c.mat[i][j]+=(a.mat[i][k]*b.mat[k][j])%maxn; c.mat[i][j]=(c.mat[i][j]+maxn)%maxn; } //c.mat[i][j]%=MOD; } } return c;}Mat operator^(Mat a,long long x){ Mat p=a; Mat q; q=init(q); while (x>=1) { if(x%2==1) p = p*q; x/=2; q = q*q; } return p;}int main(){ long long n; cin>>n; Mat ans; ans.mat[0][0]=1; ans.mat[0][1]=0; ans.mat[1][0]=0; ans.mat[1][1]=1; if(n==0){ printf("1\n"); } else if(n%2==1){ ans=ans^n; printf("%I64d\n",(ans.mat[1][0]*ans.mat[0][1])%maxn); } else{ ans=ans^n; printf("%I64d\n",(ans.mat[0][0]*ans.mat[1][1])%maxn); } return 0;}
0 0
- 矩阵模板 51nod 算法马拉松分解问题
- 51NOD 1537 分解(矩阵快速幂)——算法马拉松17(告别奥运)
- 51nod 算法马拉松3 A:序列分解
- 51nod 算法马拉松12
- 51nod算法马拉松20
- 51Nod 算法马拉松23
- 51Nod 算法马拉松24
- 51nod算法马拉松25
- 51NOD算法马拉松 最大值问题 离线预处理+set lower_bound
- 51nod 1394 差和问题(算法马拉松8)
- 【51Nod算法马拉松18 A】染色问题
- 51Nod-算法马拉松17(告别奥运)-B-分解
- 51nod 算法马拉松 集合计数
- 51nod 算法马拉松11 D 计算
- 51nod 算法马拉松12 逛街(treap)
- [51nod]算法马拉松18 总结
- 51nod算法马拉松18总结
- 51nod 算法马拉松19 A P1674
- Linux环境下,apache设置禁止恶意域名绑定和直接ip访问方法
- leetcode-32-Longest Valid Parentheses
- bbook 二手程序书籍交易平台
- 每天一条linux命令--rm命令
- Go-godoc帮助系统
- 矩阵模板 51nod 算法马拉松分解问题
- How to stop kernel messages from flooding my console?
- json文件生成与解析
- UVA 548 Tree
- 解决阿里云安骑士漏洞警告:wordpress WPImageEditorImagick 指令注入漏洞
- Go-数据类型
- EasyIPCamera Android版本RTSP服务器RTSPServer快速搭建
- Execute a command on user logon
- Unix网络编程之-------unp.h头文件详细配置-1