SDAU 课程练习3 1009
来源:互联网 发布:淘宝 售后介入 编辑:程序博客网 时间:2024/06/01 21:50
Problem I
Time Limit : 10000/5000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 6 Accepted Submission(s) : 1
Problem Description
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. <br><center><img src=../../../data/images/C154-1005-1.jpg> </center><br> 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 2<sup>L</sup> 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.<br>Your task is to calculate the number of E-queues mod M with length L by writing a program.<br>
Input
Input a length L (0 <= L <= 10 <sup>6</sup>) and M.
Output
Output K mod M(1 <= M <= 30) where K is the number of E-queues with length L.
Sample Input
3 84 74 8
Sample Output
621
题目大意:
求出所有的字符全排列,排除 含有 fff 和 fmf 的,用剩下的个数 模 m 就是最后的结果。
思路:
关于递推公式是怎么得到的,希望同学们能想一下。
f (n) = f(n-1) + f (n-3)+ f (n-4)
其实就是如果最后一个字母是 m 那么前面的所有都满足喽。
如果是 f 那么再往前考虑,mmf fmf mff fff 只有两个满足条件。如果是 mmf 的话,都成立,所以加上 f (n-3) 如果是 mff 满足的只有 mmff 所以加上 f (n-4)
然后那?如果用循环配合数组进行打表的话!!那是时间会爆掉的。因为他是 10 ^6 所以你的复杂度不能是 n^2
如果用 for (i=0;i< 10^6; i++)
f (n) = f(n-1) + f (n-3)+ f (n-4)
显然不仅超时还会溢出的对吗?
矩阵连续次幂可以快速处理递推,这个是个新知识。
感想:
今天搞了一个斐波那契的递推,但是wa 问了鹏哥哥,他说数太大,不可以用矩阵处理。
AC代码:
#include <cstdlib>#include <cstring>#include <cstdio>#include <iostream>using namespace std;int N,m;struct matrix{ int a[4][4];}origin,res;matrix multiply(matrix x,matrix y){ matrix temp; memset(temp.a,0,sizeof(temp.a)); for(int i=0;i<4;i++) { for(int j=0;j<4;j++) { for(int k=0;k<4;k++) { temp.a[i][j]+=(x.a[i][k]*y.a[k][j])%m; temp.a[i][j]%=m; } } } return temp;}void calc(int n){ while(n) { if(n&1) res=multiply(res,origin); origin=multiply(origin,origin); n>>=1; }}int main(){ int ff[5]={9,6,4,2}; int i,j; matrix aaa; //freopen("r.txt","r",stdin); while(~scanf("%d%d",&N,&m)) { if(N==0) { cout<<0<<endl; continue; } if(N<=4) { cout<<ff[4-N]%m<<endl; continue; } int doudou[5]={0}; memset(origin.a,0,sizeof(origin.a)); memset(aaa.a,0,sizeof(aaa.a)); aaa.a[0][0]=9; aaa.a[1][0]=6; aaa.a[2][0]=4; aaa.a[3][0]=2; origin.a[0][0]=origin.a[0][2]=origin.a[0][3]=origin.a[1][0]=origin.a[2][1]=origin.a[3][2]=1; res.a[0][0]=res.a[1][1]=res.a[2][2]=res.a[3][3]=1; calc(N-4); res=multiply(res,aaa); cout<<res.a[0][0]%m<<endl; } return 0;}
0 0
- SDAU 课程练习3 1009
- SDAU课程练习1009
- SDAU课程练习2 1009
- SDAU 课程练习3 1001
- SDAU 课程练习3 1002
- SDAU 课程练习3 1003
- SDAU 课程练习3 1004
- SDAU 课程练习3 1005
- SDAU 课程练习3 1008
- SDAU 课程练习3 1014
- SDAU 课程练习3 1015
- SDAU 课程练习3 1016
- SDAU 课程练习3 1017
- SDAU 课程练习3 1018
- SDAU 课程练习3 1019
- SDAU 课程练习3 1020
- SDAU 课程练习3 1021
- SDAU 课程练习3 1023
- The size of the object heap + VM data exceeds the maximum representable size问题解决办法
- facebook开放的源码,框架。
- 最新版安装cocoaPods
- 20160422_Eclipse导入依赖包出错
- SQL的左外连接、右外连接、内连接、全连接
- SDAU 课程练习3 1009
- Android studio 下的svn配置使用
- 不要在意别人的目光,走自己的路
- 程序运行流程——链接、装载及执行
- ComboBox [开放源代码]
- 如何配置react native方法1
- spring mvc对异步请求的处理
- 【POJ2887】【块状链表】Big String
- MAC APP开发笔记