HDU 5950 Recursive sequence(矩阵快速幂)
题目链接: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(矩阵快速幂)的更多相关文章
- 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 ...
- hdu 5950 Recursive sequence 矩阵快速幂
Recursive sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- 5950 Recursive sequence (矩阵快速幂)
题意:递推公式 Fn = Fn-1 + 2 * Fn-2 + n*n,让求 Fn; 析:很明显的矩阵快速幂,因为这个很像Fibonacci数列,所以我们考虑是矩阵,然后我们进行推公式,因为这样我们是无 ...
- 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] 类似于斐波那契数列的递推式子吧, 但 ...
- HDU5950 Recursive sequence (矩阵快速幂加速递推) (2016ACM/ICPC亚洲赛区沈阳站 Problem C)
题目链接:传送门 题目: Recursive sequence Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total ...
- HDU5950 Recursive sequence —— 矩阵快速幂
题目链接:https://vjudge.net/problem/HDU-5950 Recursive sequence Time Limit: 2000/1000 MS (Java/Others) ...
- HDU - 1005 Number Sequence 矩阵快速幂
HDU - 1005 Number Sequence Problem Description A number sequence is defined as follows:f(1) = 1, f(2 ...
- 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 ...
- 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 ...
- CF1106F Lunar New Year and a Recursive Sequence——矩阵快速幂&&bsgs
题意 设 $$f_i = \left\{\begin{matrix}1 , \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ i < k\\ ...
随机推荐
- #Leetcode# 836. Rectangle Overlap
https://leetcode.com/problems/rectangle-overlap/ A rectangle is represented as a list [x1, y1, x2, y ...
- PAT L3-016 二叉搜索树的结构
https://pintia.cn/problem-sets/994805046380707840/problems/994805047903240192 二叉搜索树或者是一棵空树,或者是具有下列性质 ...
- PHP常见错误汇总
日常开发和调试的时候,经常会遇到一些错误,光怪陆离的不知所以,所以,特此将错误汇总一下,借鉴!!! 1. 原因分析: 一般可能是该文件出现了问题,检查一下代码和格式,是否出现开始的地方出现了空格,或 ...
- text-decoration、text-decoration-color、text-decoration-line、text-decoration-style属性
text-decoration:over-line 定义上划线 text-decoration:line-through 定义删除线 text-decoration:underline 定义下划 ...
- java Date时间的各种转换方式和Mysql存时间类型字段的分析
一:各种Date之间的转换方法 public class TimeTest { public static void main(String[] args) { Date date = new Dat ...
- 902. Kth Smallest Element in a BST
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. You ...
- day 7-17 多表查询
一. 准备表 #部门表 create table dep( id int, name varchar(20) ); #员工表 create table emp( id int primary key ...
- Prism框架研究(二)
首先在介绍本节内容之前,首先来看看官方文档来如何描述Prism 应用的初始化吧!A Prism application requires registration and configuration ...
- 老男孩python学习自修第十六天【常用模块之sys和os】
例子: sys.argv 命令行参数List,第一个元素是程序本身路径 sys.exit(n) 退出程序,正常退出时exit(0) sys.version 获取Python解释程序的版本信息 sys. ...
- Yii2的使用
yii2的下载安装 使用下载好的文件配置高级模板,在配置文件配置好数据库和gii 在common模板配置db: 在backend模板配置gii: 配置nginx服务器访问backend和fronten ...