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 ...
随机推荐
- SQL语句汇总——数据修改、数据查询
首先创建一张表如下,创建表的方法在上篇介绍过了,这里就不再赘述. 添加新数据: INSERT INTO <表名> (<列名列表>) VALUES (<值列表>) ...
- K8S学习笔记之二进制部署Kubernetes v1.13.4 高可用集群
0x00 概述 本次采用二进制文件方式部署,本文过程写成了更详细更多可选方案的ansible部署方案 https://github.com/zhangguanzhang/Kubernetes-ansi ...
- jQuery安装和语法
jQuery是一个JavaScript函数库,可实现HTML元素选取及操作.CSS 操作.HTML事件函数.JavaScript特效和动画.HTML DOM遍历和修改.AJAX等功能. 在html中引 ...
- Html from 标签
Html from 标签 <html> <body> <!-- form 提交表单设置 --> <form> <input type=" ...
- Weighted Quick Union
Weighted Quick Union即: 在Quick Union的基础上对结点加权(weighted),在parent[i]基础上增加一个size[i]. 用来存储该结点(site)的所有子结点 ...
- 学习requests_html
一.获取页面上的所有链接. from requests_html import HTMLSession session=HTMLSession() r=session.get('https://new ...
- Ubuntu 16.04上搭建CDH5.16.1集群
本文参考自:<Ubuntu16.04上搭建CDH5.14集群> 1.准备三台(CDH默认配置为三台)安装Ubuntu 16.04.4 LTS系统的服务器,假设ip地址分布为 192.168 ...
- JxBrowser之四:对Http Response Code的处理
1.由于各种原因,客户端或者服务端都可能出现err,比如服务端无响应的504 Gateway Time-out 4×× 客户错误 5×× 服务器错误 2.使用下面代码,当发生错误时,重新加载对应的ur ...
- 覃超:Facebook的项目开发流程和工程师的绩效管理机制
覃超:Facebook的项目开发流程和工程师的绩效管理机制 http://mp.weixin.qq.com/s?__biz=MjM5MDE0Mjc4MA==&mid=2650992350&am ...
- xxx征集系统项目目标文档
分组:每四人一组 主题:xxx征集系统 成果: 讨论结束后,每组提交一份课堂讨论记录(电子版发表到博客上,纸质版小组成员签名,下节课提交). 每人根据课堂讨论结果提交一份系统利益相关者描述案例.撰写项 ...