集训第六周 矩阵快速幂 K题
2015-08-22 15:32
232 查看
Description
In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. For example, the first ten terms of the Fibonacci sequence are:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …
An alternative formula for the Fibonacci sequence is
View Code
In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. For example, the first ten terms of the Fibonacci sequence are:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …
An alternative formula for the Fibonacci sequence is
#include"iostream" #include"cstdio" using namespace std; typedef struct { int m[2][2]; }node; node work(node a,node b) { node c; c.m[0][0]=(a.m[0][0]*b.m[0][0]+a.m[0][1]*b.m[1][0])%10000; c.m[0][1]=(a.m[0][0]*b.m[0][1]+a.m[0][1]*b.m[1][1])%10000; c.m[1][0]=(a.m[1][0]*b.m[0][0]+a.m[1][1]*b.m[1][0])%10000; c.m[1][1]=(a.m[1][0]*b.m[0][1]+a.m[1][1]*b.m[1][1])%10000; return c; } void caculate(int c) { node ans,base; base.m[0][0]=base.m[1][0]=base.m[0][1]=1; base.m[1][1]=0; ans.m[0][0]=ans.m[1][1]=1; ans.m[0][1]=ans.m[1][0]=0; while(c) { if(c&1) ans=work(ans,base); base=work(base,base); c>>=1; } cout<<ans.m[1][0]<<endl; } int main() { int n; while(cin>>n&&n>=0) { caculate(n); } }
View Code
相关文章推荐
- python 网页抓取并保存图片
- 快学Scala习题解答—第十章 特质
- android利用剪切板传递数据
- 各种排序算法的总结和实现。
- vmware workstation11虚拟机破解版(附安装教程) 32/64位
- 经典排序算法 - 冒泡排序Bubble sort
- 我对“编程平台”的理解
- pjsip编译步骤,centos环境支持视频通话
- 栈的压入,弹出序列
- 剑指off-约瑟夫环
- js中 a : function(){}这是什么格式? 代表什么含义?怎样学习这样的格式?
- HDU 1484 Basic wall maze (dfs + 记忆)
- Robot Framework通过Python SMTP进行email收发测试。
- selenium C#下的zencart自动化测试(WFloginUrlPayment)环境4.0
- WAV文件格式
- WAV文件格式
- WAV文件格式
- WAV文件格式
- WAV文件格式
- WAV文件格式