Another kind of Fibonacci

题目链接

Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)

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 <= 2^31 – 1

X : 2<= X <= 2^31 – 1

Y : 2<= Y <= 2^31 – 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

思路见下图:

  1. #include<iostream>
  2. #include<cstring>
  3. using namespace std;
  4. const int maxn = 5;
  5. typedef long long LL;
  6. struct Matrix{
  7. int matrix[maxn][maxn];
  8. }ori, ans;
  9. int n = 4, N, X, Y, m = 10007;
  10. void init()
  11. {
  12. for(int i=0;i<n;i++)
  13. for(int j=0;j<n;j++)
  14. ori.matrix[i][j] = ans.matrix[i][j] = 0;
  15. ori.matrix[0][0] = ori.matrix[2][1] = 1;
  16. ori.matrix[0][1] = ori.matrix[1][1] = X * X % m;
  17. ori.matrix[0][2] = ori.matrix[1][2] = Y * Y % m;
  18. ori.matrix[0][3] = ori.matrix[1][3] = ((2 * X) % m) * Y % m;
  19. ori.matrix[3][1] = X;
  20. ori.matrix[3][3] = Y;
  21. ans.matrix[0][0] = 2;
  22. ans.matrix[1][0] = ans.matrix[2][0] = ans.matrix[3][0] = 1;
  23. }
  24. Matrix multiply(Matrix a, Matrix b)
  25. {
  26. Matrix temp;
  27. memset(temp.matrix, 0, sizeof(temp.matrix));
  28. for(int i=0;i<n;i++)
  29. for(int j=0;j<n;j++)
  30. for(int k=0;k<n;k++)
  31. temp.matrix[i][j] = (temp.matrix[i][j] + ((a.matrix[i][k] * b.matrix[k][j]) % m)) % m;
  32. return temp;
  33. }
  34. //矩阵的b次幂
  35. Matrix binaryPow(int b)
  36. {
  37. Matrix temp;
  38. memset(temp.matrix, 0, sizeof(temp.matrix));
  39. for(int i=0;i<n;i++)
  40. temp.matrix[i][i] = 1;
  41. while(b > 0)
  42. {
  43. if(b & 1)
  44. temp = multiply(ori, temp);
  45. ori = multiply(ori, ori);
  46. b >>= 1;
  47. }
  48. return temp;
  49. }
  50. int main()
  51. {
  52. //freopen("in.txt","r", stdin);
  53. while(cin>>N>>X>>Y)
  54. {
  55. X %= m;
  56. Y %= m;
  57. init();
  58. Matrix temp = binaryPow(N - 1);
  59. ans = multiply(temp, ans);
  60. cout<<ans.matrix[0][0]<<endl;
  61. }
  62. return 0;
  63. }

杭电oj3306:Another kind of 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. HDU 1588 Gauss Fibonacci(矩阵快速幂)

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

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

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

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

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

  6. poj3070 Fibonacci 矩阵快速幂

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

  7. $loj$10222 佳佳的$Fibonacci$ 矩阵快速幂

    正解:矩阵快速幂 解题报告: 我永远喜欢loj! 一看到这个就应该能想到矩阵快速幂? 然后就考虑转移式,发现好像直接想不好想,,,主要的问题在于这个*$i$,就很不好搞$QAQ$ 其实不难想到,$\s ...

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

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

  9. POJ3070:Fibonacci(矩阵快速幂模板题)

    http://poj.org/problem?id=3070 #include <iostream> #include <string.h> #include <stdl ...

  10. hdu 3306 Another kind of Fibonacci 矩阵快速幂

    参考了某大佬的 我们可以根据(s[n-2], a[n-1]^2, a[n-1]*a[n-2], a[n-2]^2) * A = (s[n-1], a[n]^2, a[n]*a[n-1], a[n-1] ...

随机推荐

  1. 使用forin循环时的注意事项

    由于遍历的对象为nil,从而导致的现象是里面的循环体根本就没有执行,并且编译器和运行期都不会报错.因为,OC语法是运行向nil发送消息的. for (WSFActivitySelectSpaceCel ...

  2. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 按钮:原始按钮样式(未被操作)

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  3. java第二次课件课后动手动脑习题整理总结(2019年9月23号)

    一.动手动脑 1 1.题目 2.程序源代码 package yang8; import java.util.Scanner; import java.util.Random; public class ...

  4. HttpServletRequest 或 HttpServletResponse显示红色,需引用的依赖包:servlet-api.jar

    解决方法:

  5. Day3-J-4 Values whose Sum is 0 POJ2785

    The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, compute ...

  6. css限制文字显示字数长度,超出部分自动用省略号显示,防止溢出到第二行

    为了保证页面的整洁美观,在很多的时候,我们常需要隐藏超出长度的文字.这在列表条目,题目,名称等地方常用到. 效果如下: 未限制显示长度,如果超出了会溢出到第二行里.严重影响用户体验和显示效果. 我们在 ...

  7. PHP获取远程图片

    <?php // // Function: 获取远程图片并把它保存到本地 // // // 确定您有把文件写入本地服务器的权限 // // // 变量说明: // $url 是远程图片的完整UR ...

  8. Tomcat+JSP经典配置实例

    经常看到jsp的初学者问tomcat下如何配置jsp.servlet和bean的问题,于是总结了一下如何tomcat下配置jsp.servlet和ben,希望对那些初学者有所帮助. 一.开发环境配置 ...

  9. 使用Spring JMS轻松实现异步消息传递

    异步进程通信是面向服务架构(SOA)一个重要的组成部分,因为企业里很多系统通信,特别是与外部组织间的通信,实质上都是异步的.Java消息服务(JMS)是用于编写使用异步消息传递的JEE应用程序的API ...

  10. MVC5仓库管理系统

    下载