题目链接:Recursive sequence

题意:给出前两项和递推式,求第n项的值。

题解:递推式为:$F[i]=F[i-1]+2*f[i-2]+i^4$

主要问题是$i^4$处理,容易想到用矩阵快速幂,那么$i^4$就需要从$(i-1)$转移过来。

$ i^4 = (i-1)^4 + 4*(i-1)^3 + 6*(i-1)^2 + 4*(i-1) + 1$

$f_i$ $f_{i-1}$ $i^4$ $i^3$ $i^2$ $i$ $1$ = $f_{i-1}$ $f_{i-2}$ $(i-1)^4$ $(i-1)^3$ $(i-1)^2$ $(i-1)$ $1$ *

$\begin{pmatrix}
1 & 1 & 0 & 0 & 0 & 0 & 0 \\
2 & 0 & 0 & 0 & 0 & 0 & 0 \\
1 & 0 & 1 & 0 & 0 & 0 & 0 \\
4 & 0 & 4 & 1 & 0 & 0 & 0 \\
6 & 0 & 6 & 3 & 1 & 0 & 0 \\
4 & 0 & 4 & 3 & 2 & 1 & 0 \\
1 & 0 & 1 & 1 & 1 & 1 & 1 \\
\end{pmatrix}$

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 7
using namespace std; typedef long long ll;
const ll mod=; struct mat
{
ll m[N][N]=
{
{,,,,,,},
{,,,,,,},
{,,,,,,},
{,,,,,,},
{,,,,,,},
{,,,,,,},
{,,,,,,}
};
}; mat mul(mat a,mat b)
{
mat ans;
int i,j,k;
for(i=;i<N;i++)
for(j=;j<N;j++)
ans.m[i][j]=; for(i=;i<N;i++)
for(j=;j<N;j++)
for(k=;k<N;k++)
ans.m[i][j]=(ans.m[i][j]+a.m[i][k]*b.m[k][j])%mod;
return ans;
} ll matpow(int p,ll A,ll B)
{
mat ans,tmp;
int i,j;
for(int i=;i<N;i++)
for(int j=;j<N;j++)
ans.m[i][j]=;
p-=;
ans.m[][]=B;ans.m[][]=A;
ans.m[][]=;ans.m[][]=;ans.m[][]=;ans.m[][]=;ans.m[][]=;
while(p)
{
if(p&) ans=mul(ans,tmp);
tmp=mul(tmp,tmp);
p=p>>;
}
return ans.m[][];
} int main(){
int t;
scanf("%d",&t);
while(t--){
ll M,A,B;
scanf("%lld%lld%lld",&M,&A,&B);
printf("%lld\n",matpow(M,A,B)%mod);
}
return ;
}

HDU 5950 Recursive sequence(矩阵快速幂)的更多相关文章

  1. 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 ...

  2. hdu 5950 Recursive sequence 矩阵快速幂

    Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  3. 5950 Recursive sequence (矩阵快速幂)

    题意:递推公式 Fn = Fn-1 + 2 * Fn-2 + n*n,让求 Fn; 析:很明显的矩阵快速幂,因为这个很像Fibonacci数列,所以我们考虑是矩阵,然后我们进行推公式,因为这样我们是无 ...

  4. 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] 类似于斐波那契数列的递推式子吧, 但 ...

  5. HDU5950 Recursive sequence (矩阵快速幂加速递推) (2016ACM/ICPC亚洲赛区沈阳站 Problem C)

    题目链接:传送门 题目: Recursive sequence Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total ...

  6. HDU5950 Recursive sequence —— 矩阵快速幂

    题目链接:https://vjudge.net/problem/HDU-5950 Recursive sequence Time Limit: 2000/1000 MS (Java/Others)   ...

  7. HDU - 1005 Number Sequence 矩阵快速幂

    HDU - 1005 Number Sequence Problem Description A number sequence is defined as follows:f(1) = 1, f(2 ...

  8. HDU 1005 Number Sequence(矩阵快速幂,快速幂模板)

    Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1 ...

  9. HDU - 1005 -Number Sequence(矩阵快速幂系数变式)

    A number sequence is defined as follows:  f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) m ...

  10. CF1106F Lunar New Year and a Recursive Sequence——矩阵快速幂&&bsgs

    题意 设 $$f_i = \left\{\begin{matrix}1 , \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \  i < k\\ ...

随机推荐

  1. 自己实现数据结构系列三---Stack

    一.代码部分 1.定义接口 public interface Stack<E> { int getSize(); boolean isEmpty(); void push(E e); E ...

  2. 【问题解决方案】The MathType Dll cannot be found 问题解决方案

    先贴几个可能的方法: 如何解决MathPage.wll或MathType.dll文件找不到问题 The MathType Dll cannot be found 问题解决办法 如果还搞不定,试试卸载重 ...

  3. Ubuntu端口开放

    一.关于iptable的介绍 维基百科:https://zh.wikipedia.org/wiki/Iptables 注意:iptables的操作需要root权限 二.具体操作 sudo apt-ge ...

  4. MySql数据库连接池专题

    MySql数据库连接池专题 - aspirant - 博客园https://www.cnblogs.com/aspirant/p/6747238.html

  5. Laravel 5.2+ 使用url()全局函数返回前一个页面的地址

    注意:文章标题中5.2+表示该文章内容可向上兼容,适用于Laravel版本5.2及更高(目前最新为5.6),但不可向下兼容,即不适用于5.2版本以下.推荐大家花一点点时间,将自己的Laravel更新至 ...

  6. Window下通过SecureCRT的SSH2跳转到另一台Linux服务器

    我工作中的示例: 先登录192.168.2.145 Your password will be expired in 200 days.Welcome to Baoleiji System.Last ...

  7. drf实现图片验证码功能

    一.背景 在之前实现过django的图片验证码,有自己实现过的,也有基于django-simple-captcha的,都是基于form表单验证,若自己实现,可以获取相应的标签name便可以获取判断,若 ...

  8. Lodop打印控件 打印‘接下一页’‘以下空白’

    Lodop打印控件中,超文本超过设置的打印项高度 或超过纸张,就会自动分页,纯文本通过设置为多页项也可以根据打印项高度自动分页,Lodop中还提供了许多手动分页的方法,对于多页文档中(自动分页或手动分 ...

  9. js函数使用prototype和不适用prototype的区别

    js中类定义函数时用prototype与不用的区别 原创 2017年06月05日 12:25:41 标签: 函数 / prototype / class   首先来看一个实例: function Li ...

  10. JQuery跳出each循环的方法(包含数组遍历)

    0. 前言 也许我们通过 jquery 的循环方法进行数组遍历,但是当不符合条件时,怎么跳出当前循环?(即用each方法内,当不满足条件时想break跳出循环体,想continue继续执行下一个循环遍 ...