递推,推得f(n) = f(n-1) + f(n-3) + f(n-4)。然后转换成矩阵相乘,如下
f(n-1)  f(n-2)  f(n-3)  f(n-4)   *    1   1   0   0   =  f(n)   f(n-1)  f(n-2)  f(n-3)
0         0        0         0               0   0   1   0       0         0        0         0
0         0        0         0               1   0   0   1       0         0        0         0
0         0        0         0               1   0   0   0       0         0        0         0
从而转化为快速矩阵相乘。

 #include <cstdio>
#include <cstring>
#include <cstdlib> #define MATN 4 typedef struct mat_st {
int m[MATN][MATN];
mat_st() {
memset(m, , sizeof(m));
}
} mat_st; int ans[MATN]={,,,};
int mod; mat_st mult_mat(mat_st a, mat_st b) {
mat_st ret;
int i, j, k; for (i=; i<MATN; ++i) {
for (j=; j<MATN; ++j) {
for (k=; k<MATN; ++k)
ret.m[i][j] += a.m[i][k]*b.m[k][j];
ret.m[i][j] %= mod;
}
}
return ret;
} mat_st pow_mat(mat_st a, int n) {
mat_st ret;
int i; for (i=; i<MATN; ++i)
ret.m[i][i] = ; while (n) {
if (n & )
ret = mult_mat(ret, a);
a = mult_mat(a, a);
n >>= ;
}
return ret;
} int main() {
int l;
int i;
mat_st e, a; e.m[][] = e.m[][] = e.m[][] = e.m[][] = e.m[][] = e.m[][] = ;
for (i=; i<MATN; ++i)
a.m[][i] = ans[MATN--i]; while (scanf("%d %d", &l, &mod) != EOF) {
if (l <= )
printf("%d\n", ans[l]%mod);
else {
mat_st c = pow_mat(e, l-);
mat_st r = mult_mat(a, c);
printf("%d\n", r.m[][]);
#ifndef ONLINE_JUDGE
fflush(stdout);
#endif
}
} return ;
}

【HDOJ】2604 Queuing的更多相关文章

  1. 【HDOJ】4729 An Easy Problem for Elfness

    其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...

  2. 【HDOJ】3337 Guess the number

    神一样的题目.简言之,利用手段获得测试用例的第一行,输出结果.很显然利用wa, TLE, OLE等judge status可以获得测试用例.因此,果断Python写一个acm提交机器人.依赖lxml库 ...

  3. 【HDOJ】【3506】Monkey Party

    DP/四边形不等式 裸题环形石子合并…… 拆环为链即可 //HDOJ 3506 #include<cmath> #include<vector> #include<cst ...

  4. 【HDOJ】【3516】Tree Construction

    DP/四边形不等式 这题跟石子合并有点像…… dp[i][j]为将第 i 个点开始的 j 个点合并的最小代价. 易知有 dp[i][j]=min{dp[i][j] , dp[i][k-i+1]+dp[ ...

  5. 【HDOJ】【3480】Division

    DP/四边形不等式 要求将一个可重集S分成M个子集,求子集的极差的平方和最小是多少…… 首先我们先将这N个数排序,容易想到每个自己都对应着这个有序数组中的一段……而不会是互相穿插着= =因为交换一下明 ...

  6. 【HDOJ】【2829】Lawrence

    DP/四边形不等式 做过POJ 1739 邮局那道题后就很容易写出动规方程: dp[i][j]=min{dp[i-1][k]+w[k+1][j]}(表示前 j 个点分成 i 块的最小代价) $w(l, ...

  7. 【HDOJ】【3415】Max Sum of Max-K-sub-sequence

    DP/单调队列优化 呃……环形链求最大k子段和. 首先拆环为链求前缀和…… 然后单调队列吧<_<,裸题没啥好说的…… WA:为毛手写队列就会挂,必须用STL的deque?(写挂自己弱……s ...

  8. 【HDOJ】【3530】Subsequence

    DP/单调队列优化 题解:http://www.cnblogs.com/yymore/archive/2011/06/22/2087553.html 引用: 首先我们要明确几件事情 1.假设我们现在知 ...

  9. 【HDOJ】【3068】最长回文

    Manacher算法 Manacher模板题…… //HDOJ 3068 #include<cstdio> #include<cstring> #include<cstd ...

随机推荐

  1. [Angular 2] Controlling how Styles are Shared with View Encapsulation

    Style and View Encapsulation is best understood by seeing how each option (Emulated, Native, and Non ...

  2. Android滚动截屏,ScrollView截屏

    在做分享功能的时候,需要截取全屏内容,一屏展示不完的内容,一般我们会用到 ListView 或 ScrollView 一: 普通截屏的实现 获取当前Window 的 DrawingCache 的方式, ...

  3. java中-静态代码块、构造代码块、构造方法的联系

    例如该题: 1 class Fu{ static { System.out.println("这是父类静态代码块"); } { System.out.println("这 ...

  4. haproxy主配置文件

    1.haproxy 配置文件 ------------------------------------------------------------------------------------- ...

  5. (转)PHP自定义遍历目录下所有文件dir(),readdir()函数

    方法一:使用dir()遍历目录 dir()函数,成功时返回Directory类实例 PHP dir() 语法格式为: dir(directory);//directory为需要显示文件名的目录名称,可 ...

  6. Ajax简单应用-购物车

    1. 2. 3. 4. 5. 6.

  7. 最全ASCLL码

    结果 描述 实体编号   space ! exclamation mark ! " quotation mark " # number sign # $ dollar sign $ ...

  8. c#读取通达信历史数据的方法

    public Bar ReadBarMin(BinaryReader br, int instrumentId, long size) { int date = br.ReadUInt16(); in ...

  9. JavaScript--函数-按值传递

    按值传递(byValue): 两个变量间赋值或将变量作为参数传入函数时,其实都是将原变量中的值,赋值一份给对方(新变量) 对原始类型的值: 修改新变量,不会影响原变量 对引用类型的对象: 通过新变量修 ...

  10. 最短路径算法——Dijkstra,Bellman-Ford,Floyd-Warshall,Johnson

    根据DSqiu的blog整理出来 :http://dsqiu.iteye.com/blog/1689163 PS:模板是自己写的,如有错误欢迎指出~ 本文内容框架: §1 Dijkstra算法 §2 ...