<题目链接>

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

 解题分析:

由于n很大,所以直接计算时不可行的,可以用矩阵快速幂来加速,并且,此题直接给出了矩阵的递推式,于是,我们只要按照题意构造矩阵即可。

#include <cstdio>
#include <cstring> const int mod=; struct Matrix{
int m[][];
Matrix(){}
Matrix(int x,int y,int z,int k){
m[][]=x;
m[][]=y;
m[][]=z;
m[][]=k;
}
}; Matrix Mul(Matrix a,Matrix b){
Matrix temp;
for(int i=;i<;i++){
for(int j=;j<;j++){
temp.m[i][j]=;
for(int k=;k<;k++){
temp.m[i][j]=(temp.m[i][j]+a.m[i][k]%mod*b.m[k][j]%mod)%mod;
}
}
}
return temp;
} Matrix pow_Mul(Matrix x,int c){
Matrix tmp(,,,);
while(c>){
if(c&){
tmp=Mul(tmp,x);
}
c>>=;
x=Mul(x,x);
}
return tmp;
} int main(){
int n;
while(scanf("%d",&n)!=EOF){
if(n==-)break;
int f[]={,,,};
if(n<=){
printf("%d\n",f[n]);
continue;
}
Matrix init(f[],f[],f[],f[]);
Matrix tmp(,,,);
Matrix ans=Mul(pow_Mul(tmp,n-),init);
printf("%d\n",ans.m[][]%mod);
}
return ;
}

2018-08-21

POJ 3070 Fibonacci 【矩阵快速幂】的更多相关文章

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

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

  2. poj 3070 Fibonacci 矩阵快速幂

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

  3. POJ 3070 Fibonacci 矩阵快速幂模板

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18607   Accepted: 12920 Descr ...

  4. POJ 3070 Fibonacci矩阵快速幂 --斐波那契

    题意: 求出斐波那契数列的第n项的后四位数字 思路:f[n]=f[n-1]+f[n-2]递推可得二阶行列式,求第n项则是这个矩阵的n次幂,所以有矩阵快速幂模板,二阶行列式相乘, sum[ i ] [ ...

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

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

  6. POJ——3070Fibonacci(矩阵快速幂)

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12329   Accepted: 8748 Descri ...

  7. UVA - 10229 Modular Fibonacci 矩阵快速幂

                                 Modular Fibonacci The Fibonacci numbers (0, 1, 1, 2, 3, 5, 8, 13, 21, 3 ...

  8. POJ 3744 【矩阵快速幂优化 概率DP】

    搞懂了什么是矩阵快速幂优化.... 这道题的重点不是DP. /* 题意: 小明要走某条路,按照个人兴致,向前走一步的概率是p,向前跳两步的概率是1-p,但是地上有地雷,给了地雷的x坐标,(一维),求小 ...

  9. poj3070 Fibonacci 矩阵快速幂

    学了线代之后 终于明白了矩阵的乘法.. 于是 第一道矩阵快速幂.. 实在是太水了... 这差不多是个模板了 #include <cstdlib> #include <cstring& ...

  10. poj 3735 稀疏矩阵矩阵快速幂

    设人数为 $n$,构造 $(n + 1) \times (n + 1)$ 的矩阵 得花生:将改行的最后一列元素 $+ 1$ \begin{gather}\begin{bmatrix}1 & 0 ...

随机推荐

  1. 3、输入一个链表,按链表值从尾到头的顺序返回一个ArrayList。

    题目描述 输入一个链表,按链表值从尾到头的顺序返回一个ArrayList. 思路: 利用栈“先进后出”的性质,将链表的值存入到栈里,然后将栈里的值存入到构建好的容器里,最后打印容器. class So ...

  2. Flask最强攻略 - 跟DragonFire学Flask - 第五篇 做一个用户登录之后查看学员信息的小例子

    需求: 1. 用户名: oldboy 密码: oldboy123 2. 用户登录成功之后跳转到列表页面 3. 失败有消息提示,重新登录 4.点击学生名称之后,可以看到学生的详细信息 后端: from ...

  3. mysql 查询优化案例汇总

    一 简介:此文章为经历过的sql案例集合和相关思路 二 案例1: 现象: 测试环境出现select语句,join2张表多次join,explain结果如下 出现 using where,using j ...

  4. 论文笔记系列-DARTS: Differentiable Architecture Search

    Summary 我的理解就是原本节点和节点之间操作是离散的,因为就是从若干个操作中选择某一个,而作者试图使用softmax和relaxation(松弛化)将操作连续化,所以模型结构搜索的任务就转变成了 ...

  5. android 使用web查看SQLite数据

    添加依赖: compile 'com.facebook.stetho:stetho:1.4.2'compile 'com.facebook.stetho:stetho-okhttp3:1.4.2' 初 ...

  6. 一套oracle的练习题

    create table student( sno varchar2(10) primary key, sname varchar2(20), sage number(2), ssex varchar ...

  7. Debian 安装配置(包括kdevelop)

    最近几天折腾了一下Debian 7 (gnome桌面DVD版,KDE桌面CD版最后会提到),总的来说收获还是挺大的,对比以前使用ubuntu,debian 7给我的感觉像是一个新生婴儿,不带多余的花俏 ...

  8. weblogic实时监控开发

    参考api文档 https://docs.oracle.com/cd/E13222_01/wls/docs90/wlsmbeanref/core/index.html https://docs.ora ...

  9. SSH原理与运用:远程登录

    一.什么是SSH? 简单说,SSH是一种网络协议,用于计算机之间的加密登录. 如果一个用户从本地计算机,使用SSH协议登录另一台远程计算机,我们就可以认为,这种登录是安全的,即使被中途截获,密码也不会 ...

  10. centos 命令和

    一.远程工具 Window系统上 Linux 远程登录客户端有SecureCRT, Putty, SSH Secure Shell.TightVNC... 重点推荐一款 FinallShell,一般人 ...