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,问有多少种不同的方案使得地面恰好被铺满且地毯不重叠.答案 ...
随机推荐
- java中sleep()的用法
Thread.sleep(long millis)和Thread.sleep(long millis, int nanos)静态方法强制当前正在执行的线程休眠(暂停执行),以“减慢线程”. 当线程睡眠 ...
- sql server导入excel等数据
1.首先打开并登陆sql server数据库 2.选择要将表导入的数据库,右击选择任务-->导入数据 3.在弹出的窗口中选择下一步 4.在弹出的窗口中选择数据源,也就是从哪种文件导入,sql s ...
- Struts2框架07 Struts2 + Spring + Mybatis 整合
1 导包 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.o ...
- pecl的使用
1. 安装方法 : pecl install packagename, 安装目录在/usr/local/php7/lib/php/extensions 例如:pecl install sealog 2 ...
- ZROI2018普转提day2t1
传送门 分析 我们通过仔细研究不难发现对于一次交换(i,i+1)的操作之后,在i之前的点就不可能跑到i之后,i+1之后的的点也不可能跑到i+1之前,所以这个序列在一次交换之后就相当于被分成了两个部分. ...
- apache的Base64编解码
import org.apache.commons.codec.binary.Base64; String Base64.encodeBase64URLSafeString(byte[]) byte ...
- Mat的迭代器使用
如果你熟悉 C++的 STL 库,那一定了解迭代器(iterator)的使用.迭代器可以方便地遍历所有元素.Mat 也增加了迭代器的支持,以便于矩阵元素的遍历.下面的例程功能跟上一节的例程类似,但是由 ...
- Python pika, TypeError: exchange_declare() got an unexpected keyword argument 'type' 问题修复
网上很多写法都是 type='fanout' 这样的.(这里是基于python=3.6版本, pika=0.13.0 版本) credentials = pika.PlainCredentials(' ...
- python运算优先级
运算符优先级(下面的优先级高) 运算符 描述 lambda Lambda表达式 or 布尔“或” and 布尔“与” not x 布尔“非” in not in 成员测试 is ...
- UVa 1204 Fun Game (状压DP)
题意:有一些小孩(至少两个)围成一圈,有 n 轮游戏,每一轮从某个小孩开始往左或者往右伟手帕,拿到手帕写上自己的性别(B,G),然后以后相同方向给下一个. 然后在某个小孩结束,给出 n 轮手帕上的序列 ...