HDU 2604 Queuing,矩阵高速幂
2024-10-01 04:18:17
题目地址:HDU 2604 Queuing
题意:
略
分析:
易推出: f(n)=f(n-1)+f(n-3)+f(n-4)
构造一个矩阵:
然后直接上板子:
/*
f[i] = f[i-1] + f[i-3] + f[i-4] */ #include<cstdio>
#include<cstring> using namespace std; const int N = 4;
int L, M; struct mtx
{
int x[N+1][N+1];
mtx(){
memset(x, 0, sizeof x );
}
}; mtx operator *(const mtx &a, const mtx &b){
mtx c;
for(int i=0; i<N; ++i)
{
for(int j=0; j<N; ++j)
{
for(int k=0; k<N; ++k)
{
c.x[i][j] = (c.x[i][j] + a.x[i][k]*b.x[k][j]) % M;
}
}
}
return c;
} mtx operator ^(mtx a, int n)
{
mtx res;
for(int i=0; i<N; ++i) res.x[i][i] = 1;
for(; n; n>>=1)
{
if(n&1) res = res * a;
a = a * a;
}
return res;
} int f[N+1];
mtx I; void init()
{
f[1] = 2;
f[2] = 4;
f[3] = 6;
f[4] = 9;
I.x[0][0] = I.x[0][2] = I.x[0][3] = I.x[1][0]
= I.x[2][1] = I.x[3][2] = 1;
} void work()
{
if(L<=4){
printf("%d\n", f[L] % M);
return ;
}
mtx res = I^(L-4);
int F_n = 0;
for(int i=0; i<N; ++i)
{
F_n = (F_n + res.x[0][i]*f[N-i] ) % M;
}
printf("%d\n", F_n);
}
int main()
{
init();
while(~scanf("%d%d", &L, &M))
{
work();
}
return 0;
}
最新文章
- C#组件系列——又一款Excel处理神器Spire.XLS,你值得拥有(二)
- CozyRSS开发记录4-抽屉效果订阅列表栏
- 记录视频“ Why I build Docker";
- 在竞赛ACM Java处理输入输出
- table_横向合并_纵向合并
- 后台数据导出为Excel
- Quartz(任务调度)- job串行避免死锁
- C++指向常量的指针和常指针
- 中高级JavaScript易错面试题
- 全文检索Lucene (2)
- 手机广告投放(phone advertising)唯一标识
- TensorFlow 常用函数汇总
- go语言 godep save 报错 is not using a known version control system
- Linux_Ubuntu_C++编程_如何完成一个C++编写,调试,运行。
- web专业课学习及往后方向发展
- CGAffineTransform的使用
- 编译安装openssl
- curl工具介绍和常用命令
- OpenCV——掩膜(又称掩码)mask的原理和作用
- Cxfreeze使用存在问题