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 ...
随机推荐
- Android SpannableString实现TextView的点击事件
最近项目中遇到一个问题,就是一段文字中股票可点击并跳到股票详情,只记得SpannableString可以实现富文本功能,但并不知道可实现的富文本有点击功能,就开始借助万能搜索引擎,结果不出意料,的确有 ...
- MFCWinInet学习
http://blog.csdn.net/segen_jaa/article/details/6278167 背景: 功能:服务端下载文件 服务端:用Java写Sevlet进行有效性验证 客户端:用C ...
- Linux中OCI开发库的配置
Oracle调用接口(Oracle Call Interface,简称OCI)提供了一组可对Oracle数据库进行存取的接口子例程(函数),通过在第三代程序设计语言(如C语言)中进行调用可达到存取Or ...
- strstr()函数的使用
strstr(str1,str2) 函数用于判断字符串str2是否是str1的子串.如果是,则该函数返回str2在str1中首次出现的地址:否则,返回NULL. 实例: /** *Descriptio ...
- CodeForces 931C Laboratory Work 水题,构造
*这种题好像不用写题解... 题意: 一个人要改动别人的实验记录,实验记录记录是一个集合 实验记录本身满足:$max(X)-min(X)<=2$ 改动结果要求: 1.新的集合平均值和之前的一样 ...
- $Django 路飞学城项目简介
- 基于极验实现动态验证码 - 在线视频播放:cc,HTML用的Flash - 基于Rest Framework实现 API接口 - 自定义rest认证token 认证 - 序列化以及自定义验证对请求 ...
- [C]*和&
一 .& c的&被称为“寻址运算符”,作用是指向某变量的指针: 请看以下代码: int main(void){ int int_1 = 16; printf(" ...
- 新建项目虚拟环境及pycharm配置
基本操作 查询已有的虚拟环境 workon 激活虚拟环境 workon 虚拟环境名 退出虚拟环境 deactivate 删除虚拟环境 rmvirtualenv 虚拟环境名 查看python版本检查 p ...
- Spring4-@Enable** 注解的实现原理
背景 在前面的工作中使用SpringBoot的时候,我碰到了很多的使用@Enable***注解的地方,使用上也都是加在@Configuration 类注解的类上面,比如: (1)@EnableAuto ...
- Go语言环境安装&搭建(Linux)
Linux的东西果然不记不行啊~ 下载&安装 下载 我们先找到linux版的下载链接 https://golang.org/dl/ 打开网址找到Linux对应的链接右键复制下载地址 然后连接服 ...