Recursive sequence

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

Problem Description
Farmer
John likes to play mathematics games with his N cows. Recently, they
are attracted by recursive sequences. In each turn, the cows would stand
in a line, while John writes two positive numbers a and b on a
blackboard. And then, the cows would say their identity number one by
one. The first cow says the first number a and the second says the
second number b. After that, the i-th cow says the sum of twice the
(i-2)-th number, the (i-1)-th number, and i4. Now, you need to write a program to calculate the number of the N-th cow in order to check if John’s cows can make it right.
 
Input
The first line of input contains an integer t, the number of test cases. t test cases follow.
Each case contains only one line with three numbers N, a and b where N,a,b < 231 as described above.
 
Output
For
each test case, output the number of the N-th cow. This number might be
very large, so you need to output it modulo 2147493647.
 
Sample Input
2
3 1 2
4 1 10
 
Sample Output
85
369

Hint

In the first case, the third number is 85 = 2*1十2十3^4.
In the second case, the third number is 93 = 2*1十1*10十3^4 and the fourth number is 369 = 2 * 10 十 93 十 4^4.

 
Source
  套板子,取模开ll就好了;
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define pi (4*atan(1.0))
  5. #define eps 1e-14
  6. const int N=2e5+,M=1e6+,inf=1e9+;
  7. const ll INF=1e18+,MOD=;
  8. struct Matrix
  9. {
  10. ll a[][];
  11. Matrix()
  12. {
  13. memset(a,,sizeof(a));
  14. }
  15. void init()
  16. {
  17. for(int i=;i<;i++)
  18. for(int j=;j<;j++)
  19. a[i][j]=(i==j);
  20. }
  21. Matrix operator + (const Matrix &B)const
  22. {
  23. Matrix C;
  24. for(int i=;i<;i++)
  25. for(int j=;j<;j++)
  26. C.a[i][j]=(a[i][j]+B.a[i][j])%MOD;
  27. return C;
  28. }
  29. Matrix operator * (const Matrix &B)const
  30. {
  31. Matrix C;
  32. for(int i=;i<;i++)
  33. for(int k=;k<;k++)
  34. for(int j=;j<;j++)
  35. C.a[i][j]=(C.a[i][j]+(a[i][k]*B.a[k][j])%MOD)%MOD;
  36. return C;
  37. }
  38. Matrix operator ^ (const ll &t)const
  39. {
  40. Matrix A=(*this),res;
  41. res.init();
  42. int p=t;
  43. while(p)
  44. {
  45. if(p&)res=res*A;
  46. A=A*A;
  47. p>>=;
  48. }
  49. return res;
  50. }
  51. };
  52. Matrix base,hh;
  53. void init()
  54. {
  55. base.a[][]=;
  56. base.a[][]=;
  57. base.a[][]=;
  58. base.a[][]=;
  59. base.a[][]=;
  60. base.a[][]=;
  61. base.a[][]=;
  62. base.a[][]=;
  63. base.a[][]=;
  64. base.a[][]=;
  65. base.a[][]=;
  66. base.a[][]=;
  67. base.a[][]=;
  68. base.a[][]=;
  69. base.a[][]=;
  70. base.a[][]=;
  71. base.a[][]=;
  72. base.a[][]=;
  73. base.a[][]=;
  74. }
  75. void init1(ll a,ll b)
  76. {
  77. memset(hh.a,,sizeof(hh.a));
  78. hh.a[][]=b%MOD;
  79. hh.a[][]=a%MOD;
  80. hh.a[][]=***;
  81. hh.a[][]=**;
  82. hh.a[][]=*;
  83. hh.a[][]=;
  84. hh.a[][]=;
  85. }
  86. int main()
  87. {
  88. init();
  89. int T,cas=;
  90. scanf("%d",&T);
  91. while(T--)
  92. {
  93. ll n,a,b;
  94. scanf("%lld%lld%lld",&n,&a,&b);
  95. init1(a,b);
  96. Matrix ans=(base^(n-));
  97. hh=hh*ans;
  98. printf("%lld\n",hh.a[][]);
  99. }
  100. return ;
  101. }

hdu 5950 Recursive sequence 矩阵快速幂的更多相关文章

  1. HDU 5950 - Recursive sequence - [矩阵快速幂加速递推][2016ACM/ICPC亚洲区沈阳站 Problem C]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 Farmer John likes to play mathematics games with ...

  2. 5950 Recursive sequence (矩阵快速幂)

    题意:递推公式 Fn = Fn-1 + 2 * Fn-2 + n*n,让求 Fn; 析:很明显的矩阵快速幂,因为这个很像Fibonacci数列,所以我们考虑是矩阵,然后我们进行推公式,因为这样我们是无 ...

  3. Recursive sequence HDU - 5950 (递推 矩阵快速幂优化)

    题目链接 F[1] = a, F[2] = b, F[i] = 2 * F[i-2] + F[i-1] + i ^ 4, (i >= 3) 现在要求F[N] 类似于斐波那契数列的递推式子吧, 但 ...

  4. HDU5950 Recursive sequence (矩阵快速幂加速递推) (2016ACM/ICPC亚洲赛区沈阳站 Problem C)

    题目链接:传送门 题目: Recursive sequence Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total ...

  5. HDU5950 Recursive sequence —— 矩阵快速幂

    题目链接:https://vjudge.net/problem/HDU-5950 Recursive sequence Time Limit: 2000/1000 MS (Java/Others)   ...

  6. HDU - 1005 Number Sequence 矩阵快速幂

    HDU - 1005 Number Sequence Problem Description A number sequence is defined as follows:f(1) = 1, f(2 ...

  7. HDU 1005 Number Sequence(矩阵快速幂,快速幂模板)

    Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1 ...

  8. HDU - 1005 -Number Sequence(矩阵快速幂系数变式)

    A number sequence is defined as follows:  f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) m ...

  9. CF1106F Lunar New Year and a Recursive Sequence——矩阵快速幂&&bsgs

    题意 设 $$f_i = \left\{\begin{matrix}1 , \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \  i < k\\ ...

随机推荐

  1. 161125、Java网络编程之统一资源定位符URL

    统一资源定位符URL(Uniform Resource Locator)是www客户机访问Internet时用来标识资源的名字和地址.超文本链路由统一资源定位符URL维持.URL的格式是: <M ...

  2. jdk8飞行记录器配置

    jdk8提供了jmc工具,应该比visualvm厉害吧 下面贴一份tomcat的配置,自己留个备份,把下面的内容粘贴到tomcat setenv.sh就可以了 nowday=`date +%Y%m%d ...

  3. [转]如何:在设备上安装 SQL Server Compact 3.5

    将设备连接到计算机,或者将仿真程序插入底座. 有关更多信息,请参见如何:将设备仿真程序插入底座和移除底座. 说明: 计算机上必须已安装了 Windows Mobile Device Center 或 ...

  4. crontab 日志备份定时任务

    -l选项,查看当前用户的所有定时任务: [xiluhua@vm-xiluhua][/home]$ crontab -l * * * * * /home/xiluhua/shell_script/log ...

  5. Keepalived原理及配置应用总结

    Keepalived——保持存活,在网络里的含义就是保持在线.Keepalived提供高可用和热备的功能,用来防止单点故障的发生. 1.VRRP协议基本原理介绍 Keepalived实现的基础是VRR ...

  6. 关于ADO.NET@SQL Server&SqlDataReader

    先说基础的,说基础的明白了再深的也是一样的.SQL是关系型数据库,所以就决定了对其操作的时候ADO的一些类要相互联系,Connection 类Command对象(ExecuteReader()方法.E ...

  7. mysql 段错误 (core dumped)

    一直使用好好的mysql命令,突然今天抽风,无论使用任何mysql选项都报“段错误 (core dumped)”,以为是mysqld程序出问题了,所以我尝试重启,因为我的环境上是多实例,用了mysql ...

  8. RecycleView使用的那些坑

    1.为条目设置margin值时,在6.0系统上会无效.此时在item的根外面套一层viewgroup解决. 2.当条目中有imageview时,必须给imageview设置 src或者backgrou ...

  9. CI框架分页类

    分页类1.分页类参数说明 'base_url' => 指向你的分页所在的控制器类/方法的完整的 URL, 'total_rows' => 数据的总行数, 'per_page' => ...

  10. C#委托之泛型

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...