题目地址: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;
}

最新文章

  1. C#组件系列——又一款Excel处理神器Spire.XLS,你值得拥有(二)
  2. CozyRSS开发记录4-抽屉效果订阅列表栏
  3. 记录视频“ Why I build Docker&quot;
  4. 在竞赛ACM Java处理输入输出
  5. table_横向合并_纵向合并
  6. 后台数据导出为Excel
  7. Quartz(任务调度)- job串行避免死锁
  8. C++指向常量的指针和常指针
  9. 中高级JavaScript易错面试题
  10. 全文检索Lucene (2)
  11. 手机广告投放(phone advertising)唯一标识
  12. TensorFlow 常用函数汇总
  13. go语言 godep save 报错 is not using a known version control system
  14. Linux_Ubuntu_C++编程_如何完成一个C++编写,调试,运行。
  15. web专业课学习及往后方向发展
  16. CGAffineTransform的使用
  17. 编译安装openssl
  18. curl工具介绍和常用命令
  19. OpenCV——掩膜(又称掩码)mask的原理和作用
  20. Cxfreeze使用存在问题

热门文章

  1. Djnago进阶
  2. Spring Boot (20) 拦截器
  3. SpringMVC参数绑定(二)
  4. matplotlib之pyplot 学习示例
  5. mysql 导入数据库时,报错1840的解决方法
  6. SAP computer之input and MAR
  7. Linux学习自动化脚本(一)
  8. Matlab/Eigen矩阵填充问题
  9. Windows Phone 应用程序的生命周期(二)
  10. 【sqli-labs】 less9 GET - Blind - Time based. - Single Quotes (基于时间的GET单引号盲注)