Fibonacci

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

 

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:

.

题意:求Fibonacci数列的第n项。

注意n很大,用矩阵,就可以了。

题目链接:http://poj.org/problem?id=3070  转载请注明出处:寻找&星空の孩子

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define LL __int64
#define mod 10000
struct matrix
{
LL mat[][];
}; matrix multiply(matrix a,matrix b)
{
matrix c;
memset(c.mat,,sizeof(c.mat));
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
if(a.mat[i][j]==)continue;
for(int k=;k<;k++)
{
if(b.mat[j][k]==)continue;
c.mat[i][k]+=a.mat[i][j]*b.mat[j][k]%mod;
c.mat[i][k]%=mod;
}
}
}
return c;
} matrix quicklymod(matrix a,LL n)
{
matrix res;
memset(res.mat,,sizeof(res.mat));
for(int i=;i<;i++) res.mat[i][i]=;
while(n)
{
if(n&)
res=multiply(a,res);
a=multiply(a,a);
n>>=;
}
return res;
} int main()
{
LL n;
while(scanf("%I64d",&n)!=EOF)
{
if(n==-)break;
matrix ans;
ans.mat[][]=;
ans.mat[][]=;
ans.mat[][]=;
ans.mat[][]=; if(n==)
{
printf("0\n");
continue;
}
/* else if(n==1)
{
printf("1\n");
continue;
}*/ else
ans=quicklymod(ans,n); /* for(int i=0; i<2; i++)
{
for(int j=0; j<2; j++)
printf("%I64d\t",ans.mat[i][j]);
printf("\n");
}
printf("\n");
*/
printf("%I64d\n",ans.mat[][]);
}
return ;
}

矩阵入门题!

Fibonacci(矩阵)的更多相关文章

  1. hdu 1588(Fibonacci矩阵求和)

    题目的大意就是求等差数列对应的Fibonacci数值的和,容易知道Fibonacci对应的矩阵为[1,1,1,0],因为题目中f[0]=0,f[1]=1,所以推出最后结果f[n]=(A^n-1).a, ...

  2. BZOJ3286 Fibonacci矩阵 矩阵 快速幂 卡常

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ3286 题意概括 n,m,a,b,c,d,e,f<=10^1000000 题解 神奇的卡常题目 ...

  3. POJ3070 Fibonacci[矩阵乘法]

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13677   Accepted: 9697 Descri ...

  4. 【全国互虐】Fibonacci矩阵

    orz啊又被屠了 人生如此艰难 题意: 给定一个k维的n^k的超立方体 超立方体的元素Ai1,i2,...,ik 的值为f(i1+i2+...+ik-k+1) f为斐波那契数列 求该超立方体的所有元素 ...

  5. poj 3070 Fibonacci (矩阵快速幂乘/模板)

    题意:给你一个n,输出Fibonacci (n)%10000的结果 思路:裸矩阵快速幂乘,直接套模板 代码: #include <cstdio> #include <cstring& ...

  6. poj 3070 Fibonacci 矩阵快速幂

    Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. F ...

  7. HDU 3306 Another kind of Fibonacci(矩阵+ll超时必须用int&输入必须取模&M必须是int类型)

    Another kind of Fibonacci [题目链接]Another kind of Fibonacci [题目类型]矩阵+ll超时必须用int&输入必须取模&M必须是int ...

  8. POJ3070 Fibonacci[矩阵乘法]【学习笔记】

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13677   Accepted: 9697 Descri ...

  9. HDU 1588 Gauss Fibonacci(矩阵快速幂)

    Gauss Fibonacci Time Limit: 3000/1000 MS (Java/Others)     Memory Limit: 32768/32768 K (Java/Others) ...

  10. HDU1588-Gauss Fibonacci(矩阵高速幂+等比数列二分求和)

    题目链接 题意:g(x) = k * x + b.f(x) 为Fibonacci数列.求f(g(x)),从x = 1到n的数字之和sum.并对m取模. 思路:  设A = |(1, 1),(1, 0) ...

随机推荐

  1. AEAI WM v1.6.0 升级说明,开源工作管理系统

    1 升级说明 AEAI WM v1.6.0版是AEAI WM v1.5.0版工作管理系统的升级版本,本次升级的系统是基于AEAI DP 3.8.0_20170228进行打包部署的,对产品中的功能及BU ...

  2. 【计算机网络】TCP通信的细节及TCP连接对HTTP事务处理性能影响

    从三次握手的细节说起 刚开始尝试使用java等后端语言写IO流,或用套接字(socket)实现简单C/S通信的同学们,常常会接触到的一个概念:就是所谓的“三次握手”,socket作为一个API接口,封 ...

  3. docker容器间跨主机通信

    http://jnzg905.iteye.com/blog/2269583 https://blog.csdn.net/pingpangbing0902/article/details/7823889 ...

  4. Code Chef MINPOLY(计算几何+dp)

    题面 传送门 题解 我们枚举这个凸多边形\(y\)坐标最小的点\(p_i\),然后对于所有\(y\)坐标大于等于它的点极角排序 我们预处理出\(s_{j,k}\)表示三角形\(p_i,p_j,p_k\ ...

  5. 试着用java实现DNS(一)——DatagramSocket, DatagramPacket, Message

    一般来说,自己编写DNS是没有必要的,目前开源的dns服务软件很多,功能也很强大.但是,有时候又是很有必要的,有着诸多好处.比如说,用于企业内网,简化DNS配置,可以根据企业需求添加新的功能,非常灵活 ...

  6. Shell - 简明Shell入门09 - 重定向(Redirection)

    示例脚本及注释 #!/bin/bash pwd > 1.log # 输出重定向到指定文件 date 1> 1.log # ">"与"1>" ...

  7. WebDriver高级应用实例(7)

    7.1在测试中断言失败的步骤进行屏幕截图 目的:在测试过程中,在断言语句执行失败时,对当前的浏览器进行截屏,并在磁盘上新建一个yyyy-mm-dd格式的目录,并在断言失败时新建一个已hh-mm-ss格 ...

  8. POJ 2453

    #include <iostream> #include <algorithm> #include <cmath> #define MAXN 1000 #defin ...

  9. Flask-socketio聊天室 (附源码)

    `* A Chat Room Completed with Flask-socketio 功能: 实时消息更新,实时用户上线显示 快捷注册 快捷登陆 表情支持 截图: 使用方法: 安装依赖:pip i ...

  10. 基于GTK+3 开发远程控制管理软件(C语言实现)系列 一 开篇

    近期趁公司没项目来,想学习一下C和GTK+3 ,顺道再学习一下Linux下有关网络编程方面的知识. 一.学习知识: 1.C基本语法 2.GTK+3 API学习 GUI相关知识学习 3.Glade使用及 ...