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. 约定优于配置---Java的eclipse项目配置

    0.测试文件夹test (测试文件的文件夹和源文件夹src是并行的关系,且位于同一目录) 以后源文件.java文件放在src目录下,相应的单元测试文件放在同级别的test目录下,且内部路径要相同 1. ...

  2. devm_kzalloc【转】

    本文转载自:https://blog.csdn.net/liuhuahan/article/details/42145507 看内核代码的时候看到这个函数不理解它的具体作用然后就上网上查,但是网上只查 ...

  3. 论文笔记:Visual Semantic Navigation Using Scene Priors

    Visual Semantic Navigation Using Scene Priors 2018-10-21 19:39:26 Paper:  https://arxiv.org/pdf/1810 ...

  4. 编码原则 之 Hollywood Principle

    原文 The Hollywood Principle states, “Don’t Call Us, We’ll Call You.” It’s closely related to the Depe ...

  5. FastJson中JSONObject用法及常用方法总结

    本文为博主原创,未经允许不得转载: 最近一直有用到解析各种数据,主要是用FastJson进行数据解析,其中一个重要的类为JSONObject,今天有时间,所以进行总结一下: JSONobject是Fa ...

  6. c# 7.1 Async Main方法

    安装 .net framework sdk 7.1 新建一个 .net framework 7.1 的程序 在程序的工程文件的第一个 PropertyGroup  节点下加入以下子属性   <L ...

  7. 使用C#加密及解密字符串

    using System; using System.IO; using System.Security.Cryptography; using System.Text; namespace Util ...

  8. hibernate 的API使用

    1.Query对象:不需要写sql语句,但需要hql语句,和sql很类似 (1)sql和hql区别:sql操作表和表字段,hql操作实体和实体属性 (2)使用: 2.Criteria对象:不需要写语句 ...

  9. JVM垃圾回收(一)- 什么是垃圾回收

    什么是垃圾回收? 垃圾回收是追踪所有正在被使用的对象,并标注剩余的为garbage.这里我们先从JVM的GC是如何实现的说起. 手动内存管理 在开始介绍垃圾回收之前,我们先复习一下手动内存管理.它是指 ...

  10. Lab 6-4

    In this lab, we'll analyze the malware found in the file Lab06-04.exe. Questions and Short Answers W ...