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次幂
int ans=1;
while(n){
    if(n&1)
        ans*=x;
    x*=x;
    n>>=1;
就是此模板啦,将相应的ans 和x换成矩阵就行*/#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int n,a[2][2],ans[2][2];
void mul(int a[2][2],int b[2][2]){
    int temp[2][2];
    memset(temp,0,sizeof temp);
    for(int i=0;i<2;i++){        for(int j=0;j<2;j++){            for(int k=0;k<2;k++){                temp[i][j]=(temp[i][j]+a[i][k]*b[k][j])%10000;
            }
        }
    }
    for(int i=0;i<2;i++){        for(int j=0;j<2;j++){            b[i][j]=temp[i][j];
        }
    }
}
int main()
{
    while(scanf("%d",&n)!=EOF){        if(n==-1)
           return 0;
        a[0][0]=a[0][1]=a[1][0]=1;
        a[1][1]=0;
        ans[0][0]=ans[1][1]=1;
        ans[0][1]=ans[1][0]=0;//相当于ans=1,初始化为单位矩阵
        while(n){            if(n&1)
                mul(a,ans);//此过程相当于求ans=ans*a;
            mul(a,a);//相当于求a=a*a;
            n>>=1;
        }
        printf("%d\n",ans[0][1]);
    }
}

A - Fibonacci相关推荐

  1. Fibonacci数列的java实现

    关于Fibonacci应该都比较熟悉,0,1,1,2,3..... 基本公式为f(n) = f(n-1) + f(n-2); f(0) = 0; f(1) =1; 方法1:可以运用迭代的方法实现: p ...

  2. ZOJ 2723 Semi-Prime ||ZOJ 2060 Fibonacci Again 水水水!

    两题水题: 1.如果一个数能被分解为两个素数的乘积,则称为Semi-Prime,给你一个数,让你判断是不是Semi-Prime数. 2.定义F(0) = 7, F(1) = 11, F(n) = F( ...

  3. Codeforces Round #FF 446 C. DZY Loves Fibonacci Numbers

    參考:http://www.cnblogs.com/chanme/p/3843859.html 然后我看到在别人的AC的方法里还有这么一种神方法,他预先设定了一个阈值K,当当前的更新操作数j<K ...

  4. (C++)求Fibonacci数列的第n个数的两种方法

    方法一 #include<cstdio> #include<cmath>int main(){int n;scanf("%d",&n);if(n== ...

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

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

  6. LeetCode 509. Fibonacci Number--Python解法

    题目地址:Fibonacci Number - LeetCode The Fibonacci numbers, commonly denoted F(n) form a sequence, calle ...

  7. 求Fibonacci数列的前20项

    <程序设计基础-c语言>杨莉 刘鸿翔 ISBN-978-7-03-032903-5 p112 习题4 2.编程求Fibonacci数列的前20项. Fibonacci数列的定义:F0=0, ...

  8. 【c语言】蓝桥杯入门训练 Fibonacci数列

    [问题描述] Fibonacci数列的递推公式为:Fn=F(n-1)+F(n-2),其中F1=F2=1. 当n比较大时,Fn也非常大,现在我们想知道,Fn除以10007的余数是多少. [输入格式] 输 ...

  9. Codeforces 446C —— DZY Loves Fibonacci Numbers(线段树)

    题目:DZY Loves Fibonacci Numbers 题意比較简单,不解释了. 尽管官方的题解也是用线段树,但还利用了二次剩余. 可是我没有想到二次剩余,然后写了个感觉非常复杂度的线段树,还是 ...

  10. 1732 Fibonacci数列 2

    1732 Fibonacci数列 2  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解 题目描述 Description 在"1250 Fi ...

最新文章

  1. 秒杀(PHP,Redis,Lua)
  2. axis2 默认端口_使用axis2创建webservice
  3. python期末考试重点_如何应付大学的python考试而不至于挂科?
  4. php 防止url输入,php防止伪造数据从地址栏URL提交的方法
  5. Redis添加密码认证Cacti监控读取Redis状态值为-1的最快速解决方案
  6. vscode中安装webpack_leaflet-webpack 入门开发系列一初探篇(附源码下载)
  7. go语言中输入的方式,获取用户的输入信息
  8. 如何从头开始使用Python实现堆栈泛化(Stacking)
  9. I2C 总线协议详解
  10. GAN (Generative Adversarial Nets 生成对抗网络)
  11. 计算机视觉学习路线—计算机视觉入门必读的20本书
  12. chrome或其它浏览器无法拖拽文件(不仅仅是crx文件)的问题解决
  13. 第二部分 : 简单句的核心变化(时态)
  14. java架构师视频,附源代码
  15. 实验二——网络嗅探与欺骗.
  16. 微信如何群发消息?微信群发消息只需要4步!
  17. 读 S. Meyers 之 《Effective STL 中文版:50条有效使用 STL 的经验》
  18. Windows10系统开机内存占有率超50%的原因
  19. 徒手格斗技巧 源自特种部队 防身必备
  20. HTML5简介(补充. 浏览器私有前缀)

热门文章

  1. 面对批评与争议,苹果让步,延迟推出CSAM儿童保护功能
  2. 不盲追大模型与堆算力!沈向洋、曹颖与马毅提出理解 AI 的两个基本原理:简约性与自一致性...
  3. Mac OS的管理员密码重置方法
  4. 计算机等级怎么查ip地址,ip查地址方法是什么【图文教程】
  5. caj文件打不开显示内存不足_面向大数据的高效存储容量缩减技术研究
  6. win10任务栏显示“中/英“语言标识
  7. CSS让背景图适应整个屏幕(填满)
  8. 支撑马蜂窝「双11」营销大战背后的技术架构
  9. 基于ssm的校园二手物品交易平台(idea+spring+springmvc+mybatis+jsp)
  10. 大学物理实验————自组惠斯通电桥测电阻数据处理代码