HDU 5950 Recursive sequence 【递推+矩阵快速幂】 (2016ACM/ICPC亚洲区沈阳站)
Recursive sequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 249 Accepted Submission(s): 140Problem DescriptionFarmer 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.InputThe 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.OutputFor 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 Input2
3 1 2
4 1 10Sample Output85
369HintIn 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.SourceRecommendjiangzijing2015 | We have carefully selected several similar problems for you: 5960 5959 5958 5957 5956
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5950
题目大意:
Fi=Fi-1+2Fi-2+i4。给定F1和F2求Fn。
题目思路:
【递推+矩阵快速幂】
现场用算了1个多小时的公式过了。
主要还是我太菜。递推写的太少。
先考虑f(i)=f(i-1)+2f(i-2),很容易写出递推矩阵
0 2
1 1
(i+1)4=i4+4i3+6i2+4i+1。
所以需要在递推矩阵种存下i的4 3 2 1 0次幂,以便推出(i+1)4,矩阵为
1 0 0 0 0
4 1 0 0 0
6 3 1 0 0
4 3 2 1 0
1 1 1 1 1
于是f={fi-1,fi,i4,i3,i2,i1,i0},将以上两个矩阵合并,即可推出{fi,fi+1,(i+1)4,(i+1)3,(i+1)2,(i+1)1,(i+1)0}.矩阵如下
0 2 0 0 0 0 0
1 1 0 0 0 0 0
0 1 1 0 0 0 0
0 4 4 1 0 0 0
0 6 6 3 1 0 0
0 4 4 3 2 1 0
0 1 1 1 1 1 1
推出转移矩阵后只需要根据n求矩阵快速幂即可。
//
//by coolxxx
//#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<iomanip>
#include<map>
#include<stack>
#include<queue>
#include<set>
#include<bitset>
#include<memory.h>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//#include<stdbool.h>
#include<math.h>
#pragma comment(linker,"/STACK:1024000000,1024000000")
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) ((a)*(a))
#define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
#define mem(a,b) memset(a,b,sizeof(a))
#define eps (1e-8)
#define J 10000
#define mod 2147493647
#define MAX 0x7f7f7f7f
#define PI 3.14159265358979323
#define N 14
#define M 7
using namespace std;
typedef long long LL;
double anss;
LL aans;
int cas,cass;
int n,m,lll,ans;
LL f[N];
LL a[N][N];
LL ma[N][N]={{},
{,,,,,,,},
{,,,,,,,},
{,,,,,,,},
{,,,,,,,},
{,,,,,,,},
{,,,,,,,},
{,,,,,,,}};
void multi(LL a[][N],LL b[][N],LL c[][N])
{
int i,j,k;
LL t[N][N];
mem(t,);
for(i=;i<=M;i++)
for(j=;j<=M;j++)
for(k=;k<=M;k++)
t[i][j]=(t[i][j]+a[i][k]*b[k][j]%mod)%mod;
memcpy(c,t,sizeof(t));
}
void mi(LL a[][N],int y)
{
LL tmp[N][N];
mem(tmp,);
tmp[][]=tmp[][]=tmp[][]=tmp[][]=tmp[][]=tmp[][]=tmp[][]=;
while(y)
{
if(y&)multi(tmp,a,tmp);
y>>=;multi(a,a,a);
}
memcpy(a,tmp,sizeof(tmp));
}
void work()
{
LL t[N];
mem(t,);
int i,j;
for(i=;i<=M;i++)
for(j=;j<=M;j++)
t[i]=(t[i]+f[j]*a[j][i]%mod)%mod;
memcpy(f,t,sizeof(t));
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("1.txt","r",stdin);
// freopen("2.txt","w",stdout);
#endif
int i,j,k;
int x,y,z;
// init();
for(scanf("%d",&cass);cass;cass--)
// for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
// while(~scanf("%s",s))
// while(~scanf("%d%d",&n,&m))
{
memcpy(a,ma,sizeof(a));
scanf("%d%lld%lld",&n,&f[],&f[]);
f[]=,f[]=,f[]=,f[]=,f[]=;
if(n==)
{
printf("%lld\n",f[]);
continue;
}
mi(a,n-);
work();
printf("%lld\n",f[]);
}
return ;
}
/*
// //
*/
HDU 5950 Recursive sequence 【递推+矩阵快速幂】 (2016ACM/ICPC亚洲区沈阳站)的更多相关文章
- HDU 5952 Counting Cliques 【DFS+剪枝】 (2016ACM/ICPC亚洲区沈阳站)
Counting Cliques Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- HDU 5949 Relative atomic mass 【模拟】 (2016ACM/ICPC亚洲区沈阳站)
Relative atomic mass Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- 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 5950 Recursive sequence 递推转矩阵
Recursive sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- hdu 2604 递推 矩阵快速幂
HDU 2604 Queuing (递推+矩阵快速幂) 这位作者讲的不错,可以看看他的 #include <cstdio> #include <iostream> #inclu ...
- HDU 5948 Thickest Burger 【模拟】 (2016ACM/ICPC亚洲区沈阳站)
Thickest Burger Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- 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 ...
- Recursive sequence HDU - 5950 (递推 矩阵快速幂优化)
题目链接 F[1] = a, F[2] = b, F[i] = 2 * F[i-2] + F[i-1] + i ^ 4, (i >= 3) 现在要求F[N] 类似于斐波那契数列的递推式子吧, 但 ...
- HDU - 5950 Recursive sequence(二项式+矩阵合并+矩阵快速幂)
Recursive sequence Farmer John likes to play mathematics games with his N cows. Recently, they are a ...
随机推荐
- 开发一个完整的JavaScript组件
作为一名开发者,大家应该都知道在浏览器中存在一些内置的控件:Alert,Confirm等,但是这些控件通常根据浏览器产商的不同而形态各异,视觉效果往往达不到UI设计师的要求.更重要的是,这类内置控件的 ...
- java之内存管理
对于JVM的垃圾回收机制来说,是否回收一个对象的标准在于:是否还有引用变量引用该对象,只要有引用变量引用该对象,垃圾回收机制就不会回收它. 强引用:创建一个对象,并把这个对象赋给一个引用变量.这种引用 ...
- Codevs 3233 古道
3233 古道 时间限制: 1 s 空间限制: 8000 KB 题目等级:**白银 Silver** [传送门](http://codevs.cn/problem/3233/) 题目描述 Descri ...
- C#语言之“string格式的日期时间字符串转为DateTime类型”的方法(转)
原文链接:http://www.cnblogs.com/Pickuper/articles/2058880.html 方法一:Convert.ToDateTime(string) string格式有要 ...
- OpenCV(6)-腐蚀和膨胀
腐蚀和膨胀属于形态学操作. 腐蚀和膨胀 腐蚀是指:将卷积核B滑过图像A,找出卷积核区域内最小像素值作为锚点像素值.这一操作可以扩大低像素值区域. 膨胀是指:将卷积核B滑过图像A,找出卷积核区域内最大像 ...
- 暑假集训(2)第一弹 -----Is It A Tree?(Poj308)
A - Is It A Tree? Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:10000KB ...
- Mac OS 安装 Port
简介 MacPorts类似与apt-get以及yum等软件包管理工具,可以方便的进行安装与卸载软件的功能,同时可以自动安装软件包的依赖,非常方便,同类的还有brew等工具. 安装 下载MacPorts ...
- CI的面向切面的普通权限验证
第一步:开启CI的钩子配置,此次不多说看CI手册即可. 第二步:在cofig/hooks.php中进行钩子配置,CI手册中有记载 <?php defined('BASEPATH') OR exi ...
- PHP用memcached做实时分页
用memcached做分页缓存,可能很多人会觉得麻烦而不用.因为在增加.修改.删除的过程中,你不知道会影响到哪些数据,而如果把所有分页相关的数据缓存都删除并重新生成一遍,实现又很麻烦,甚至不可行,所以 ...
- 摩尔斯电码(Morse Code)Csharp实现
摩尔斯电码,在早期的"生产斗争"生活中,扮演了很重要的角色,作为一种信息编码标准,摩尔斯电码拥有其他编码方案无法超越的长久生命.摩尔斯电码在海事通讯中被作为国际标准一直使用到199 ...