Queuing(矩阵快速幂(递推and模板))
来源:互联网 发布:爱奇艺格式转换器 mac 编辑:程序博客网 时间:2024/04/28 04:58
【题目来源】:https://vjudge.net/problem/HDU-2604
【题意】
f,m分别是female与male的缩写,假设有一个队列里面是这些字母缩写,长度为L,那么共有2^L种,如果含有fmf或者fff这种子队列的队列被称为0队列,其余的被称为E队列,问,长度为k的有多少种E队列。
【思路】
先推出前几项,得到:
f[0]=0,f[1]=2,f[2]=4,f[3]=6,f[4]=9,f[5]=15,f[6]=25;
故得到关系式:f[x]=f[x-1]+f[x-3]+f[x-4]。
其中x>=5.
所以(矩阵1)^(k-4)*矩阵2=矩阵3
我们要求矩阵3,首先要构造矩阵1和矩阵2。
那么怎么构造呢?
首先,构造矩阵1:
在f[x]=f[x-1]+f[x-3]+f[x-4]式子里:(一般递推的矩阵2都是由关系式得来的)
f[x]=f[x-1]++0*f[x-2]+f[x-3]+f[x-4]。
f[x-1]=1*f[x-1];
f[x-2]=1*f[x-2];
f[x-3]=1*f[x-3];
f[x-4]=1*f[x-4];
得到矩阵:
1 0 1 1
1 0 0 0
0 1 0 0
0 0 1 0
接着就是构造矩阵2:
f[x-1] 0 0 0
f[x-2] 0 0 0
f[x-3] 0 0 0
f[x-4] 0 0 0
……
没啦。。。质疑可以发评论讨论一下0.0 交流0.0
【代码】
#include<set>#include<map>#include<stack>#include<cmath>#include<queue>#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<limits.h>#include<algorithm>#define mem(a,b) memset(a,b,sizeof(a))using namespace std;const int mod=9973;typedef unsigned long long ll;typedef long long LL;int k,m;struct mat{ int a[5][5]; mat() { mem(a,0); for(int i=1; i<=4; i++) a[i][i]=1; }};mat operator*(mat s,mat t){ mat r; mem(r.a,0); for(int i=1; i<=4; i++) { for(int j=1; j<=4; j++) { for(int p=1; p<=4; p++) { r.a[i][j]=r.a[i][j]+s.a[i][p]*t.a[p][j]; if(r.a[i][j]>=m) r.a[i][j]%=m; } } } return r;}void print(mat t){ for(int i=1; i<=4; i++) { for(int j=1; j<=4; j++) { printf("%d ",t.a[i][j]); } printf("\n"); }}mat pow_mat(mat &ans){ mat base,temp; mem(base.a,0); base.a[1][1]=1; base.a[1][3]=1; base.a[1][4]=1; for(int i=2; i<=4; i++) base.a[i][i-1]=1; k-=4; while(k) { if(k&1) temp=temp*base; base=base*base; k>>=1; }// print(temp); ans=temp*ans;}int main(){ while(~scanf("%d%d",&k,&m)) { if(k<5) { int p=0; switch(k) { case 0: p=0; break; case 1: p=2; break; case 2: p=4; break; case 3: p=6; break; case 4: p=9; break; } printf("%d\n",p%m); } else { mat ans; mem(ans.a,0); ans.a[1][1]=9; ans.a[2][1]=6; ans.a[3][1]=4; ans.a[4][1]=2; pow_mat(ans); printf("%d\n",(ans.a[1][1])%m); } }}
阅读全文
0 0
- Queuing(矩阵快速幂(递推and模板))
- HDOJ 2604 Queuing (递推+矩阵快速幂)
- HDU2604-Queuing(递推+矩阵快速幂)
- 【递推+矩阵快速幂】【HDU2604】【Queuing】
- Queuing(矩阵快速幂)
- 【HDU】2604 - Queuing(递推 & 思维 & 矩阵构造 & 快速幂)
- HDU 2604-Queuing(递推+矩阵快速幂)
- HDU 2604 Queuing (递推+矩阵快速幂)
- hdoj 4062 Queuing 【矩阵快速幂优化递推公式】
- hdu 2604 Queuing 递推+矩阵快速幂
- hdu 2604 Queuing【递推+矩阵快速幂】
- HDU 2604 Queuing [递推]【矩阵快速幂】
- 【hdu 4062】Queuing 【思维+递推+矩阵快速幂】
- HDU 2604 Queuing (递推+ 矩阵快速幂)
- HDU - 2604 Queuing (矩阵快速幂)
- HDU 2604 Queuing (矩阵快速幂)
- HDU 2604 Queuing(矩阵快速幂)
- hdu 2604 Queuing (矩阵快速幂)
- Openwrt 實戰
- 1019: 公园门票
- java入门学习:多线程创建-Thread,Runnable,callable和threadpool
- 使用jQuery 快速高效制作 网页特效
- React native 分辨率适配
- Queuing(矩阵快速幂(递推and模板))
- Android如何判断网络状态是否良好
- opencv3_java 图形图像的高斯模糊GaussianBlur GaussianBlur
- Spring Boot 使用 FreeMarker 渲染页面
- 1020: 两整数排序
- TCP建立可靠性连接的介绍
- 1021: 三个整数的最大值
- 第一部分 线程APIs(Thread APIs)线程和运行(Threads and Runnables)
- QiYuAdmin-BootStrapTable增加通用的搜索功能(Metronic实战)