hdu 5950 Recursive sequence 矩阵快速幂
Recursive sequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
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.
Each case contains only one line with three numbers N, a and b where N,a,b < 231 as described above.
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.
3 1 2
4 1 10
369
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.
- #include<bits/stdc++.h>
- using namespace std;
- #define ll long long
- #define pi (4*atan(1.0))
- #define eps 1e-14
- const int N=2e5+,M=1e6+,inf=1e9+;
- const ll INF=1e18+,MOD=;
- struct Matrix
- {
- ll a[][];
- Matrix()
- {
- memset(a,,sizeof(a));
- }
- void init()
- {
- for(int i=;i<;i++)
- for(int j=;j<;j++)
- a[i][j]=(i==j);
- }
- Matrix operator + (const Matrix &B)const
- {
- Matrix C;
- for(int i=;i<;i++)
- for(int j=;j<;j++)
- C.a[i][j]=(a[i][j]+B.a[i][j])%MOD;
- return C;
- }
- Matrix operator * (const Matrix &B)const
- {
- Matrix C;
- for(int i=;i<;i++)
- for(int k=;k<;k++)
- for(int j=;j<;j++)
- C.a[i][j]=(C.a[i][j]+(a[i][k]*B.a[k][j])%MOD)%MOD;
- return C;
- }
- Matrix operator ^ (const ll &t)const
- {
- Matrix A=(*this),res;
- res.init();
- int p=t;
- while(p)
- {
- if(p&)res=res*A;
- A=A*A;
- p>>=;
- }
- return res;
- }
- };
- Matrix base,hh;
- void init()
- {
- base.a[][]=;
- base.a[][]=;
- base.a[][]=;
- base.a[][]=;
- base.a[][]=;
- base.a[][]=;
- base.a[][]=;
- base.a[][]=;
- base.a[][]=;
- base.a[][]=;
- base.a[][]=;
- base.a[][]=;
- base.a[][]=;
- base.a[][]=;
- base.a[][]=;
- base.a[][]=;
- base.a[][]=;
- base.a[][]=;
- base.a[][]=;
- }
- void init1(ll a,ll b)
- {
- memset(hh.a,,sizeof(hh.a));
- hh.a[][]=b%MOD;
- hh.a[][]=a%MOD;
- hh.a[][]=***;
- hh.a[][]=**;
- hh.a[][]=*;
- hh.a[][]=;
- hh.a[][]=;
- }
- int main()
- {
- init();
- int T,cas=;
- scanf("%d",&T);
- while(T--)
- {
- ll n,a,b;
- scanf("%lld%lld%lld",&n,&a,&b);
- init1(a,b);
- Matrix ans=(base^(n-));
- hh=hh*ans;
- printf("%lld\n",hh.a[][]);
- }
- return ;
- }
hdu 5950 Recursive sequence 矩阵快速幂的更多相关文章
- 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 ...
- 5950 Recursive sequence (矩阵快速幂)
题意:递推公式 Fn = Fn-1 + 2 * Fn-2 + n*n,让求 Fn; 析:很明显的矩阵快速幂,因为这个很像Fibonacci数列,所以我们考虑是矩阵,然后我们进行推公式,因为这样我们是无 ...
- 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] 类似于斐波那契数列的递推式子吧, 但 ...
- HDU5950 Recursive sequence (矩阵快速幂加速递推) (2016ACM/ICPC亚洲赛区沈阳站 Problem C)
题目链接:传送门 题目: Recursive sequence Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total ...
- HDU5950 Recursive sequence —— 矩阵快速幂
题目链接:https://vjudge.net/problem/HDU-5950 Recursive sequence Time Limit: 2000/1000 MS (Java/Others) ...
- HDU - 1005 Number Sequence 矩阵快速幂
HDU - 1005 Number Sequence Problem Description A number sequence is defined as follows:f(1) = 1, f(2 ...
- 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 ...
- 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 ...
- CF1106F Lunar New Year and a Recursive Sequence——矩阵快速幂&&bsgs
题意 设 $$f_i = \left\{\begin{matrix}1 , \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ i < k\\ ...
随机推荐
- 161125、Java网络编程之统一资源定位符URL
统一资源定位符URL(Uniform Resource Locator)是www客户机访问Internet时用来标识资源的名字和地址.超文本链路由统一资源定位符URL维持.URL的格式是: <M ...
- jdk8飞行记录器配置
jdk8提供了jmc工具,应该比visualvm厉害吧 下面贴一份tomcat的配置,自己留个备份,把下面的内容粘贴到tomcat setenv.sh就可以了 nowday=`date +%Y%m%d ...
- [转]如何:在设备上安装 SQL Server Compact 3.5
将设备连接到计算机,或者将仿真程序插入底座. 有关更多信息,请参见如何:将设备仿真程序插入底座和移除底座. 说明: 计算机上必须已安装了 Windows Mobile Device Center 或 ...
- crontab 日志备份定时任务
-l选项,查看当前用户的所有定时任务: [xiluhua@vm-xiluhua][/home]$ crontab -l * * * * * /home/xiluhua/shell_script/log ...
- Keepalived原理及配置应用总结
Keepalived——保持存活,在网络里的含义就是保持在线.Keepalived提供高可用和热备的功能,用来防止单点故障的发生. 1.VRRP协议基本原理介绍 Keepalived实现的基础是VRR ...
- 关于ADO.NET@SQL Server&SqlDataReader
先说基础的,说基础的明白了再深的也是一样的.SQL是关系型数据库,所以就决定了对其操作的时候ADO的一些类要相互联系,Connection 类Command对象(ExecuteReader()方法.E ...
- mysql 段错误 (core dumped)
一直使用好好的mysql命令,突然今天抽风,无论使用任何mysql选项都报“段错误 (core dumped)”,以为是mysqld程序出问题了,所以我尝试重启,因为我的环境上是多实例,用了mysql ...
- RecycleView使用的那些坑
1.为条目设置margin值时,在6.0系统上会无效.此时在item的根外面套一层viewgroup解决. 2.当条目中有imageview时,必须给imageview设置 src或者backgrou ...
- CI框架分页类
分页类1.分页类参数说明 'base_url' => 指向你的分页所在的控制器类/方法的完整的 URL, 'total_rows' => 数据的总行数, 'per_page' => ...
- C#委托之泛型
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...