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

  1. 0
  2. 9
  3. 999999999
  4. 1000000000
  5. -1

Sample Output

  1. 0
  2. 34
  3. 626
  4. 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. #define ll long long
  2. const ll maxn = 1e6+5;
  3. const ll mod = 1e4;
  4. const double eps = 1e-9;
  5. const double pi = acos(-1.0);
  6. const ll inf = 0x3f3f3f3f;
  7.  
  8. ll n;
  9. struct mat
  10. {
  11. ll a[2][2];
  12. };
  13.  
  14. mat mul(mat A, mat B){
  15. mat r;
  16. memset(r.a, 0, sizeof(r.a));
  17.  
  18. for(ll i = 0; i < 2; i++){
  19. for(ll j = 0; j < 2; j++){
  20. for(ll k = 0; k < 2; k++){
  21. r.a[i][j] += (A.a[i][k]*B.a[k][j])%mod;
  22. r.a[i][j] %= mod;
  23. }
  24. }
  25. }
  26. return r;
  27. }
  28.  
  29. mat qpow(mat A, ll x){
  30. mat B;
  31. B.a[0][0] = B.a[1][1] = 1; // 单位矩阵
  32. B.a[0][1] = B.a[1][0] = 0;
  33.  
  34. while(x){
  35. if (x&1) B = mul(B, A);
  36. A = mul(A, A);
  37. x >>= 1;
  38. }
  39. return B;
  40. }
  41.  
  42. int main() {
  43. //freopen("in.txt", "r", stdin);
  44. //freopen("out.txt", "w", stdout);
  45.  
  46. while(~scanf("%lld", &n)){
  47. if (n == -1) break;
  48.  
  49. mat a;
  50. a.a[0][0] = a.a[0][1] = a.a[1][0] = 1;
  51. a.a[1][1] = 0;
  52.  
  53. if (n == 0) printf("0\n");
  54. else if (n == 1) printf("1\n");
  55. else {
  56. a = qpow(a, n-1);
  57. printf("%d\n", a.a[0][0]%mod);
  58. }
  59.  
  60. }
  61. return 0;
  62. }

Fibnoccia 数列简单题的更多相关文章

  1. acm.njupt 1001-1026 简单题

    点击可展开上面目录 Acm.njupt 1001-1026简单题 第一页许多是简单题,每题拿出来说说,没有必要,也说不了什么. 直接贴上AC的代码.初学者一题题做,看看别人的AC代码,寻找自己的问题. ...

  2. NYOJ 821 简单求值【简单题】

    /* 解题人:lingnichong 解题时间:2014.10.18   00:46 解题体会:简单题 */ 简单求值 时间限制:1000 ms  |  内存限制:65535 KB 难度:1 描写叙述 ...

  3. 【bzoj2751】[HAOI2012]容易题(easy) 数论,简单题

    Description 为了使得大家高兴,小Q特意出个自认为的简单题(easy)来满足大家,这道简单题是描述如下:有一个数列A已知对于所有的A[i]都是1~n的自然数,并且知道对于一些A[i]不能取哪 ...

  4. BZOJ 2683: 简单题

    2683: 简单题 Time Limit: 50 Sec  Memory Limit: 128 MBSubmit: 913  Solved: 379[Submit][Status][Discuss] ...

  5. 【BZOJ-1176&2683】Mokia&简单题 CDQ分治

    1176: [Balkan2007]Mokia Time Limit: 30 Sec  Memory Limit: 162 MBSubmit: 1854  Solved: 821[Submit][St ...

  6. Bzoj4066 简单题

    Time Limit: 50 Sec  Memory Limit: 20 MBSubmit: 2185  Solved: 581 Description 你有一个N*N的棋盘,每个格子内有一个整数,初 ...

  7. Bzoj2683 简单题

    Time Limit: 50 Sec  Memory Limit: 128 MBSubmit: 1071  Solved: 428 Description 你有一个N*N的棋盘,每个格子内有一个整数, ...

  8. 这样leetcode简单题都更完了

    这样leetcode简单题都更完了,作为水题王的我开始要更新leetcode中等题和难题了,有些挖了很久的坑也将在在这个阶段一一揭晓,接下来的算法性更强,我就要开始分专题更新题目,而不是再以我的A题顺 ...

  9. [BZOJ2683][BZOJ4066]简单题

    [BZOJ2683][BZOJ4066]简单题 试题描述 你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要维护两种操作: 命令 参数限制 内容 1 x y A 1<=x ...

随机推荐

  1. Nutch2.3 编译

    $ antBuildfile: build.xmlTrying to override old definition of task javac ivy-probe-antlib: ivy-downl ...

  2. Python--day40--threading模块的几个方法

    import time import threading #threading.get_ident() 查看当前进程号 def wahaha(n): time.sleep(0.5) print(n,t ...

  3. HDU 2871"Memory Control"(线段树区间和并+set.lower_bound)

    传送门 •题意 有 n 个内存单元(编号从1开始): 给出 4 种操作: (1)Reset :表示把所有的内存清空,然后输出 "Reset Now". (2)New x :表示申请 ...

  4. Roslyn 使用 WriteLinesToFile 解决参数过长无法传入

    在写 Roslyn 的时候,经常需要辅助编译的工具,而这些工具需要传入一些参数,在项目很大的时候,会发现自己传入的参数比微软限制控制台可以传入的参数大很多,这时就无法传入了参数 本文告诉大家如何使用 ...

  5. 修改github上的项目语言类型

    当在github上上传一个项目时,可能会出现一个问题就是项目代码类型是自动生成的,可能与我们实际项目代码种类不匹配,此时就需要修改项目语言类型了. 由于无法直接更改,所以用到此方法: 在你的项目根目录 ...

  6. Linux 内核块 urb

    块 urb 被初始化非常象中断 urb. 做这个的函数是 usb_fill_bulk_urb, 它看来如此: void usb_fill_bulk_urb(struct urb *urb, struc ...

  7. js算法(1)

    数组排序 arr.sort(function compare(a,b){return b.value-a.value}); json 排序 $.getJSON('URl',function(data) ...

  8. jquery中报错Uncaught ReferenceError: $ is not defined的解决办法

    jquery中报错提示为:Uncaught ReferenceError: $ is not defined 这个错误的原因就是你没有引入jquery库文件或者引入的路径不对造成的

  9. [工具] Git版本管理(三)(工作流)

    一.冲突解决 Beyond Compare软件 下载BCompare软件,并安装. 删除安装目录下的BCUnrar.dll文件. 使用码: w4G-in5u3SH75RoB3VZIX8htiZgw4E ...

  10. 【题解】P4503 [CTSC2014]企鹅QQ(哈希)

    [题解]P4503 [CTSC2014]企鹅QQ(哈希) 考虑这样一种做法,将每个字符串的删去某个字符的新字符串的哈希值存下来,然后最后\(sort\)一遍双指针统计每个值相同的数的个数\(x\),这 ...