poj3070
来源:互联网 发布:底火帽在淘宝叫什么 编辑:程序博客网 时间:2024/05/20 14:25
题目大意:
如上述公式所示,求Fn的最后4位数字
解题思路:
快速幂求得斐波那契数列的第n项,由于数据较大,还要记得要不断的对10000取模
代码如下:
#include<iostream>#include<cstdlib>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespace std;struct prog{ int a[2][2]; void init() { a[0][0]=a[1][0]=a[0][1]=1; a[1][1]=0; } };prog matrixmul(prog a,prog b){ int i,j,k; prog c; for(i=0;i<2;i++) { for(j=0;j<2;j++) { c.a[i][j]=0; for(k=0;k<2;k++) { c.a[i][j]+=(a.a[i][k]*b.a[k][j]); } c.a[i][j]%=10000; } } return c;}prog mul(prog s,int k){ prog ans; ans.init(); while(k>=1) { if(k&1) { ans=matrixmul(ans,s); } k=k>>1; s=matrixmul(s,s); } return ans;}int main(){ int n; while(cin>>n,~n) { if(!n) { cout<<0<<endl; continue; } prog s; s.init(); s=mul(s,n-1); cout<<s.a[0][1]%10000<<endl; } return 0; }
0 0
- poj3070
- poj3070
- poj3070
- poj3070
- poj3070
- poj3070
- poj3070
- poj3070
- poj3070
- poj3070
- poj3070
- POJ3070
- poj3070
- poj3070
- poj3070
- poj3070
- POJ3070
- POJ3070
- Linux 终端 Bash 常用快捷键介绍及经验
- Ubuntu系统Matlab桌面快捷方式
- 2017年2月10日
- 特殊字符总结
- 关于ListView中控件点击事件与Item点击事件冲突的问题
- poj3070
- 初始SVN与GIT代码上传
- [杂项]关于github安装包打开失败的原因之一
- asp.net core mvc权限控制:权限控制介绍
- PAT B1043. 输出PATest(20)
- string 与char* char[]之间的转换
- Maven 修改本地存储库位置
- 0211
- 串口