hdu 2604 Queuing
2016-05-31 16:33
483 查看
Queuing
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4387 Accepted Submission(s):
1936
[align=left]Problem Description[/align]
Queues and Priority Queues are data structures which
are known to most computer scientists. The Queue occurs often in our daily life.
There are many people lined up at the lunch time.
Now we define that ‘f’ is short for female and ‘m’ is short for
male. If the queue’s length is L, then there are 2L numbers of
queues. For example, if L = 2, then they are ff, mm, fm, mf . If there exists a
subqueue as fmf or fff, we call it O-queue else it is a E-queue.
Your task is
to calculate the number of E-queues mod M with length L by writing a
program.
[align=left]Input[/align]
Input a length L (0 <= L <= 10 6) and
M.
[align=left]Output[/align]
Output K mod M(1 <= M <= 30) where K is the
number of E-queues with length L.
[align=left]Sample Input[/align]
3 8
4 7
4 8
[align=left]Sample Output[/align]
6
2
1
[align=left]Author[/align]
WhereIsHeroFrom
[align=left]Source[/align]
HDU
1st “Vegetable-Birds Cup” Programming Open Contest
[align=left]Recommend[/align]
lcy | We have carefully selected several similar
problems for you: 1588 2606 2276 2603 3117
递推后使用矩阵快速幂求解。
递推的规律:
1 根据题目的意思,我们可以求出F[0] = 0 , F[1] = 2 , F[2] = 4 , F[3] = 6 , F[4] = 9 , F[5] = 15
那么我们就可以构造出矩阵
| 0 1 0 0 | | F[n-4] | | F[n-3] |
| 0 0 1 0 | * | F[n-3] | = | F[n-2] |
| 0 0 0 1 | | F[n-2] | | F[n-1] |
| 1 1 0 1 | | F[n-1] | | F
|
从第五个开始。
题意:一个由f和m组成的串,求不存在fff和fmf的串的个数。
附上代码:
#include <iostream> #include <cstdio> #include <cstring> using namespace std; struct mat { int m[5][5]; }; int mod,n; mat mul(mat a,mat b) { mat c; int i,j,k; memset(c.m,0,sizeof(c.m)); for(i=0; i<4; i++) for(j=0; j<4; j++) { for(k=0; k<4; k++) c.m[i][j]+=(a.m[i][k]*b.m[k][j])%mod; c.m[i][j]%=mod; } return c; } mat product(mat a,int k) { if(k==1) return a; else if(k&1) return mul(product(a,k-1),a); else return product(mul(a,a),k/2); } int main() { int i,j,k; mat a,b; int x[5]= {0,2,4,6,9}; while(~scanf("%d%d",&n,&mod)) { if(n<5) { printf("%d\n",x %mod); continue; } memset(a.m,0,sizeof(a.m)); a.m[0][1]=1; ///构建矩阵 a.m[1][2]=1; a.m[2][3]=1; a.m[3][0]=1; a.m[3][1]=1; a.m[3][3]=1; b=product(a,n-4); int ans=0; for(i=0; i<4; i++) ans+=(b.m[3][i]*x[i+1])%mod; printf("%d\n",ans%mod); } return 0; }
相关文章推荐
- ERROR ITMS-9000: “Redundant Binary Upload. There already exists a binary upload with build version '
- Middle-题目61:63. Unique Paths II
- 02-线性结构3 Pop Sequence
- UI 如何制作配色色卡
- iOS开发之--UITextField属性
- 用编程的方式开发UI界面
- 控件类——UIControl(其子类、属性)
- PxCook像素大厨,UI设计师标注切图的利器
- UITextField —— 键盘弹出视图上移
- java 通过request获取ip 在通过ip获取地址
- django不要设置datetime字段auto_now=True
- 控件类——Button、UIControlState状态、title及其属性
- Middle-题目44:334. Increasing Triplet Subsequence
- 线程加载数据后的ui线程刷新实现
- HBuilder工作空间修改
- Java LinkedBlockingQueue 分析
- Property List Programming Guide(一)
- Middle-题目33:300. Longest Increasing Subsequence
- 利用spt_values求2个时间段内所有的日期。主要用于关联数据。
- LXC quick start