hdu4914 Linear recursive sequence
首先把递推式求解转化为矩阵求幂,再利用特征多项式f(λ)满足f(A) = 0,将矩阵求幂转化为多项式相乘,
最后利用傅里叶变换的高效算法(迭代取代递归)(参见算法导论)解决。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <string>
#include <vector>
#include <set>
#include <cmath>
#include <ctime>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
#define lson (u << 1)
#define rson (u << 1 | 1)
typedef long long ll;
const double eps = 1e-;
const double pi = acos(-1.0);
const int maxn = 4e4 + ;
const int maxm = ;
const int mod = ;
const int inf = 0x3f3f3f3f; int n, a, b, p, q;
int size;
int f[maxn], g[maxn]; struct Complex{
double ii, ij;//ii::real, ij::image
Complex(double ii = , double ij = ) : ii(ii), ij(ij) {}
// Complex clear() { this->ii = this->ij = 0; }
Complex operator + (const Complex &rhs) const{
return Complex(ii + rhs.ii, ij + rhs.ij);
}
Complex operator - (const Complex &rhs) const{
return Complex(ii - rhs.ii, ij - rhs.ij);
}
Complex operator * (const Complex &rhs) const{
return Complex(ii * rhs.ii - ij * rhs.ij, ii * rhs.ij + ij * rhs.ii);
}
}; Complex a1[maxn], a2[maxn]; void fft(Complex *src, int len, int rev){
//len is power of 2
//rev == 1::dft rev == -1::idft
for(int i = , j = ; i < len; i++){
for(int k = len >> ; k > (j ^= k); k >>= ) ;
if(i < j) swap(src[i], src[j]);
}
for(int i = ; i <= len; i <<= ){
Complex wi(cos( * pi * rev / i), sin( * pi * rev / i));
//(wi)^i = 1
for(int j = ; j < len; j += i){
//using iteration insetad of recursion
Complex w(1.0, 0.0);
//w = (wi)^0
for(int k = j; k < j + i / ; k++){
Complex tem = w * src[k + i / ];
src[k + i / ] = src[k] - tem;
src[k] = src[k] + tem;
w = w * wi;
}
}
}
if(rev == -){
for(int i = ; i < len; i++) src[i].ii = (src[i].ii / len + eps);
}
} void multi(int *src1, int *src2, int len){
for(int i = ; i < len; i++){
a1[i].ii = a1[i].ij = a2[i].ii = a2[i].ij = ;
if(i < q){
a1[i].ii = (double)src1[i];
a2[i].ii = (double)src2[i];
}
}
fft(a1, len, ), fft(a2, len, );
for(int i = ; i < len; i++) a1[i] = a1[i] * a2[i];
fft(a1, len, -);
for(int i = ; i < len; i++) g[i] = (int)((ll)(a1[i].ii + eps) % mod);
for(int i = * q - ; i >= q; i--){
//this is because for the fisrt row in matrix A,
//which satisfies ths (f(n + q),...,f(n))T = A((f(n + q - 1),...,f(n - 1))T)
//only two elements are nonzero integers
g[i - q] = (g[i - q] + g[i] * b) % mod;
g[i - p] = (g[i - p] + g[i] * a) % mod;
}
memcpy(src1, g, sizeof(int) * q);
} int tmp[maxn], ans[maxn]; int main(){
//freopen("in.txt", "r", stdin);
while(~scanf("%d%d%d%d%d", &n, &a, &b, &p, &q)){
a %= mod, b %= mod;
f[] = ;
for(int i = ; i < q; i++){
f[i] = i < p ? a + b : a * f[i - p] + b;
f[i] %= mod;
}
if(n < q){
printf("%d\n", f[n]);
continue;
}
size = ;
while(size <= (q - ) * ) size <<= ;
memset(tmp, , sizeof tmp);
memset(ans, , sizeof ans);
ans[] = tmp[] = ;
while(n){
if(n & ) multi(ans, tmp, size);
multi(tmp, tmp, size);
n >>= ;
}
int res = ;
for(int i = ; i < q; i++){
res = (res + ans[i] * f[i]) % mod;
}
printf("%d\n", res);
}
return ;
}
hdu4914 Linear recursive sequence的更多相关文章
- HDU 4914 Linear recursive sequence(矩阵乘法递推的优化)
题解见X姐的论文 矩阵乘法递推的优化.仅仅是mark一下. .
- hdu 5950 Recursive sequence 矩阵快速幂
Recursive sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- HDU 5950 Recursive sequence 【递推+矩阵快速幂】 (2016ACM/ICPC亚洲区沈阳站)
Recursive sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- CF1106F Lunar New Year and a Recursive Sequence
题目链接:CF1106F Lunar New Year and a Recursive Sequence 大意:已知\(f_1,f_2,\cdots,f_{k-1}\)和\(b_1,b_2,\cdot ...
- HDU 5950 Recursive sequence(矩阵快速幂)
题目链接:Recursive sequence 题意:给出前两项和递推式,求第n项的值. 题解:递推式为:$F[i]=F[i-1]+2*f[i-2]+i^4$ 主要问题是$i^4$处理,容易想到用矩阵 ...
- HDU5950 Recursive sequence (矩阵快速幂)
Recursive sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- Codeforces 1106F Lunar New Year and a Recursive Sequence | BSGS/exgcd/矩阵乘法
我诈尸啦! 高三退役选手好不容易抛弃天利和金考卷打场CF,结果打得和shi一样--还因为queue太长而unrated了!一个学期不敲代码实在是忘干净了-- 没分该没分,考题还是要订正的 =v= 欢迎 ...
- HDU5950 Recursive sequence (矩阵快速幂加速递推) (2016ACM/ICPC亚洲赛区沈阳站 Problem C)
题目链接:传送门 题目: Recursive sequence Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total ...
- HDU5950 Recursive sequence 非线性递推式 矩阵快速幂
题目传送门 题目描述:给出一个数列的第一项和第二项,计算第n项. 递推式是 f(n)=f(n-1)+2*f(n-2)+n^4. 由于n很大,所以肯定是矩阵快速幂的题目,但是矩阵快速幂只能解决线性的问题 ...
随机推荐
- Tomcat类加载器机制
Tomcat为什么需要定制自己的ClassLoader: 1.定制特定的规则:隔离webapp,安全考虑,reload热插拔 2.缓存类 3.事先加载 要说Tomcat的Classloader机制,我 ...
- Some settings of PostgreSQL
Here are some setting recommendations about checkpoints, some values to set in postgresql.conf. A ch ...
- SQL 数据库基础
SQL:Structured Quety Language SQL SERVER是一个以客户/服务器(c/s)模式访问.使用Transact-SQL语言的关系型数据库管理子系统(RDBMS) DBMS ...
- [转]MySQL数据库引擎介绍、区别、创建和性能测试的深入分析
本篇文章是对MySQL数据库引擎介绍.区别.创建和性能测试进行了详细的分析介绍,需要的朋友参考下 数据库引擎介绍 MySQL数据库引擎取决于MySQL在安装的时候是如何被编译的.要添加一个新的引擎 ...
- HDU 5000 Clone(离散数学+DP)(2014 ACM/ICPC Asia Regional Anshan Online)
Problem Description After eating food from Chernobyl, DRD got a super power: he could clone himself ...
- CCF真题之节日
201503-3 问题描述 有一类节日的日期并不是固定的,而是以“a月的第b个星期c”的形式定下来的,比如说母亲节就定为每年的五月的第二个星期日. 现在,给你a,b,c和y1, y2(1850 ≤ y ...
- Android 5.0新特性了解(二)----RippleEffect
1.本文介绍的是Android5.0中其中一个炫酷的效果,点击水波纹扩散效果( RippleEffect),以下介绍的实现方式都是调用Android5.0的新API,并非自定义实现,所以支持在Andr ...
- Silverlight ModelView中调用UI进程
Silverlihgt: Deployment.Current.Dispatcher.BeginInvoke wpf: App.Current.Dispatcher.Invoke
- pic计数
#include <pic.h> //用的是PICC编译器 __CONFIG (HS & PROTECT & PWRTEN & BOREN & WDTDIS ...
- 弹出层和ajax数据交互
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs& ...