题意:f[1]=a,f[2]=b,f[i]=2f[i-2]+f[i-1]+i^4(i>=3),多组询问求f[n]对2147493647取模

N,a,b < 2^31

思路:重点在于i^4的处理

对于i转移矩阵中可以记录下它的0,1,2,3,4次项

i的幂又可以由i-1的幂运算得出,最后推出的系数是二项式展开的系数

试试新的矩乘模板

 #include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<vector>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef vector<int> VI;
#define fi first
#define se second
#define MP make_pair
#define N 2100000
#define MOD 2147493647
#define eps 1e-8
#define pi acos(-1)
const int MAXN=; int read()
{
int v=,f=;
char c=getchar();
while(c<||<c) {if(c=='-') f=-; c=getchar();}
while(<=c&&c<=) v=(v<<)+v+v+c-,c=getchar();
return v*f;
} struct matrix //矩阵类
{
int n,m;
ll data[MAXN][MAXN];
}; matrix ma,mb;
ll a,b,c,d,p,n; matrix matrixMul(matrix a, matrix b)
{
matrix re;
if(a.m!=b.n)
{
printf("error\n");
return re;
}
memset(re.data,,sizeof(re.data));
re.n = a.n; re.m = b.m;
for(int i = ; i <= a.n; i++)
{
for(int j = ; j <= a.m; j++)
{
if(a.data[i][j] == ) continue;
for(int k = ; k <= b.m; k++)
{
re.data[i][k] += (a.data[i][j] % MOD * b.data[j][k] % MOD) % MOD;
re.data[i][k] %= MOD;
}
}
}
return re;
} matrix matrixPow(matrix a,int b)
{
matrix re;
if(a.n!=a.m)
{
printf("error2\n");
return re;
}
re.n=re.m=a.n;
memset(re.data,,sizeof(re.data));
for(int i=;i<=re.n;i++) re.data[i][i]=;
while(b)
{
if(b&) re=matrixMul(re,a);
a=matrixMul(a,a);
b>>=;
}
return re;
} void inputMat(int n,int m,matrix &a,ll *b)
{
a.n = n; a.m = m;
for(int i = ; i <= n; i++)
for(int j = ; j <= m; j++)
a.data[i][j] = *(b + (i - ) * m + (j - ));
} void init(){
ll pt[][] = {b,a,,,,,};
inputMat(,,ma,*pt);
ll pt2[][] = {,,,,,,,
,,,,,,,
,,,,,,,
,,,,,,,
,,,,,,,
,,,,,,,
,,,,,,,};
inputMat(,,mb,*pt2);
} int main()
{
int cas;
scanf("%d",&cas);
while(cas--)
{
scanf("%I64d%I64d%I64d",&n,&a,&b);
int i=;
a%=MOD;
b%=MOD;
if(n == )
printf("%I64d\n",a);
else if(n == )
printf("%I64d\n",b);
else
{ init();
ma=matrixMul(ma,matrixPow(mb,n-));
}
printf("%I64d\n",ma.data[][]);
}
return ;
}

【HDOJ5950】Recursive sequence(矩阵乘法,快速幂)的更多相关文章

  1. Qbxt 模拟赛 Day4 T2 gcd(矩阵乘法快速幂)

    /* 矩阵乘法+快速幂. 一开始迷之题意.. 这个gcd有个规律. a b b c=a*x+b(x为常数). 然后要使b+c最小的话. 那x就等于1咯. 那么问题转化为求 a b b a+b 就是斐波 ...

  2. 洛谷 P4910 帕秋莉的手环 矩阵乘法+快速幂详解

    矩阵快速幂解法: 这是一个类似斐波那契数列的矩乘快速幂,所以推荐大家先做一下下列题目:(会了,差不多就是多倍经验题了) 注:如果你不会矩阵乘法,可以了解一下P3390的题解 P1939 [模板]矩阵加 ...

  3. ACM学习历程—HDU5667 Sequence(数论 && 矩阵乘法 && 快速幂)

    http://acm.hdu.edu.cn/showproblem.php?pid=5667 这题的关键是处理指数,因为最后结果是a^t这种的,主要是如何计算t. 发现t是一个递推式,t(n) = c ...

  4. 矩阵乘法快速幂 codevs 1732 Fibonacci数列 2

    1732 Fibonacci数列 2  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解  查看运行结果     题目描述 Description 在“ ...

  5. codevs1281 矩阵乘法 快速幂 !!!手写乘法取模!!! 练习struct的构造函数和成员函数

    对于这道题目以及我的快速幂以及我的一节半晚自习我表示无力吐槽,, 首先矩阵乘法和快速幂没必要太多说吧,,嗯没必要,,我相信没必要,,实在做不出来写两个矩阵手推一下也就能理解矩阵的顺序了,要格外注意一些 ...

  6. [vijos1725&bzoj2875]随机数生成器<矩阵乘法&快速幂&快速乘>

    题目链接:https://vijos.org/p/1725 http://www.lydsy.com/JudgeOnline/problem.php?id=2875 这题是前几年的noi的题,时间比较 ...

  7. [codevs]1250斐波那契数列<矩阵乘法&快速幂>

    题目描述 Description 定义:f0=f1=1, fn=fn-1+fn-2(n>=2).{fi}称为Fibonacci数列. 输入n,求fn mod q.其中1<=q<=30 ...

  8. 【BZOJ-1009】GT考试 KMP+DP+矩阵乘法+快速幂

    1009: [HNOI2008]GT考试 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 2745  Solved: 1694[Submit][Statu ...

  9. 矩阵乘法快速幂 codevs 1574 广义斐波那契数列

    codevs 1574 广义斐波那契数列  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 钻石 Diamond   题目描述 Description 广义的斐波那契数列是指形如 ...

  10. BZOJ-1875 HH去散步 DP+矩阵乘法快速幂

    1875: [SDOI2009]HH去散步 Time Limit: 20 Sec Memory Limit: 64 MB Submit: 1196 Solved: 553 [Submit][Statu ...

随机推荐

  1. 通过StringBuilder的reverse()实现倒序

    import java.util.Scanner; public class ReverseString { public static void main(String[] args) { Scan ...

  2. Example of how to implement a view-based source list (NSOutlineView) using Cocoa Bindings

    You tagged this with the cocoa-bindings tag, so I assume you mean "with bindings." I whipp ...

  3. 安装linux虚拟机(Ubuntu & KALI)

    VMware workstation 15.0.0 ubuntu-18.10-desktop 首先安装VMware 参考资料很多,不再赘述. 之后参考 https://www.cnblogs.com/ ...

  4. Evaluate|GC content|Phred|BAC|heterozygous single nucleotide polymorphisms|estimate genome size|

    (Evaluate):检查reads,可使用比对软件:使用SOAPaligner重新排列:采用massively parallel next-generation sequencing technol ...

  5. Bootstrap 网格系统(Grid System)实例4

    Bootstrap 网格系统(Grid System)实例4:中型和大型设备 <!DOCTYPE html><html><head><meta http-eq ...

  6. java在线聊天项目0.3版本 制作客户端窗体,实现发送按钮和回车发送信息功能,使用ActionListener监听事件中actionPerformed方法(用内部类和匿名内部类两种方法)

    方法一,使用匿名内部类的监听方法,因方法一致代码稍冗余 package com.swift; import java.awt.BorderLayout; import java.awt.Color; ...

  7. CSS3的border-image

    border-image:none|image-url|number|percentage|stretch,repeat,round 参数: none:默认,无背景图片 url:地址,可以为绝对,也可 ...

  8. (三)Python3 循环语句——while

    while语句的一般形式: while 判断条件: 语句 同样需要注意冒号和缩进.另外,在 Python 中没有 do..while 循环. 以下实例使用了 while 来计算 1 到 100 的总和 ...

  9. iOS使用Reveal分析他人app界面

    本文转自http://blog.csdn.net/cuibo1123/article/details/45694657 安装: 首先前往 http://revealapp.com/download/  ...

  10. LeetCode(96) Unique Binary Search Trees

    题目 Given n, how many structurally unique BST's (binary search trees) that store values 1-n? For exam ...