poj3070矩阵快速幂
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 7752 | Accepted: 5501 |
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
.
Given an integer n, your goal is to compute the last 4 digits of Fn.
Input
The input test file will contain multiple test cases. Each test case consists of a single line containing n (where 0 ≤ n ≤ 1,000,000,000). The end-of-file is denoted by a single line containing the number −1.
Output
For each test case, print the last four digits of Fn. If the last four digits of Fn are all zeros, print ‘0’; otherwise, omit any leading zeros (i.e., print Fn mod 10000).
Sample Input
0 9 999999999 1000000000 -1
Sample Output
0 34 626 6875
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <math.h> 5 #include <stdlib.h> 6 7 using namespace std; 8 int N; 9 struct matrix 10 { 11 int a[2][2]; 12 }origin,res; 13 matrix multiply(matrix x,matrix y) 14 { 15 matrix temp; 16 memset(temp.a,0,sizeof(temp.a)); 17 for(int i=0;i<N;i++) 18 { 19 for(int j=0;j<N;j++) 20 { 21 for(int k=0;k<N;k++) 22 { 23 temp.a[i][j]+=x.a[i][k]*y.a[k][j]%10000; 24 temp.a[i][j]%=10000; 25 } 26 } 27 } 28 return temp; 29 } 30 void calc(int n) 31 { 32 memset(res.a,0,sizeof(res.a)); 33 origin.a[0][0]=1;origin.a[0][1]=1; 34 origin.a[1][0]=1;origin.a[1][1]=0; 35 for(int i=0;i<N;i++) 36 res.a[i][i]=1; 37 while(n) 38 { 39 if(n&1) 40 res=multiply(res,origin); 41 n>>=1; 42 origin=multiply(origin,origin); 43 } 44 } 45 int main() 46 { 47 N=2; 48 int n; 49 while(cin>>n) 50 { 51 if(n==-1) 52 break; 53 if(n) 54 calc(n-1); 55 if(n) 56 cout<<res.a[0][0]<<endl; 57 else cout<<0<<endl; 58 } 59 }
View Code
转载于:https://www.cnblogs.com/ERKE/p/3272728.html
poj3070矩阵快速幂相关推荐
- POJ3070矩阵快速幂简单题
题意: 求斐波那契后四位,n <= 1,000,000,000. 思路: 简单矩阵快速幂,好久没刷矩阵题了,先找个最简单的练练手,总结下矩阵推理过程,其实比较简单,关键 ...
- POJ3070 矩阵快速幂模板
题目:http://poj.org/problem?id=3070 矩阵快速幂模板.mod写到乘法的定义部分就行了. 别忘了 I ( ) 和 i n i t ( ) 要传引用! #include< ...
- 快速幂、快速乘、矩阵快速幂
文章目录 一 快速幂 模板 模板例题 二 快速乘 模板 三 矩阵快速幂 例题 模板题 应用题1 应用题2 一 快速幂 目的:当我们在求f(x) = a ^ x % mod时,f(x)的结果会呈指数型增 ...
- POJ3070 Fibonacci【矩阵快速幂】
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20098 Accepted: 13850 Descripti ...
- poj3070 Fibonacci 矩阵快速幂
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18084 Accepted: 12572 Descr ...
- POJ3070 Fibonacci(矩阵快速幂)
用矩阵快速幂求fibonacci序列的第n项. /* *********************************************** Author :devil Created Tim ...
- 一文彻底搞懂快速幂(原理、实现、矩阵快速幂)
前言 大家好,我是bigsai,之前有个小老弟问到一个剑指offer一道相关快速幂的题,这里梳理一下讲一下快速幂! 快速幂是什么? 顾名思义,快速幂就是快速算底数的n次幂.你可能疑问,求n次幂算n次叠 ...
- 矩阵快速幂求斐波那契数列(初学整理)
参考文章: http://blog.csdn.net/u013795055/article/details/38599321 http://blog.csdn.net/g_congratulation ...
- 矩阵快速幂+构造方法
与快速幂一样,可以将递推式通过二进制的方式来进行优化,这个学了快速幂就是十分容易理解 大概的板子如下: struct mat///自己定义大小的矩阵 {ll m[11][11]; }; mat mul ...
最新文章
- java runnable 启动_Java开发笔记(九十七)利用Runnable启动线程
- sersync 同步
- 我的WINCE4.2历程(10)
- 在 Ubuntu Linux 上从源代码构建 Qt 6.2.2的简短教程
- 特征值和特征向量的实际意义
- 在Windows 64位下为PHP5.6.14安装redis扩展
- 2011年 11月底-12月初 51Aspx源码发布详情
- 黑科技轮胎:有能发电的,脑洞简直不要太大...
- 地雷会炸到自己吗_回顾自己曾经的往事 ——记双语学习有感
- 君威u0073故障码解决_格力多联机同时报三个故障“U2”“L0”“d9”怎么修?
- 初试django模型层
- java组合数打印出结果_Java打印一组数据中,抽取固定数目的数的所有可能组合...
- 拓端tecdat|R语言中的划分聚类模型
- 阿里巴巴Java开发手册评述
- Python:实现蓝牙通信
- 一款很好用的软还原卡
- -XX:+PrintGCTimeStamps -XX:+PrintGCDetails 日志分析
- matlab fft 采样点数,MATLAB中的FFT的采样频率和采样点怎样确定
- 解决 Maven ‘parent.relativePath‘ of POM
- 全球与中国量身定制生产线市场深度研究分析报告
热门文章
- java中setid(),Java Process.setId方法代碼示例
- unity windows打包ios_ios打包unity应用以及配置签名!
- 系统分析与仿真1-单输入单输出质量块+阻尼器系统
- java 国际化 properties_java处理国际化和读取properties文件代码片段
- “Could not import PIL.Image. The use of array_to_img requires PIL.”错误的解决办法
- aurora IP中选择了小端支持,但小端体现在了什么地方呢?
- bzoj 4237 稻草人
- Nginx服务优化——性能与安全
- 运用双腾讯云搭建《饥荒》多人联机服务器
- 安全框架 - Shiro与springMVC整合的注解以及JSP标签