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 ...
随机推荐
- 剑指offer(36)两个链表中的第一个公共节点
题目描述 输入两个链表,找出它们的第一个公共结点. 题目分析 我发现关于链表的题都涉及双指针,大家做的时候记得用双指针. 题目理解了就很好做了,比较简单,先在长的链表上跑,知道长的和短的一样长,再一起 ...
- 爆料:2019手游折扣app是真福利还是骗人哪个靠谱?
直接上干货.也许你在找寻,安全的手游折扣App,稳定的手游折扣App,不断续充的折扣App,续充不涨价的折扣App,网上的内容太多,难以分辨.那么看这个可以直接给你答案 1.历史(2004年成立,15 ...
- 从GitHub远程仓库中删除文件夹或文件
在上传项目到github时,忘记忽略了某个文件夹target,就直接push上去了, 最后意识到了此问题,决定删除掉远程仓库中的target文件夹 删除前: 删除后: 在github上只能删除仓库,却 ...
- MySQL安装指南(转)
MySQL安装指南 安装MySQL sudo apt-get install mysql-server 这个应该很简单了,而且我觉得大家在安装方面也没什么太大问题,所以也就不多说了,下面我们来讲讲 ...
- ubuntun 18.04 安装google浏览器
---恢复内容开始--- 一:下载谷歌浏览器镜像源 sudo wget http://www.linuxidc.com/files/repo/google-chrome.list -P /etc/ap ...
- Luffy之购物车页面搭建
前面已经将一些课程加入购物车中,并保存到了后端的redis数据库中,此时做购物车页面时,我们需要将在前端向后端发送请求,用来获取数据数据 购物车页面 1.首先后端要将数据构建好,后端视图函数如下代码: ...
- SSH框架学习摸索记
Unable to load configuration. - bean - jar:file:/E:/tomcat-7.0.11/webapps/struts/WEB-INF/lib/struts ...
- Loadrunner加密算法脚本与token作为get请求url上的参数处理
1.当字符串被封装好加密时(下例将算法封装在md5中),使用Loadrunner编写脚本,需要进行如下操作: 1)将md5.h文件添加到Extra Files 下,如图(Loadrunne ...
- 通过cookie记录,设置页面访问的跳转页
通过cookie记录,设置页面访问的跳转页 转载自:http://blog.csdn.net/yixiao_naihe/article/details/26679515. 目的: 1.访问fm.htm ...
- 1.3 第一个Go程序
1.3.1 Hello Go // hello.go package main import ( "fmt" ) func main() { fmt.Println("H ...