3070 Fibonacci
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 21048 | Accepted: 14416 |
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
Hint
As a reminder, matrix multiplication is associative, and the product of two 2 × 2 matrices is given by
.
Also, note that raising any 2 × 2 matrix to the 0th power gives the identity matrix:
.
裸的快速矩阵幂 >>=1就是/=2 前几天做题目头脑没转过弯来 好气
讲解参考 https://www.cnblogs.com/cmmdc/p/6936196.html
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
#include <set>
#include <sstream>
#include <algorithm>
int N;
using namespace std;
const int si = , mod = ; struct mat {
int m[si][si];
}; mat mul(mat A, mat B) {
mat tp;
for (int i = ; i < si; i++) {
for (int j = ; j < si; j++) {
tp.m[i][j] = ;
}
}
for (int i = ; i < si; i++) {
for (int j = ; j < si; j++) {
for (int k = ; k < si; k++) {
tp.m[i][j] += A.m[i][k] * B.m[k][j];
tp.m[i][j] %= mod;
}
}
}
return tp;
}
mat pow (mat A, int e){
mat tp;
for (int i = ; i < si; i++) {
for (int j = ; j < si; j++) {
if (i == j) tp.m[i][j] = ;
else tp.m[i][j] = ;
}
}
while (e) {
if (e & ) {
tp = mul(tp, A);
}
A = mul(A, A);
e /= ;
}
return tp;
}
int main() {
while () {
scanf("%d", &N);
if (N < ) break;
if (N == ) {
printf("%d\n", % mod);
continue;
}
mat MA;
MA.m[][] = ; MA.m[][] = ;
MA.m[][] = ; MA.m[][] = ;
MA = pow(MA, N);
printf("%d\n", MA.m[][] % mod);
}
return ;
}
3070 Fibonacci的更多相关文章
- 矩阵快速幂 POJ 3070 Fibonacci
题目传送门 /* 矩阵快速幂:求第n项的Fibonacci数,转置矩阵都给出,套个模板就可以了.效率很高啊 */ #include <cstdio> #include <algori ...
- 【POJ】3070 Fibonacci(矩阵乘法)
http://poj.org/problem?id=3070 根据本题算矩阵,用快速幂即可. 裸题 #include <cstdio> #include <cstring> # ...
- POJ 3070 Fibonacci
Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. F ...
- 矩阵经典题目六:poj 3070 Fibonacci
http://poj.org/problem?id=3070 按已构造好的矩阵,那么该矩阵的n次方的右上角的数便是f[n]. #include <stdio.h> #include < ...
- POJ 3070 Fibonacci(矩阵高速功率)
职务地址:POJ 3070 用这个题学会了用矩阵高速幂来高速求斐波那契数. 依据上个公式可知,第1行第2列和第2行第1列的数都是第n个斐波那契数.所以构造矩阵.求高速幂就可以. 代码例如以下: #in ...
- poj 3070 Fibonacci (矩阵快速幂乘/模板)
题意:给你一个n,输出Fibonacci (n)%10000的结果 思路:裸矩阵快速幂乘,直接套模板 代码: #include <cstdio> #include <cstring& ...
- poj 3070 Fibonacci 矩阵快速幂
Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. F ...
- POJ 3070 Fibonacci 【矩阵快速幂】
<题目链接> Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 ...
- poj 3070 Fibonacci 矩阵相乘
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7715 Accepted: 5474 Descrip ...
随机推荐
- rangeOfString 和 containsString 兼容iOS7处理
//查找字符串是否包含"心" NSString *str = @"每天都有好心情"; if ([str containsString:@"心" ...
- Tomcat基本
Tomcat web 应用服务器基础 jdk+tomcat安装 1.运行Tomcat为什么要装jdk? http://blog.sina.com.cn/s/blog_753bc97d0102w5rd. ...
- HBase最佳实践-写性能优化策略
本篇文章来说道说道如何诊断HBase写数据的异常问题以及优化写性能.和读相比,HBase写数据流程倒是显得很简单:数据先顺序写入HLog,再写入对应的缓存Memstore,当Memstore中数据大小 ...
- Git的初次使用
一.配置本地Git库 1.下载安装好Git,并配置自己的信息. git config --global user.name"yourname"配置你的名称 git config - ...
- 转载:如何搭建turn server 在centos7上。
https://www.cnblogs.com/idignew/p/7440048.html
- java中Map集合的常用方法
Map集合和Collection集合的区别 Map集合是有Key和Value的,Collection集合是只有Value. Collection集合底层也是有Key和Value,只是隐藏起来. V p ...
- WebApi请求原理
一.路由: 1.首先执行Application_start 2 .注册路由 WebApiConfig,把路由规则写入一个容器 运行 请求会去容器匹配-找到控制器容器 一般不指定Action, rest ...
- pinpoint与zipkin的比较
经过本周部署和测试pinpoint监控平台的工作,我对这套开源系统有了更进一步的认识. 初次见到pinpoint这套系统时,我被它各方面优秀的特征所折服:无需对项目代码进行任何改动就可以部署探针.追踪 ...
- 【转】Rancher 2.0 里程碑版本:支持添加自定义节点!
原文链接: http://mp.weixin.qq.com/s?__biz=MzIyMTUwMDMyOQ==&mid=2247487533&idx=1&sn=c70258577 ...
- Redis中Pipeline的使用
通过Java访问Redis,我们一般使用Jedis,示例代码如下: Jedis jedis = new Jedis("172.23.88.107", 6379); jedis.se ...