BUPT2017 wintertraining(15) #6F

题意

\(f(1)=a,f(2)=b,f(i)=2*(f(i-2)+f(i-1)+i^4)\)

给定n,a,b ,\(N,a,b < 2^{31}\),求f(n)% 2147493647。

题解

\[f[i]=(f[i-1]+2*f[i-2]+i^4)*2\\
i^4=(i-1)^4+4*(i-1)^3+6*(i-1)^2+4*(i-1)+1
\]

我们可以构造出矩阵乘法

\[\left[
\begin{matrix}
f_{i}\\
f_{i-1}\\
i^4\\
i^3\\
i^2\\
i\\
1\\
\end{matrix}
\right]
=
\left[
\begin{matrix}
1&2&1&4&6&4&1\\
1&0&0&0&0&0&0\\
0&0&1&4&6&4&1\\
0&0&0&1&3&3&1\\
0&0&0&0&1&2&1\\
0&0&0&0&0&1&1\\
0&0&0&0&0&0&1\\
\end{matrix}
\right]
*
\left[
\begin{matrix}
f_{i-1}\\
f_{i-2}\\
(i-1)^4\\
(i-1)^3\\
(i-1)^2\\
i-1\\
1\\
\end{matrix}
\right]
\]

B为\([f_2,f_1,2^4,2^3,2^2,2,1]^T\)于是\(f(n)=A^{n-2}*B\)的第一项。

有了递推关系,再用矩阵快速幂解决就好了。

代码

#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
#include <iostream>
using namespace std;
const ll mod=2147493647;
struct Mat{
int r,c;
ll a[10][10];
Mat(int _r,int _c){
r=_r;c=_c;
memset(a,0,sizeof a);
}
Mat operator *(const Mat &b)const{
Mat c(r,b.c);
for(int i=0;i<r;i++)
for(int j=0;j<b.c;j++)
for(int k=0;k<b.r;k++){
c.a[i][j]=(c.a[i][j]+a[i][k]*b.a[k][j]%mod)%mod;
}
return c;
}
}A(7,7),B(7,1); Mat qpow(Mat a,int b){
Mat c(a.r,a.c);
for(int i=0;i<a.r;i++)c.a[i][i]=1;
while(b){
if(b&1)c=c*a;
b>>=1;
a=a*a;
}
return c;
}
int main() {
int at[10][10]={{1,2,1,4,6,4,1},
{1,0,0,0,0,0,0},
{0,0,1,4,6,4,1},
{0,0,0,1,3,3,1},
{0,0,0,0,1,2,1},
{0,0,0,0,0,1,1},
{0,0,0,0,0,0,1}};
for(int i=0;i<7;i++)for(int j=0;j<7;j++)A.a[i][j]=at[i][j];
int t,n,a,b;
cin>>t;
while(t--){
scanf("%d%d%d",&n,&a,&b);
B.a[0][0]=b;B.a[1][0]=a;
B.a[6][0]=1;
for(int i=5;i>1;i--)B.a[i][0]=B.a[i+1][0]*2;
if(n==1){
printf("%d\n",a);
}else if(n==2){
printf("%d\n",b);
}else{
Mat C=qpow(A,n-2)*B;
printf("%lld\n",C.a[0][0]);
}
}
return 0;
}

【HDU5950】Recursive sequence(矩阵快速幂)的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

  7. hdu 5950 Recursive sequence 矩阵快速幂

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

  8. hdu-5667 Sequence(矩阵快速幂+费马小定理+快速幂)

    题目链接: Sequence Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others) ...

  9. UVA - 10689 Yet another Number Sequence 矩阵快速幂

                      Yet another Number Sequence Let’s define another number sequence, given by the foll ...

  10. Yet Another Number Sequence——[矩阵快速幂]

    Description Everyone knows what the Fibonacci sequence is. This sequence can be defined by the recur ...

随机推荐

  1. myeclipse使用hibernate5框架load延迟装载对象报错_$$_javassist_0 cannot be cast to javassist.util.proxy.Proxy

    jar包问题,将hibernate-core-5.0.12.Final.jar删除,换为hibernate-core-4.2.3.final.jar搞定.注意项目运行过后可能删不掉jar包,只需关闭m ...

  2. ES5中文分词(IK)

    ElasticSearch5中文分词(IK) ElasticSearch安装 官网:https://www.elastic.co 1.ElasticSearch安装 1.1.下载安装公共密钥 rpm ...

  3. 【学习总结】Git学习-参考廖雪峰老师教程六-分支管理

    学习总结之Git学习-总 目录: 一.Git简介 二.安装Git 三.创建版本库 四.时光机穿梭 五.远程仓库 六.分支管理 七.标签管理 八.使用GitHub 九.使用码云 十.自定义Git 期末总 ...

  4. 配置nginx反向代理服务器,解决浏览器跨域调用接口的限制问题

    配置nginx反向代理服务器,解决浏览器跨域调用接口的限制问题 - 大venn的博客 - CSDN博客https://blog.csdn.net/u011135260/article/details/ ...

  5. ORACLE 当字段中有数据如何修改字段类型

    创建视图的时候,因为表太多,里面一些字段类型不一样,PL/SQL报错,为‘表达式必须具有对应表达式相同的数据类型’,发现后,一个字段的类型为CLOB和VARCHAR2(4000)两种,将CLOB进行修 ...

  6. IdentityServer4【QuickStart】之使用ClientCredentials流程保护API

    使用ClientCredentials流程保护API 这个示例展示了使用IdentityServer中保护APIs的最基本的场景. 在这个场景中我们会定义一个API和一个想要访问它的客户端.客户端会在 ...

  7. 局域网 FTP建立,搭建一个简易的局域网服务器

    1.创建用户名以及密码: 右键我的电脑 -> 管理->本地用户和组->右键用户->新用户----设置用户名密码: 2.安装IIS 和FTP :控制面板->程序->打 ...

  8. Day 5-<补充> 类的的继承和查找顺序

    类的继承于查找顺序: 在py2中,不继承object的类为经典类,经典类继承查找:深度优先. 在py3中,默认继承object,所以python3中都是新式类,新式类的继承查找:广度优先. 类的特殊属 ...

  9. VS code常用快捷方式—转载

    http://www.cnblogs.com/weihe-xunwu/p/6687000.html

  10. Android——Activity的简绍

    Activity Activity的运行机制其实和JavaEE中的servlet很像,而我们的Android系统也就相当与其servlet容器,Activity在其中进行创建实例.初始化.运行.销毁等 ...