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的更多相关文章

  1. 矩阵快速幂 POJ 3070 Fibonacci

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

  2. 【POJ】3070 Fibonacci(矩阵乘法)

    http://poj.org/problem?id=3070 根据本题算矩阵,用快速幂即可. 裸题 #include <cstdio> #include <cstring> # ...

  3. POJ 3070 Fibonacci

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

  4. 矩阵经典题目六:poj 3070 Fibonacci

    http://poj.org/problem?id=3070 按已构造好的矩阵,那么该矩阵的n次方的右上角的数便是f[n]. #include <stdio.h> #include < ...

  5. POJ 3070 Fibonacci(矩阵高速功率)

    职务地址:POJ 3070 用这个题学会了用矩阵高速幂来高速求斐波那契数. 依据上个公式可知,第1行第2列和第2行第1列的数都是第n个斐波那契数.所以构造矩阵.求高速幂就可以. 代码例如以下: #in ...

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

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

  7. poj 3070 Fibonacci 矩阵快速幂

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

  8. POJ 3070 Fibonacci 【矩阵快速幂】

    <题目链接> Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 ...

  9. poj 3070 Fibonacci 矩阵相乘

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7715   Accepted: 5474 Descrip ...

随机推荐

  1. 20190402Linux常用命令week1.1

    Linux常用命令详解week1.1 基础命令:lsmanpwdcdmkdirechotouchcpmvrmrmdircatmorelessheadtailclearpoweroffreboot 命令 ...

  2. Python 简单soket例子

      简单的soket例子 Python 2.0 客户端服务端传输 1.可发字符串,可发字节 bys类型 Python 3.0 客户端服务端传输 1.只能发bys,比特流的类型. 2.bys类型只能接收 ...

  3. 深入学习IOZone【转】

    本文转载自:https://blog.csdn.net/werm520/article/details/7262103 深入学习IOZone 刘智朋       2011-3-29 1        ...

  4. Windows Server 2008 R2 下载地址

    以下资源均来自微软 MSDN,是原汁原味的原版系统资源,值得系统爱好者收藏.以下多数链接是 ed2k 链接,推荐使用国外开源的 eMule 下载,亦可使用迅雷,但使用 eMule 更有利于共享资源. ...

  5. CSS【05】:CSS三大特性

    继承性 作用:给父元素设置一些属性,子元素也可以使用,这个我们就称之为继承性 示例代码: <style> div { color: red; } </style> <di ...

  6. 在WPF中调用文件夹浏览/选择对话框

    var dialog = new System.Windows.Forms.FolderBrowserDialog(); System.Windows.Forms.DialogResult resul ...

  7. MapReduce编程:词频统计

    首先在项目的src文件中需要加入以下文件,log4j的内容为: log4j.rootLogger=INFO, stdout log4j.appender.stdout=org.apache.log4j ...

  8. Nat Med:单独使用anti-CTLA4治疗前列腺癌效果差的原因

    肿瘤细胞能够分泌特定的细胞因子,结合T细胞表面的受体抑制其活性,从而来影响免疫细胞杀死肿瘤细胞的能力.这一类细胞因子被冠名为抗肿瘤免疫反应的“check point”.针对这类check point信 ...

  9. python 过滤四字节字符 表情字符

    项目中有时需要过滤掉四字节以上的字符(表情),比如mysql数据库5.5.3以下的版本text字段不支持四字节以上字符 于是就需要过滤掉再入库,python中的方法为:   try:   # pyth ...

  10. caffe生成deploy.prototxt文件

    参考: http://blog.csdn.net/cham_3/article/details/52682479 以caffe工程自带的mnist数据集,lenet网络为例: 将lenet_train ...