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 ...
随机推荐
- Photoshop CS6打开图片后看不到怎么办
https://jingyan.baidu.com/album/54b6b9c0d45c632d583b4793.html
- string.Empty, "" 和 null 三者的区别
转载:http://www.cnblogs.com/mxxblog/archive/2013/08/22/3275387.html 这是一个及其常见的问题,网上已经有关于这个问题的很多讨论.但是我觉得 ...
- telegram即时通信软件和outline ---- by 余弦 by倾旋
第一次遇到它 是在余弦的圈子里发现的tele推送,他当时关键部分说的极其少,就三个字,你懂的..看完以后,我想哭.我真的不懂.. 我是如何突破这个术语的 近期,随着信息源的增多.(收集了大量的可靠公众 ...
- js中 && 和 || 的用法
js中的&& 和 || 一直以为是php那一套,上网查了一些资料,才发现不一样 a() && b() :如果执行a()后返回true,则执行b()并返回b的值:如果执行 ...
- workqueue --最清晰的讲解
带你入门: 1.INIT_WORK(struct work_struct *work, void (*function)(void *), void *data) 上面一句只是定义了work和work ...
- input[type=file]上传文件(格式判断、文件大小、上传成功后操作)
var isUploadImg = false; //在input file内容改变的时候触发事件******************上传图片 $('#filed').change(function( ...
- c++学习day3(字符串_指针)
1.字符串 1)三种形式 用双引号括起来的字符串常量:结尾会有一个'\0'字符,但该字符只占据字节数,不会使字符串长度增加. 存放于字符数组中,以'\0'字符结尾:数组元素个数应至少为字符串长度+1 ...
- RS485 通信接收时丢失0x11等数据
RS485通信接收方,丢弃掉了值为 0x11 的数据. 怀疑 0x11 被转义,没有按照原始数据接收, 查看ASCII码对应表 0x11 代表 “本文结束”, 因此丢弃是有可能的. 要想接收原始数据而 ...
- 使用Fiddler进行手机端抓包
1.手机和电脑在同一局域网 2.在电脑上查看ip地址 3.如果是需要抓取https,则需要在浏览器中输入http://xxx.xxx.x.xxx:8888(第二部查到的ip地址 + Fiddler 的 ...
- linux备忘簿
1.ubuntu中按ctrl+s锁定屏幕,按ctrl+q解锁. 2.vim中撤销和恢复为u和ctlr+r 3.静态库和动态库编译命令: (1)得到hello.o g++ -c hello.cpp (2 ...