Fibonacci
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 13732   Accepted: 9728

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:

.

这道题就是根据题意算快速幂。裸,但是这个规律比较重要了。

#include <map>
#include <set>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <iostream>
#include <stack>
#include <cmath>
#include <string>
#include <vector>
#include <cstdlib>
//#include <bits/stdc++.h>
//#define LOACL
#define space " "
using namespace std;
typedef long long LL;
typedef __int64 Int;
typedef pair<int, int> paii;
const int INF = 0x3f3f3f3f;
const double ESP = 1e-5;
const double PI = acos(-1.0);
const int MOD = 1e9 + 7;
const int MAXN = 100 + 10;
int n, mod;
LL m;
struct Matrix {LL m[3][3];int row, col;
};
Matrix ori, res;
void init() {memset(res.m, 0, sizeof(res.m));ori.m[1][1] = ori.m[1][2] = ori.m[2][1] = 1;
}
Matrix multi(Matrix x, Matrix y) {Matrix z;memset(z.m, 0, sizeof(z.m));for (int i = 1; i <= 2; i++) {for (int k = 1; k <= 2; k++) {for (int j = 1; j <= 2; j++) {z.m[i][j] += x.m[i][k]*y.m[k][j]%mod;}z.m[i][k] %= mod;}}return z;
}
Matrix pow_mod(Matrix a, LL x){Matrix b;memset(b.m, 0, sizeof(b.m));for (int i = 1; i <= 2; i++) {b.m[i][i] = 1;}while(x){if(x&1) b = multi(a,b);a = multi(a, a);x >>= 1;}return b;
}
int main() {while (scanf("%lld", &m), m != -1) {init(); mod = 10000;res = pow_mod(ori, m);printf("%lld\n", res.m[1][2]%mod);}return 0;
}

转载于:https://www.cnblogs.com/cniwoq/p/6770759.html

poj - problem 3070 Fibonacci 【矩阵 +快速幂】相关推荐

  1. POJ 3070 Fibonacci(矩阵快速幂入门、模板)

    ? 题目链接:http://poj.org/problem?id=3070 ?   这题就是让求斐波那契数列的第n项,但是题目中n很大,所以打表和直接求都会TLE,对于这个题我们可以用矩阵快速幂,下面 ...

  2. H - Fibonacci POJ - 3070 (矩阵快速幂)

    H - Fibonacci POJ - 3070 (矩阵快速幂) Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and ...

  3. poj 2778 AC自动机+矩阵快速幂

    题目链接:https://vjudge.net/problem/POJ-2778 题意:输入n和m表示n个病毒,和一个长为m的字符串,里面只可以有'A','C','G','T' 这四个字符,现在问这个 ...

  4. poj3070 Fibonacci 矩阵快速幂

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18084   Accepted: 12572 Descr ...

  5. LightOJ 1070 Algebraic Problem (推导+矩阵快速幂)

    题目链接:LightOJ 1070 Algebraic Problem 题意:已知a+b和ab的值求a^n+b^n.结果模2^64. 思路: 1.找递推式 得到递推式之后就是矩阵快速幂了 注意:模2^ ...

  6. HDU 3306 Another kind of Fibonacci 矩阵快速幂

    题目链接 因为S(N) , S(N) = A(0)^2 +A(1)^2+--+A(n)^2.所以构造的矩阵一定要维护A(n)^2 s[n-1]=s[n-2]+A[n-1]^2 A[n]=x*A[n-1 ...

  7. 矩阵快速幂 POJ 3070 Fibonacci

    题目传送门 1 /* 2 矩阵快速幂:求第n项的Fibonacci数,转置矩阵都给出,套个模板就可以了.效率很高啊 3 */ 4 #include <cstdio> 5 #include ...

  8. I-Matrix Power Series POJ - 3233 矩阵快速幂+分治

    I-Matrix Power Series POJ - 3233 矩阵快速幂+分治 Problem Description Given a n × n matrix A and a positive ...

  9. POJ 2778 DNA Sequence [AC自动机 + 矩阵快速幂]

    http://poj.org/problem?id=2778 题意:给一些只由ACGT组成的模式串,问有多少种长度为n且不含有给出的模式串的DNA序列. 自动机的状态转换可以看成一个有向图(有重边的) ...

最新文章

  1. ORB-SLAM2系统的实时点云地图构建
  2. Mqtt paho 回调函数触发机制跟踪
  3. 《JavaScript数据可视化编程》——1.4 用离散图表绘制x/y值
  4. SQL Server 2012高可用性组
  5. PHP程序员的技术成长规划(转)
  6. python干货_python 基础干货 02
  7. 去小公司了解哪些工具?
  8. java json u0026_特殊字符的json序列化
  9. (软件工程复习核心重点)第十二章软件项目管理-第四节:软件配置管理和能力成熟度模型
  10. AI一周热闻:北大建立人工智能新校区;英国首例机器人心脏手术致死
  11. I2C总线协议的简要说明
  12. appium历史版本下载
  13. python 特征选择方法_机器学习小窍门:Python 帮你进行特征选择
  14. android10一键root权限获取,完美root方法教你如何一键获取手机ROOT权限
  15. html中calc属性什么意思,深入理解CSS calc属性
  16. invalid vcs root mapping 怎么解决_一加黑鲨华硕OPPO等手机root后微信指纹支付不可用怎么解决...
  17. apk开发教程!了解Android架构组件后,构建APP超简单!先收藏了
  18. 实验一 基于TCP和UDP的客户端和服务器端
  19. 前端模拟终端(三):文本显示与自动换行
  20. 使用lodop设置导出表格的数据格式

热门文章

  1. 激活MyEclipse 6.5方法-通过一段Java程序生成激活码
  2. SharePoint 2007有性能问题? 先试试这篇.
  3. 20岁黑客发现苹果核心 web 应用中的严重漏洞,获奖$3.6万
  4. CloudFlare CDNJS 漏洞差点造成大规模的供应链攻击
  5. jQuery基础系列
  6. 接口,实现类,对象方法的调用关系.(查看程序输出)
  7. 京东的交易系统 之 高并发架构分享
  8. tuning 02 Diagnostic and Tuning Tools
  9. [导入]在vs2005中调用远程WebService(幻想曲)
  10. Python中dict详解