Another kind of Fibonacci

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2353    Accepted Submission(s): 936

Problem Description
As
we all known , the Fibonacci series : F(0) = 1, F(1) = 1, F(N) = F(N -
1) + F(N - 2) (N >= 2).Now we define another kind of Fibonacci : A(0)
= 1 , A(1) = 1 , A(N) = X * A(N - 1) + Y * A(N - 2) (N >= 2).And we
want to Calculate S(N) , S(N) = A(0)2 +A(1)2+……+A(n)2.

 
Input
There are several test cases.
Each test case will contain three integers , N, X , Y .
N : 2<= N <= 231 – 1
X : 2<= X <= 231– 1
Y : 2<= Y <= 231 – 1
 
Output
For each test case , output the answer of S(n).If the answer is too big , divide it by 10007 and give me the reminder.
 
Sample Input
2 1 1
3 2 3
 
Sample Output
6
196
 思路:矩阵快速幂;
S(n) = ∑f(n)2 = S(n-1)+f(n)2 = S(n-1)+x2f(n-1)2+y2f(n-2)2+2xyf(n-1)f(n-2);
然后f(n)*f(n-1) = (x*f(n-1)+y*f(n-2))*f(n-1) = x*f(n-1)2+y*f(n-1)*f(n-2);
然后构造矩阵;
其中的第三个矩阵写错了,应该是s[n-1];     f[n-1]^2;     f[n-2]^2;  f[n-1]*f[n-2];
  1. 1 #include<stdio.h>
  2. 2 #include<algorithm>
  3. 3 #include<iostream>
  4. 4 #include<string.h>
  5. 5 #include<queue>
  6. 6 #include<set>
  7. 7 #include<math.h>
  8. 8 #include<map>
  9. 9 using namespace std;
  10. 10 typedef struct node
  11. 11 {
  12. 12 int m[4][4];
  13. 13 node()
  14. 14 {
  15. 15 memset(m,0,sizeof(m));
  16. 16 }
  17. 17 } maxtr;
  18. 18 void Init(maxtr *ans,int x,int y);
  19. 19 maxtr E();
  20. 20 maxtr quick_m(maxtr ak,int m);
  21. 21 const int mod = 10007;
  22. 22 int main(void)
  23. 23 {
  24. 24 int n,x,y;
  25. 25 while(scanf("%d %d %d",&n,&x,&y)!=EOF)
  26. 26 {
  27. 27 int f1 = 2;
  28. 28 int a1 = 1;
  29. 29 int a0 = 1;
  30. 30 int xx = 1;
  31. 31 maxtr ask ;
  32. 32 Init(&ask,x,y);
  33. 33 maxtr tp = quick_m(ask,n-1);
  34. 34 printf("%d\n",(tp.m[0][0]*2+tp.m[0][1]*a1+tp.m[0][2]*a0+tp.m[0][3]*xx)%mod);
  35. 35 }
  36. 36 return 0;
  37. 37 }
  38. 38 void Init(maxtr *ans,int x,int y)
  39. 39 { memset(ans->m,0,sizeof(ans->m));
  40. 40 x%=mod;y%=mod;
  41. 41 ans->m[0][0] = 1;
  42. 42 ans->m[0][1] = x*x%mod;
  43. 43 ans->m[0][2] = y*y%mod;
  44. 44 ans->m[0][3] =2*x*y%mod;
  45. 45 ans->m[1][1] = x*x%mod;
  46. 46 ans->m[1][2] = y*y%mod;
  47. 47 ans->m[1][3] = 2*x*y%mod;
  48. 48 ans->m[2][1] = 1;
  49. 49 ans->m[3][1] = x%mod;
  50. 50 ans->m[3][3] = y%mod;
  51. 51 }
  52. 52 maxtr E()
  53. 53 {
  54. 54 maxtr ak;
  55. 55 int i,j;
  56. 56 for(i = 0; i < 4; i++)
  57. 57 {
  58. 58 for(j = 0; j < 4; j++)
  59. 59 {
  60. 60 if(i == j)
  61. 61 ak.m[i][j] = 1;
  62. 62 }
  63. 63 }
  64. 64 return ak;
  65. 65 }
  66. 66 maxtr quick_m(maxtr ak,int m)
  67. 67 {
  68. 68 int i,j;
  69. 69 maxtr ac = E();
  70. 70 while(m)
  71. 71 {
  72. 72 if(m&1)
  73. 73 {
  74. 74 maxtr a;
  75. 75 for(i = 0; i < 4; i++)
  76. 76 for(j = 0; j < 4; j++)
  77. 77 for(int s= 0; s < 4; s++)
  78. 78 a.m[i][j] = (a.m[i][j] + ak.m[i][s]*ac.m[s][j]%mod)%mod;
  79. 79 ac = a;
  80. 80 }
  81. 81 maxtr b;
  82. 82 for(i = 0; i < 4; i++)
  83. 83 for(j = 0; j < 4; j++)
  84. 84 for(int s = 0; s < 4; s++)
  85. 85 b.m[i][j] = (b.m[i][j] + ak.m[i][s]*ak.m[s][j]%mod)%mod;
  86. 86 ak = b;
  87. 87 m>>=1;
  88. 88 }
  89. 89 return ac;
  90. 90 }

Another kind of Fibonacci(hdu3306)的更多相关文章

  1. hdu3306 Another kind of Fibonacci【矩阵快速幂】

    转载请注明出处:http://www.cnblogs.com/KirisameMarisa/p/4187670.html 题目链接:http://acm.hdu.edu.cn/showproblem. ...

  2. HDU3306 Another kind of Fibonacci 矩阵

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - HDU3306 题意概括 A0=1,A1=1,AN=X*AN-1+Y*AN-2(N>=2).求SN,SN ...

  3. HDU3306—Another kind of Fibonacci

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3306 题目意思:一个斐波那契数列的变式,本来是A[n]=A[n-1]+A[n-2],现在变成A[n]= ...

  4. hdu3306:Another kind of Fibonacci

    A(0)=A(1)=1,A(i)=X*A(i-1)+Y*A(i-2),求S(n)=A(0)^2+A(1)^2+A(2)^2+A(3)^2+……+A(n)^2. 这个矩阵有点毒.. #include&l ...

  5. HDU3306 Another kind of Fibonacci

    本篇题解用于作者本人对于矩阵乘法的印象加深,也欢迎大家的阅读. 题目大意 众所周知,斐波那契数列为 \(f(0)=1\) , \(f(1)=1\) ,\(f(n)=f(n-1)+f(n-2)~(n&g ...

  6. 算法与数据结构(九) 查找表的顺序查找、折半查找、插值查找以及Fibonacci查找

    今天这篇博客就聊聊几种常见的查找算法,当然本篇博客只是涉及了部分查找算法,接下来的几篇博客中都将会介绍关于查找的相关内容.本篇博客主要介绍查找表的顺序查找.折半查找.插值查找以及Fibonacci查找 ...

  7. #26 fibonacci seqs

    Difficulty: Easy Topic: Fibonacci seqs Write a function which returns the first X fibonacci numbers. ...

  8. 关于java的递归写法,经典的Fibonacci数的问题

    经典的Fibonacci数的问题 主要想展示一下迭代与递归,以及尾递归的三种写法,以及他们各自的时间性能. public class Fibonacci { /*迭代*/ public static ...

  9. 斐波拉契数列(Fibonacci) 的python实现方式

    第一种:利用for循环 利用for循环时,不涉及到函数,但是这种方法对我种小小白来说比较好理解,一涉及到函数就比较抽象了... >>> fibs = [0,1] >>&g ...

随机推荐

  1. Redis源码解析(1)

    在文章的开头我们把所有服务端文件列出来,并且标示出其作用: adlist.c //双向链表 ae.c //事件驱动 ae_epoll.c //epoll接口, linux用 ae_kqueue.c / ...

  2. A Child's History of England.9

    But, first, as it was important to know how numerous those pestilent Danes were, and how they were f ...

  3. day10 负载均衡

    day10 负载均衡 负载均衡反向代理 正向代理:即是客户端代理, 代理客户端, 服务端不知道实际发起请求的客户端. # (内部上网) 客户端 <-> 代理 -> 服务端 反向代理即 ...

  4. Linux 内存泄漏 valgrind

    Valgrind 是个开源的工具,功能很多.例如检查内存泄漏工具---memcheck. Valgrind 安装: 去官网下载: http://valgrind.org/downloads/curre ...

  5. 零基础学习java------31---------共享单车案例,html快速入门(常见标签,get和post的区别)

     一 .单车案例 二. HTML快速入门 红字表示要掌握的内容 超文本标记语言,此处的标记指的即是关键字,其用处是用来写页面(展示数据). 语法:(1)./当前目录:../ 父级目录 (2)注释符号: ...

  6. 零基础学习java------day5------do....while循环、嵌套、方法(函数)

    1  do...while循环 格式 初始化语句; do { 循环体语句; 控制条件语句; }while(判断条件语句); 流程: 先执行初始化语句 再执行循环体语句 再执行条件控制语句 再做条件的判 ...

  7. set、multiset深度探索

    set/multiset的底层是rb_tree,因此它有自动排序特性.set中的元素不允许重复必须独一无二,key与value值相同,multiset中的元素允许重复. set的模板参数key即为关键 ...

  8. Linux基础命令---alias别名

    alias Alias不带参数或使用-p选项在标准输出上以"name=value"的形式打印别名列表.当提供参数时,为其值给定的每个名称定义一个别名.值中的尾随空格将导致在扩展别名 ...

  9. mybatis-插件开发

    在Executor.StatementHandler.parameterHandler.resultSetHandler创建的时候都有一步这样的操作xxxHandler=interceptorChai ...

  10. 【编程思想】【设计模式】【创建模式creational】抽象工厂模式abstract_factory

    Python版 https://github.com/faif/python-patterns/blob/master/creational/abstract_factory.py #!/usr/bi ...