就算告诉我是矩阵快速幂我也推不出递推式呀!!!

官方题解:

对于任意i>=1,当j>=3时,有通过归纳法可以得到

进而推导出

后来自己重新推导了一遍

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std; typedef long long ll;
const ll Mod=1e9+7;
const int msize=2;
const int N=4; struct Mat
{
ll mat[N][N];
}; Mat operator *(Mat a, Mat b)
{
Mat c;
memset(c.mat, 0, sizeof(c.mat));
for(int k = 0; k < msize; ++k)
for(int i = 0; i < msize; ++i)
if(a.mat[i][k])
for(int j = 0; j < msize; ++j)
if(b.mat[k][j])
c.mat[i][j] = (c.mat[i][j] +a.mat[i][k] * b.mat[k][j])%Mod;
return c;
} Mat operator ^(Mat a, ll k)
{
Mat c;
memset(c.mat,0,sizeof(c.mat));
for(int i = 0; i < msize; ++i)
c.mat[i][i]=1;
for(; k; k >>= 1)
{
if(k&1) c = c*a;
a = a*a;
}
return c;
} Mat operator -(Mat a,Mat b)
{
for(int i=0; i<msize; i++)
for(int j=0; j<msize; j++)
a.mat[i][j]=(a.mat[i][j]-b.mat[i][j])%Mod;
return a;
} int main()
{
// freopen("in.txt","r",stdin);
int t;
ll n,m;
Mat A,B1,B2;
A.mat[0][0]=1,A.mat[0][1]=2;
A.mat[1][0]=1,A.mat[1][1]=0;
B1.mat[0][0]=B1.mat[1][1]=1;
B1.mat[0][1]=B1.mat[1][0]=0;
B2.mat[0][0]=0,B2.mat[0][1]=2;
B2.mat[1][0]=1,B2.mat[1][1]=-1;
scanf("%d",&t);
while(t--)
{
scanf("%I64d%I64d",&n,&m);
Mat ans;
if(n%2==0) ans=((A^n)-B1)^(m-1);
else ans=((A^n)-B2)^(m-1);
printf("%I64d\n",(ans.mat[1][0]+ans.mat[1][1])%Mod);
}
return 0;
}

hdu 6050 Funny Function 矩阵快速幂的更多相关文章

  1. HDU.1575 Tr A ( 矩阵快速幂)

    HDU.1575 Tr A ( 矩阵快速幂) 点我挑战题目 题意分析 直接求矩阵A^K的结果,然后计算正对角线,即左上到右下对角线的和,结果模9973后输出即可. 由于此题矩阵直接给出的,题目比较裸. ...

  2. hdu 3117 Fibonacci Numbers 矩阵快速幂+公式

    斐波那契数列后四位可以用快速幂取模(模10000)算出.前四位要用公式推 HDU 3117 Fibonacci Numbers(矩阵快速幂+公式) f(n)=(((1+√5)/2)^n+((1-√5) ...

  3. HDU 2842 (递推+矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2842 题目大意:棒子上套环.第i个环能拿下的条件是:第i-1个环在棒子上,前i-2个环不在棒子上.每个 ...

  4. hdu 2604 Queuing(矩阵快速幂乘法)

    Problem Description Queues and Priority Queues are data structures which are known to most computer ...

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

  6. 2013长春网赛1009 hdu 4767 Bell(矩阵快速幂+中国剩余定理)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4767 题意:求集合{1, 2, 3, ..., n}有多少种划分情况bell[n],最后结果bell[ ...

  7. HDU 6470 Count 【矩阵快速幂】(广东工业大学第十四届程序设计竞赛 )

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6470 Count Time Limit: 6000/3000 MS (Java/Others)    ...

  8. HDU 6395 Sequence 【矩阵快速幂 && 暴力】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6395 Sequence Time Limit: 4000/2000 MS (Java/Others)   ...

  9. HDU 5667 Sequence【矩阵快速幂+费马小定理】

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5667 题意: Lcomyn 是个很厉害的选手,除了喜欢写17kb+的代码题,偶尔还会写数学题.他找到 ...

随机推荐

  1. Mycat调优启用useOffHeapForMerge报java.lang.NumberFormatException异常解决(附源码)

    come from : https://blog.csdn.net/u013716179/article/details/89886452

  2. suse11sp3、suse12 安装 zabbix-agent

    1.添加repo源 suse11SP3zypper addrepo http://download.opensuse.org/repositories/server:/monitoring/SLE_1 ...

  3. Ansible命令行方式执行

    Ansible ad-hoc 什么是ad-hoc? 临时命令,执行完不会保存,类似于批量执行命令. ansible的选项 -i # 指定主机清单 ansible rsync -m ping -i 1. ...

  4. linux 解压总结

    tar解压 gz解压 bz2等各种解压文件使用方法 .tar 解包:tar xvf FileName.tar 打包:tar cvf FileName.tar DirName (注:tar是打包,不是压 ...

  5. 逗号字符的使用、字符数组与字符串数组、sizeof与strlen

    (1)连接两个表达式为一个表达式 for(ux=0,uxt=1;uxt<444;ux++,uxt++) 允许通过编译:他可以给FOR循环更多的初始化值: (2)一般定义的话要区别只有 字符数组 ...

  6. 纯C++代码实现将像素矩阵保存为bmp图片

    由于工作需要,时常需要将像素矩阵保存图片显示观看.为此,特地总结了三种使用纯C++代码生成bmp图片的方法.分别是使用自定义数据.从外界导入的txt和csv以及从图片中导入的数据. 1.使用自定义数据 ...

  7. CVPR2020论文解读:CNN合成的图片鉴别

    CVPR2020论文解读:CNN合成的图片鉴别 <CNN-generated images are surprisingly easy to spot... for now> 论文链接:h ...

  8. tensorflow-yolov4实施方法

    tensorflow-yolov4实施方法 tensorflow-yolov4-tflite YOLOv4: Optimal Speed and Accuracy of Object Detectio ...

  9. java后端知识点梳理——java集合

    集合概览 Java中的集合,从上层接口上看分为了两类,Map和Collection.Map是和Collection并列的集合上层接口,没有继承关系. Java中的常见集合可以概括如下. Map接口和C ...

  10. 【NX二次开发】多种变换

    变换的种类: uf5942 矩阵乘积变换 uf5943 平移变换 uf5944 缩放变换 uf5945 旋转变换 uf5946 镜像变换 最后使用 uf5947 实现uf5942-uf5946的变换. ...