HDU5950 Recursive sequence 非线性递推式 矩阵快速幂
题目描述:给出一个数列的第一项和第二项,计算第n项。
递推式是 f(n)=f(n-1)+2*f(n-2)+n^4.
由于n很大,所以肯定是矩阵快速幂的题目,但是矩阵快速幂只能解决线性的问题,n^4在这个式子中是非线性的,后一项和前一项没有什么直接关系,所以模拟赛的时候想破头也不会做。
这里要做一个转换,把n^4变成一个线性的,也就是和(n-1)^4有关系的东西,而这个办法就是:
n^4=(n-1+1)^4=(n-1)^4+4*(n-1)^3+6*(n-1)^2+4*(n-1)^1+(n-1)^0;
这个转换就建立了某一项和前一项的关系,矩阵的F数组就是 f[7]={ b , a , 81 , 27 , 9 , 3 , 1 };,整体的矩阵也很好构造,代码里有。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<string.h>
#include<sstream>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<bitset>
#define CLR(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
ll mod=2147493647;
inline int rd(void) {
int x=0; int f=1;char s=getchar();
while(s<'0'||s>'9') { if(s=='-')f=-1; s=getchar();}
while(s>='0'&&s<='9') { x=x*10+s-'0';s=getchar();}
x*=f;return x;
}
ll n,a,b;
void mul(ll f[7],ll a[7][7]){
ll c[7];
CLR(c,0);
for(int j=0;j<7;j++){
for(int k=0;k<7;k++){
c[j]=(c[j]+f[k]*a[k][j]%mod)%mod;
}
}
memcpy(f,c,sizeof(c));
}
void mulself(ll a[7][7]){
ll c[7][7];
CLR(c,0);
for(int i=0;i<7;i++){
for(int j=0;j<7;j++){
for(int k=0;k<7;k++){
c[i][j]=(c[i][j]+a[i][k]*a[k][j]%mod)%mod;
}
}
}
memcpy(a,c,sizeof(c));
}
int main(){
int T;
cin>>T;
while(T--){
scanf("%lld%lld%lld",&n,&a,&b);
if(n==1){
printf("%lld\n",a);
}else if(n==2){
printf("%lld\n",b);
}else{
ll f[7]={b,a,81,27,9,3,1};
ll a[7][7]=
{{1,1,0,0,0,0,0},
{2,0,0,0,0,0,0},
{1,0,1,0,0,0,0},
{0,0,4,1,0,0,0},
{0,0,6,3,1,0,0},
{0,0,4,3,2,1,0},
{0,0,1,1,1,1,1}};
n-=2;
for(;n;n>>=1){
if(n&1)mul(f,a);
mulself(a);
}
printf("%lld\n",f[0]);
}
}
}
Recursive sequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3374 Accepted Submission(s): 1485
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.
HDU5950 Recursive sequence 非线性递推式 矩阵快速幂的更多相关文章
- 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 ...
- HDU - 2604 Queuing(递推式+矩阵快速幂)
Queuing Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- [题解][SHOI2013]超级跳马 动态规划/递推式/矩阵快速幂优化
这道题... 让我见识了纪中的强大 这道题是来纪中第二天(7.2)做的,这么晚写题解是因为 我去学矩阵乘法啦啦啦啦啦对矩阵乘法一窍不通的童鞋戳链接啦 层层递推会TLE,正解矩阵快速幂 首先题意就是给你 ...
- HDU-6185-Covering(推递推式+矩阵快速幂)
Covering Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- [Lonlife1031]Bob and Alice are eating food(递推,矩阵快速幂)
题目链接:http://www.ifrog.cc/acm/problem/1031 题意:6个水果中挑出n个,使得其中2个水果个数必须是偶数,问有多少种选择方法. 设中0代表偶数,1代表奇数.分别代表 ...
- [HDOJ2604]Queuing(递推,矩阵快速幂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2604 递推式是百度的,主要是练习一下如何使用矩阵快速幂优化. 递推式:f(n)=f(n-1)+f(n- ...
- hihoCoder 1143 : 骨牌覆盖问题·一(递推,矩阵快速幂)
[题目链接]:click here~~ 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 骨牌,一种古老的玩具.今天我们要研究的是骨牌的覆盖问题: 我们有一个2xN的长条形 ...
- 【图灵杯 F】一道简单的递推题(矩阵快速幂,乘法模板)
Description 存在如下递推式: F(n+1)=A1*F(n)+A2*F(n-1)+-+An*F(1) F(n+2)=A1*F(n+1)+A2*F(n)+-+An*F(2) - 求第K项的值对 ...
- hdu 6185 递推+【矩阵快速幂】
<题目链接> <转载于 >>> > 题目大意: 让你用1*2规格的地毯去铺4*n规格的地面,告诉你n,问有多少种不同的方案使得地面恰好被铺满且地毯不重叠.答案 ...
随机推荐
- SQL server 2008安装教程
下载SQL server 2008 r2(网上资源很多,这里给出一个:安装) 解压后右键以管理员权限打开set-up 这里可能会出现问题:.net framework 3.5未安装,可以参考 快速安装 ...
- POJ 1191 棋盘分割 (区间DP,记忆化搜索)
题面 思路:分析公式,我们可以发现平均值那一项和我们怎么分的具体方案无关,影响答案的是每个矩阵的矩阵和的平方,由于数据很小,我们可以预处理出每个矩阵的和的平方,执行状态转移. 设dp[l1][r1][ ...
- Flow Layout
--------------siwuxie095 将根面板 contentPane 的布局切换为 Flow Layout Flow La ...
- Ros学习——launch文件解析
launch文件的重点是:节点(node)元素的集合. roslaunch 则是让所有的节点共享同一个终端. 1.标签(元素)说明 1. group标签 2. node标签 <group ns= ...
- ld 链接选项-L,-rpath-link,-rpath
转载自:http://blog.csdn.net/q1302182594/article/details/42102961 1. 三个C文件 1. world.c #include<stdio. ...
- java中字符串处理、串联和转换的几个常用方法,以及如果需要自己编程实现的具体实施步骤。
What? 如何分类? 如何使用? //String 类提供处理 Unicode 代码点(即字符)(TT观点:指的莫非就是对象的意思.)和 Unicode 代码单元(即 char 值)的方法.(TT观 ...
- 229B Planets
传送门 题目大意 有编号1~n的星球,在不用的星球间共有m个传送门,任意两个星球间传送门不超过1个,每个传送门需要花费一定的时间,但是在某时刻会在某星球有旅客到达,这时要一定等到没有旅客到达的时候才能 ...
- Luogu 3627 [APIO2009]抢掠计划
不爽. 为什么tarjan能爆栈啊 十分显然的缩点,给缩点之后的点连上权值为后一个点集权值的有向边,然后spfa跑最长路. 注意一开始$dis_{st}$应该等于$st$这个集合的权值. 时间复杂度$ ...
- WOJ 43 电话邀请
并查集缩点这个trick感觉明明用得很广泛,为什么以前都不知道…… 先把$m$条线路从小到大排个序,这样可以保证之前合并出来的一定是最小的,大的代价不会把小的覆盖掉. 维护两个并查集,一个用来缩点,另 ...
- 后台传Map到ftl
userInfo是后台传过来的Map <#list userInfo?keys as key> <tr > <td class="case_id" d ...