Recursive sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2882    Accepted Submission(s): 1284

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.

 
题意     f[n]=f[n-1]+2*f[n-2]+n^4; f[1]=a f[2]=b   求第n项
解析     直接递推肯定会超时的   所以 构造一个7*7系数矩阵 直接快速幂解出来  由于现在比较菜 只会最简单的矩阵 勉强可以写出来。。。。
 
1 2 1 0 0 0 0         f[i-1]        f[i]
1 0 0 0 0 0 0         f[i-2]        f[i-1]  
0 0 1 4 6 4 1    i^4          (i+1)^4
0 0 0 1 3 3 1       *    i^3                  =            (i+1)^3
0 0 0 0 1 2 1    i^2          (i+1)^2
0 0 0 0 0 1 1    i            i+1
0 0 0 0 0 0 1      1           1  
 
AC代码
 #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=,maxn=;
struct Matrix
{
ll m[maxn][maxn];
Matrix()
{
memset(m,,sizeof(m));
}
void init()
{
for(int i=; i<maxn; i++)
for(int j=; j<maxn; j++)
m[i][j]=(i==j);
}
Matrix operator +(const Matrix &b)const
{
Matrix c;
for(int i=; i<maxn; i++)
{
for(int j=; j<maxn; j++)
{
c.m[i][j]=(m[i][j]+b.m[i][j])%mod;
}
}
return c;
}
Matrix operator *(const Matrix &b)const
{
Matrix c;
for(int i=; i<maxn; i++)
{
for(int j=; j<maxn; j++)
{
for(int k=; k<maxn; k++)
{
c.m[i][j]=(c.m[i][j]+(m[i][k]*b.m[k][j])%mod)%mod;
}
}
}
return c;
}
Matrix operator^(const ll &t)const
{
Matrix ans,a=(*this);
ans.init();
ll n=t;
while(n)
{
if(n&) ans=ans*a;
a=a*a;
n>>=;
}
return ans;
}
};
int main()
{
int t;
ll n,m,a,b;
scanf("%d",&t);
while(t--)
{
scanf("%lld %lld %lld",&n,&a,&b);
if(n==)
{
cout<<a%mod<<endl;
continue;
}
if(n==)
{
cout<<b%mod<<endl;
continue;
}
Matrix temp;
temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=;temp.m[][]=;
temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=;temp.m[][]=;
temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=;temp.m[][]=;
temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=;temp.m[][]=;
temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=;temp.m[][]=;
temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=;temp.m[][]=;
temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=;temp.m[][]=;
Matrix aa,bb;
bb.m[][]=b%mod;
bb.m[][]=a%mod;
bb.m[][]=;
bb.m[][]=;
bb.m[][]=;
bb.m[][]=;
bb.m[][]=;
aa=temp^(n-);
aa=aa*bb;
cout<<aa.m[][]%mod<<endl;
}
return ;
}
    

HDU 5950 Recursive sequence 递推转矩阵的更多相关文章

  1. hdu 5950 Recursive sequence 递推式 矩阵快速幂

    题目链接 题意 给定\(c_0,c_1,求c_n(c_0,c_1,n\lt 2^{31})\),递推公式为 \[c_i=c_{i-1}+2c_{i-2}+i^4\] 思路 参考 将递推式改写\[\be ...

  2. HDU 5860 Death Sequence(递推)

    HDU 5860 Death Sequence(递推) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5860 Description You ...

  3. HDU 5950 Recursive sequence 【递推+矩阵快速幂】 (2016ACM/ICPC亚洲区沈阳站)

    Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  4. 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 ...

  5. HDU 5950 Recursive sequence(矩阵快速幂)

    题目链接:Recursive sequence 题意:给出前两项和递推式,求第n项的值. 题解:递推式为:$F[i]=F[i-1]+2*f[i-2]+i^4$ 主要问题是$i^4$处理,容易想到用矩阵 ...

  6. HDU - 5950 Recursive sequence(二项式+矩阵合并+矩阵快速幂)

    Recursive sequence Farmer John likes to play mathematics games with his N cows. Recently, they are a ...

  7. hdu 5950 Recursive sequence 矩阵快速幂

    Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  8. hdu 5860 Death Sequence(递推+脑洞)

    Problem Description You may heard of the Joseph Problem, the story comes from a Jewish historian liv ...

  9. HDU 5950 Recursive sequence(矩阵快速幂)题解

    思路:一开始不会n^4的推导,原来是要找n和n-1的关系,这道题的MOD是long long 的,矩阵具体如下所示 最近自己总是很坑啊,代码都瞎吉坝写,一个long long的输入写成%d一直判我TL ...

随机推荐

  1. 调用wsdl接口,参数是xml格式

    1.最近太累了,好困.闲话少许直奔主题吧.上代码 try{ String wsurl = "http://172.16.16.236:9999/xxx/ws/WSService?wsdl&q ...

  2. 死磕 java集合之终结篇

    概览 我们先来看一看java中所有集合的类关系图. 这里面的类太多了,请放大看,如果放大还看不清,请再放大看,如果还是看不清,请放弃. 我们下面主要分成五个部分来逐个击破. List List中的元素 ...

  3. Linux 之 2>&1

    我们在Linux下经常会碰到nohup command>/dev/null 2>&1 &这样形式的命令.首先我们把这条命令大概分解下首先就是一个nohup表示当前用户和系统 ...

  4. 学习笔记 第九章 使用CSS美化表格

    第9章  使用CSS美化表格 学习重点 正确使用表格标签: 设置表格和单元格属性: 设计表格的CSS样式. 9.1 表格的基本结构 表格由行.列.单元格3部分组成,单元格时行与列交叉的部分. 在HTM ...

  5. iOS游戏开发之UIDynamic

    iOS游戏开发之UIDynamic 简介 什么是UIDynamic UIDynamic是从iOS 7开始引入的一种新技术,隶属于UIKit框架 可以认为是一种物理引擎,能模拟和仿真现实生活中的物理现象 ...

  6. select 1浅析

    今天看到项目代码里有这条语句,不懂select 1 from XXXXXXX里的1是何意,查了一番才知道: 1.select 1 from mytable;与select anycol(目的表集合中的 ...

  7. java内存查看与分析

    业界有很多强大的java profile的工具,比如Jporfiler,yourkit,这些收费的东西我就不想说了,想说的是,其实java自己就提供了很多内存监控的小工具,下面列举的工具只是一小部分, ...

  8. druid数据库连接池整合到SpringMvc

    1.maven项目加入相关的依赖 <dependency> <groupId>com.alibaba</groupId> <artifactId>dru ...

  9. updating error reports database解决方案

    Window--->Preferences--->General--->Startup and Shutdown--->取消勾选Eclipse Automated Error  ...

  10. Android原生方式获取经纬度

    两种定位方式:GPS定位.WiFi定位优劣: 如果项目定位要求较高还是建议使用三方地图库 GPS定位相比Wifi定位更精准且可在无网络情况下使用,但在室内基本暴毙无法使用WiFi定位没有室内外限制也不 ...