POJ 3070 Fibonacci 矩阵快速幂模板
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 18607 | Accepted: 12920 |
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:
.
Source
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#define debug(a) cout << #a << " " << a << endl
using namespace std;
const int maxn = 1e4 + 10;
const int mod = 10000;
typedef long long ll;
struct matrix {
ll a[10][10];
};
matrix base, ans;
matrix multip( matrix x, matrix y ) { //求c矩阵的过程
matrix tmp;
for( ll i = 0; i < 2; i ++ ) {
for( ll j = 0; j < 2; j ++ ) {
tmp.a[i][j] = 0;
for( ll k = 0; k < 2; k ++ ) {
tmp.a[i][j] = ( tmp.a[i][j] + x.a[i][k] * y.a[k][j] ) % mod;
}
}
}
return tmp;
}
ll qow( ll a, ll b ) { //求数的快速幂,与此题无关
ll sum = 1;
while( b ) {
if( b&1 ) {
sum = sum*a%mod;
}
a = a*a%mod;
b /= 2;
}
return sum;
}
ll f( ll x ) { //矩阵快速幂的运算
while( x ) {
if( x&1 ) {
ans = multip( ans, base );
}
base = multip( base, base );
x /= 2;
}
return ans.a[0][0];
}
int main() {
ll n;
while( cin >> n ) {
if( n == -1 ) {
break;
}
memset( ans.a, 0, sizeof(ans.a) ); //初始化a矩阵和b矩阵,根据你所得到的矩阵式初始化
memset( base.a, 0, sizeof(base.a) );
ans.a[0][0] = 1, ans.a[0][1] = 0;
base.a[0][0] = base.a[0][1] = base.a[1][0] = 1;
if( n == 0 ) {
cout << 0 << endl;
} else if( n == 1 ) {
cout << 1 << endl;
} else {
cout << f(n-1) << endl;
}
}
return 0;
}
POJ 3070 Fibonacci 矩阵快速幂模板的更多相关文章
- POJ 3070 Fibonacci矩阵快速幂 --斐波那契
题意: 求出斐波那契数列的第n项的后四位数字 思路:f[n]=f[n-1]+f[n-2]递推可得二阶行列式,求第n项则是这个矩阵的n次幂,所以有矩阵快速幂模板,二阶行列式相乘, sum[ i ] [ ...
- 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 ...
- POJ3070:Fibonacci(矩阵快速幂模板题)
http://poj.org/problem?id=3070 #include <iostream> #include <string.h> #include <stdl ...
- POJ3070 矩阵快速幂模板
题目:http://poj.org/problem?id=3070 矩阵快速幂模板.mod写到乘法的定义部分就行了. 别忘了 I ( ) 和 i n i t ( ) 要传引用! #include< ...
- 矩阵快速幂模板(pascal)
洛谷P3390 题目背景 矩阵快速幂 题目描述 给定n*n的矩阵A,求A^k 输入输出格式 输入格式: 第一行,n,k 第2至n+1行,每行n个数,第i+1行第j个数表示矩阵第i行第j列的元素 输出格 ...
- 51nod1113(矩阵快速幂模板)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1113 题意:中文题诶- 思路:矩阵快速幂模板 代码: #inc ...
- luoguP3390(矩阵快速幂模板题)
链接:https://www.luogu.org/problemnew/show/P3390 题意:矩阵快速幂模板题,思路和快速幂一致,只需提供矩阵的乘法即可. AC代码: #include<c ...
- HDU 1588 Gauss Fibonacci(矩阵快速幂)
Gauss Fibonacci Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
随机推荐
- SpringBoot Jar包瘦身 - 跟大文件说再见!
前言 SpringBoot部署起来配置非常少,如果服务器部署在公司内网,上传速度还行,但是如果部署在公网(阿里云等云服务器上),部署起来实在头疼.就是 编译出来的 Jar 包很大,如果工程引入了许多开 ...
- 林大妈的JavaScript基础知识(三):JavaScript编程(4)数组
数组,是一段线性分配的,具有非常高性能的数据结构.简单地说,数组以连续的空间存储,通过整数地计算偏移量访问其中的元素,将读取修改的时间复杂度降低至O(1),我们称之为猝发式存取.是不是非常期待?没错, ...
- <<Modern CMake>> 翻译 2.4 项目目录结构
<<Modern CMake>> 翻译 2.4 项目目录结构 本节内容有点跑题.但我认为这是一个很好的方法. 我将告诉你如何规划项目的目录. 这是基于惯例,但将帮助您: 轻松阅 ...
- ubuntu18.04下安装matlab2018a
一.下载 百度网盘链接:https://pan.baidu.com/s/1M6KafnsljmYV9_5m_1pXMw 提取玛:jp76 二.安装 下载下来的文件夹中有三个文件,分别是破解文文件与映像 ...
- Re-Architecting the Video Gatekeeper(一)
原文 https://medium.com/netflix-techblog/re-architecting-the-video-gatekeeper-f7b0ac2f6b00 本文介绍了了内容配置工 ...
- 利用hash或history实现单页面路由
目录 html代码 css代码 JavaScript代码 hash方式 history 方式 浏览器端代码 服务器端 在chrome(版本 70.0.3538.110)测试正常 编写涉及:css, h ...
- Java——检测其他线程的状态以及启动已死亡的线程
这次这个的思路是在主类中维护一个map,map的key是线程名,value是线程的状态,然后创建周期执行的线程通过检测这个map来判断进程的状态,如果有死亡的进程就把该进程启动. 首先是主类,这里的m ...
- (七)c#Winform自定义控件-进度条
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- 暂停研发surging,是否继续维护!
前言 surging从2017 年开始,2 年来利用业余时间为 surging语言添砖加瓦. 这种活雷锋行为并没有得到开发者们的理解,很多人甚至用命令的口吻,灵魂拷问方式要求活雷锋们再苦再累也得免费为 ...
- .NET使用Bogus生成大量随机数据
.NET如何生成大量随机数据 在演示Demo.数据库脱敏.性能测试中,有时需要生成大量随机数据.Bogus就是.NET中优秀的高性能.合理.支持多语言的随机数据生成库. Bogus的Github链接: ...