HDU5950 Recursive sequence (矩阵快速幂)
Recursive sequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3832 Accepted Submission(s): 1662
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
HintIn 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
2016ACM/ICPC亚洲区沈阳站-重现赛(感谢东北大学)
f(n)=f(n-1)+2f(n-2)+n^4
f(n) | 1 | 2 | 1 | 0 | 0 | 0 | 0 | f(n-1) |
f(n-1) | 1 | 0 | 0 | 0 | 0 | 0 | 0 | f(n-2) |
(n+1)^4 | 0 | 0 | 1 | 4 | 6 | 4 | 1 | n^4 |
(n+1)^3 | 0 | 0 | 0 | 1 | 3 | 3 | 1 | n^3 |
(n+1)^2 | 0 | 0 | 0 | 0 | 1 | 2 | 1 | n^2 |
(n+1) | 0 | 0 | 0 | 0 | 0 | 1 | 1 | n |
1 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 |
#include<iostream>
#include<string.h>
#include<algorithm>
#define inf 2147493647
#define ll long long
using namespace std;
struct mat{
ll t[7][7];
mat(){
memset(t,0,sizeof(t));
}
mat operator*(mat b){
mat c;
for(int i=0;i<7;i++)
for(int j=0;j<7;j++)
for(int k=0;k<7;k++)
c.t[i][j]=(c.t[i][j]%inf+t[i][k]*b.t[k][j])%inf;
return c;
}
};
mat pow(int nn,mat B,mat A)
{
while(nn){
if(nn%2==1)
B=A*B;
A=A*A;
nn/=2;
}
return B;
}
int main()
{
int T,n;
ll a[7][7]=
{1,2,1,0,0,0,0,
1,0,0,0,0,0,0,
0,0,1,4,6,4,1,
0,0,0,1,3,3,1,
0,0,0,0,1,2,1,
0,0,0,0,0,1,1,
0,0,0,0,0,0,1};
mat A;
for(int i=0;i<7;i++)
for(int j=0;j<7;j++)
A.t[i][j]=a[i][j];
mat B;
B.t[2][0]=81;
B.t[3][0]=27;
B.t[4][0]=9;
B.t[5][0]=3;
B.t[6][0]=1;
scanf("%d",&T);
while(T--)
{
scanf("%d%lld%lld",&n,&B.t[1][0],&B.t[0][0]);
if(n==1)
printf("%lld\n",B.t[1][0]);
else if(n==2)
printf("%lld\n",B.t[0][0]);
else{
mat C=pow(n-2,B,A);
printf("%lld\n",C.t[0][0]%inf);
}
}
return 0;
}
HDU5950 Recursive sequence (矩阵快速幂)的更多相关文章
- HDU5950 Recursive sequence —— 矩阵快速幂
题目链接:https://vjudge.net/problem/HDU-5950 Recursive sequence Time Limit: 2000/1000 MS (Java/Others) ...
- HDU5950 Recursive sequence (矩阵快速幂加速递推) (2016ACM/ICPC亚洲赛区沈阳站 Problem C)
题目链接:传送门 题目: Recursive sequence Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total ...
- 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数列,所以我们考虑是矩阵,然后我们进行推公式,因为这样我们是无 ...
- CF1106F Lunar New Year and a Recursive Sequence——矩阵快速幂&&bsgs
题意 设 $$f_i = \left\{\begin{matrix}1 , \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ i < k\\ ...
- hdu 5950 Recursive sequence 矩阵快速幂
Recursive sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- hdu-5667 Sequence(矩阵快速幂+费马小定理+快速幂)
题目链接: Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- UVA - 10689 Yet another Number Sequence 矩阵快速幂
Yet another Number Sequence Let’s define another number sequence, given by the foll ...
- Yet Another Number Sequence——[矩阵快速幂]
Description Everyone knows what the Fibonacci sequence is. This sequence can be defined by the recur ...
随机推荐
- Informatic 使用过程中的问题
1.database driver error ORA-12154 1)检查监听是否启动 2)是否在tnsnames.ora中配置
- Netty实现简单WebSocket服务器
本文参考<Netty权威指南>├── WebSocketServerHandler.java├── WebSocketServer.java└── wsclient.html packag ...
- Focal Loss
为了有效地同时解决样本类别不均衡和苦难样本的问题,何凯明和RGB以二分类交叉熵为例提出了一种新的Loss----Focal loss 原始的二分类交叉熵形式如下: Focal Loss形式如下: 上式 ...
- MySQL数据库的锁详解【转】
当然在我们的数据库中也有锁用来控制资源的并发访问,这也是数据库和文件系统的区别之一. 为什么要懂数据库锁? 通常来说对于一般的开发人员,在使用数据库的时候一般懂点 DQL(select),DML(in ...
- async_mongo_helper
# -*- coding: utf-8 -*- # @Time : 2019/1/7 2:11 PM # @Author : cxa # @File : motortesdt.py # @Softwa ...
- Git学习笔记06-版本回退
在实际中,向版本库提交多次后,几千行代码肯定不记得每次都改了什么,可以使用git log来查看提交日志.也就是git commit -m 后面填写的这部分内容 也可以使用git log --pre ...
- VC里判断系统是不是64bit
不过,理论上来说,也可以用一个int的大小作为参考,判断是32位还是64位.sizeof(int) == 4 //32位系统.sizeof(int) == 8 //64位系统. 也可以使用函数如下: ...
- C#委托delegate、Action、Func、predicate 对比用法
委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递.事件是一种特殊的委托. 一.委托的声明 (1) delegate delegate我们常用到的一种声明 Delega ...
- 10)django-ORM(创建,字段类型,字段参数)
一:ORM关系对象映射(Object Relational Mapping,简称ORM) ORM分两种: DB first 先在数据库中创建数据库表等 Code first 先创建类,然后根据类创建数 ...
- IntelliJ IDEA插件 - ApiDebugger
IntelliJ IDEA插件 - ApiDebuggerApiDebugger,是一个开源的接口调试IntelliJ IDEA插件,具有与IDEA一致的界面,无需切换程序即可完成网络API请求,让你 ...