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,问有多少种不同的方案使得地面恰好被铺满且地毯不重叠.答案 ...
随机推荐
- 浅谈Android四大组建之一Service---Service的创建
Service是安卓四大组件之一,个人觉得Service的使用还是比较简单的额,可以理解为看不见的Activity,因为Service的使用和Activity十分接近.启动啊,生命周期等,都十分简单. ...
- LaTex: 表格单元格内容 分行显示/换行
问题:如何同时让表格同一行一个单元格的文字能垂直居中?比如说文字超长超出页面范围需要分行显示 答:(来源于smth) 方案一: \newcommand{\tabincell}[2]{\begin{ta ...
- 自定义Android Studio方法注释模板
前言 你们从Eclipse转到Android Studio的时候,是不是会怀念Eclipse的方法注释模版? 敲/**加回车,模板就出来了,而Android Studio却不能自定义(或者我没有找到) ...
- Replace Pioneer注册方法
Replace Pioneer注册方法 Replace Pioneer过期后,会弹出一个注册(Registration)窗口,其中有一个试用选项(Trial License),点击Trial Lice ...
- 多线程 wait(),notify()方法,案例总结
废话不多说,案例如下 package com.xujingyang.Exok; /** * 商品类 * @author 徐景洋 */ public class Goods { private Stri ...
- 204. Count Primes 统计<n的质数的个数
[抄题]: Count the number of prime numbers less than a non-negative number, n. [暴力解法]: 时间分析: 空间分析: [优化后 ...
- ROS Learning-001 安装 ROS indigo
如何在 Ubuntu14.04 上安装 ROS indigo 我使用的虚拟机软件:VMware Workstation 11 使用的Ubuntu系统:Ubuntu 14.04.4 LTS ROS 版本 ...
- 628D Magic Numbers
传送门 题目大意 定义n-magic为从左往右,偶数位置均为n,奇数位置不为n的一类数.求出[a,b]内所有可被m整除的d-magic个数. 分析 显然是数位dp,我们用dp[i][j][k]表示考虑 ...
- A - Dictionary
传送门 题目大意 给你n个字符串,问是否可以通过改变26个字母的排列顺序是这n个字符串的字典序是非降排列的. 分析 我们考虑设相邻两个字符串的第一个不相同字符的位置为j,以为要求字典序不降,所以有第i ...
- 自定义select样式
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...