Fibonacci
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11587   Accepted: 8229

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:

.

题意:就是求斐波那契数列,只是有点大,递推也会超时,矩阵快速幂0ms,搞定;

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = ;
const int mod = ;
struct Mat
{
int mat[][];
};
Mat operator * (Mat a, Mat b)
{
Mat c;
memset(c.mat, , sizeof(c.mat));
for(int k = ; k < N; k++)
{
for(int i = ; i < N; i++)
{
for(int j = ; j < N; j++)
{
c.mat[i][j] = (c.mat[i][j] + a.mat[i][k] * b.mat[k][j] % mod) % mod;
}
}
}
return c;
}
Mat operator ^ (Mat a, int k)
{
Mat c;
for(int i = ; i < N; i++)
for(int j = ; j < N; j++)
c.mat[i][j] = (i == j);
while(k)
{
if(k & )
c = c * a;
a = a * a;
k >>= ;
}
return c;
}
int main()
{
int n;
while(scanf("%d", &n) != EOF)
{
if(n == -)
break;
if(n == )
printf("0\n");
else if(n == )
printf("1\n");
else
{
Mat a;
a.mat[][] = ;
a.mat[][] = ;
a.mat[][] = ;
a.mat[][] = ;
a = a ^ (n - );
printf("%d\n",a.mat[][]);
}
} return ;
}

POJ3070Fibonacci(矩阵快速幂+高效)的更多相关文章

  1. POJ-3070Fibonacci(矩阵快速幂求Fibonacci数列) uva 10689 Yet another Number Sequence【矩阵快速幂】

    典型的两道矩阵快速幂求斐波那契数列 POJ 那是 默认a=0,b=1 UVA 一般情况是 斐波那契f(n)=(n-1)次幂情况下的(ans.m[0][0] * b + ans.m[0][1] * a) ...

  2. nyoj_148_fibonacci数列(二)_矩阵快速幂

    fibonacci数列(二) 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 In the Fibonacci integer sequence, F0 = 0, F ...

  3. 矩阵快速幂小结-Hdu2604

    矩阵快速幂可以想象为线性代数的矩阵相乘,主要是运用于高效的计算矩阵高次方. 将矩阵两两分组,若要求a^n,即知道a^(n/2)次方即可,矩阵快速幂便是运用的这个思路. 比方想求(A)^7那么(A)^6 ...

  4. 整数快速乘法/快速幂+矩阵快速幂+Strassen算法

    快速幂算法可以说是ACM一类竞赛中必不可少,并且也是非常基础的一类算法,鉴于我一直学的比较零散,所以今天用这个帖子总结一下 快速乘法通常有两类应用:一.整数的运算,计算(a*b) mod c  二.矩 ...

  5. leetcode_935. Knight Dialer_动态规划_矩阵快速幂

    https://leetcode.com/problems/knight-dialer/ 在如下图的拨号键盘上,初始在键盘中任意位置,按照国际象棋中骑士(中国象棋中马)的走法走N-1步,能拨出多少种不 ...

  6. 矩阵快速幂 HDU 4565 So Easy!(简单?才怪!)

    题目链接 题意: 思路: 直接拿别人的图,自己写太麻烦了~ 然后就可以用矩阵快速幂套模板求递推式啦~ 另外: 这题想不到或者不会矩阵快速幂,根本没法做,还是2013年长沙邀请赛水题,也是2008年Go ...

  7. 51nod 算法马拉松18 B 非010串 矩阵快速幂

    非010串 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 如果一个01字符串满足不存在010这样的子串,那么称它为非010串. 求长度为n的非010串的个数.(对1e9+7取模) ...

  8. 51nod 1113 矩阵快速幂

    题目链接:51nod 1113 矩阵快速幂 模板题,学习下. #include<cstdio> #include<cmath> #include<cstring> ...

  9. 【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】

    还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. ...

随机推荐

  1. Linux下squid代理缓存服务环境部署

    代理服务器英文全称是Proxy Server,其功能就是代理网络用户去取得网络信息. Squid是一个缓存Internet 数据的软件,其接收用户的下载申请,并自动处理所下载的数据.当一个用户想要下载 ...

  2. iOS数据本地持久化

      p1:归档.Preference(NSUserDefault).沙盒存储 iOS开发中本地存储主要有三种形式 XML属性列表(plist)归档 Preference(偏好设置) NSKeyedAr ...

  3. 在Function对象上扩展method方法

    ;(function() { /** * 在Function对象上扩展method方法 * @param {String} name 扩展的方法名称 * @param {Function} callb ...

  4. f2fs源码分析之文件读写过程

    本篇包括三个部分:1)f2fs 文件表示方法: 2)NAT详细介绍:3)f2fs文件读写过程:4) 下面详细阐述f2fs读写的过程. 管理数据位置关键的数据结构是node,node包括三种:inode ...

  5. 我们为什么要使用NodeJS

    科普文一则,说说我对NodeJS(一种服务端JavaScript实现)的一些认识,以及我为什么会向后端工程师推荐NodeJS. "Node.js 是服务器端的 JavaScript 运行环境 ...

  6. [经验分享] 最近调试FT232H遇到的坑

    cnblogs.com Yeats叶子 原创,转载请注明原始地址 - http://www.cnblogs.com/xiedidan/p/ft232h-poc.html Abstract FT232H ...

  7. [资料]pthreads PHP

    1. 参考手册http://php.net/manual/zh/book.pthreads.php 2. windows下安装php真正的多线程扩展pthreads教程http://www.think ...

  8. 从Python爬虫到SAE云和微信公众号:二、新浪SAE上搭建微信服务

    目的:用PHP在SAE上搭建一个微信公众号的服务器. 1.申请一个SAE云账号 SAE申请地址:http://sae.sina.com.cn/  可以使用微博账号登陆,SAE是新浪的云服务,时间也比较 ...

  9. jQuery使用ajax跨域获取数据

    var webMethod = "http://localhost:54473/Service1.asmx/HelloWorld";  jQuery.support.cors = ...

  10. 【转】其实你不知道MultiDex到底有多坑

    遭遇MultiDex 愉快地写着Android代码的总悟君往工程里引入了一个默默无闻的jar然后Run了一下, 经过漫长的等待AndroidStudio构建失败了. 于是带着疑惑查看错误信息. UNE ...