HDU 2604 Queuing
2015-07-30 18:43
351 查看
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.
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 L 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.
Input
Input a length L (0 <= L <= 10 6) and M.
Output
Output K mod M(1 <= M <= 30) where K is the number of E-queues with length L.
Sample Input
3 8
4 7
4 8
Sample Output
6
2
1
题意:L个人排队,f为男生,m为女生,队伍中没有fff和fmf的队伍有多少种。
这是递推题,由于数据比较大,所以要先找到递推公式,再用矩阵快速幂做。
ff[i]表示队伍长度为i时,队伍第i个为f,第i-1个也为f。fm[i],mf[i],mm[i]就不解释了。ff[i]=mf[i-1],因为在mf[i-1]后面加f就是mff。ff[i]!=ff[i-1].因为在f[i-1后面加f就是fff,不合法。同理fm[i]=ff[i-1]+mf[i-1],mf[i]=mm[i-1],mm[i]=fm[i-1]+mm[i-1];下面构造矩阵
0 1 0 0
(ff[i-1] fm[i-1] mf[i-1] mm[i-1] * 0 0 0 1 =(ff[i] fm[i] mf[i] mm[i])
1 1 0 0
0 0 1 1
还有一种关系是a[i]=a[i-1]+a[i-3]+a[i-4],a[i]表示长度为i时合法的种数,为什么这样,我就懒得推了。
第一种
第二种
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 2 L 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.
Input
Input a length L (0 <= L <= 10 6) and M.
Output
Output K mod M(1 <= M <= 30) where K is the number of E-queues with length L.
Sample Input
3 8
4 7
4 8
Sample Output
6
2
1
题意:L个人排队,f为男生,m为女生,队伍中没有fff和fmf的队伍有多少种。
这是递推题,由于数据比较大,所以要先找到递推公式,再用矩阵快速幂做。
ff[i]表示队伍长度为i时,队伍第i个为f,第i-1个也为f。fm[i],mf[i],mm[i]就不解释了。ff[i]=mf[i-1],因为在mf[i-1]后面加f就是mff。ff[i]!=ff[i-1].因为在f[i-1后面加f就是fff,不合法。同理fm[i]=ff[i-1]+mf[i-1],mf[i]=mm[i-1],mm[i]=fm[i-1]+mm[i-1];下面构造矩阵
0 1 0 0
(ff[i-1] fm[i-1] mf[i-1] mm[i-1] * 0 0 0 1 =(ff[i] fm[i] mf[i] mm[i])
1 1 0 0
0 0 1 1
还有一种关系是a[i]=a[i-1]+a[i-3]+a[i-4],a[i]表示长度为i时合法的种数,为什么这样,我就懒得推了。
第一种
#include<cstdio> #include<cstring> using namespace std; int m; int f(int x) { int s1[5][5],s2[5][5],s3[5][5],i,a=0,j,k,b=0,c=0,d=0; memset(s1,0,sizeof(s1)); memset(s2,0,sizeof(s2)); s1[0][1]=1;s2[0][1]=1; s1[1][3]=1;s2[1][3]=1; s1[2][0]=1;s2[2][0]=1; s1[2][1]=1;s2[2][1]=1; s1[3][2]=1;s2[3][2]=1; s1[3][3]=1;s2[3][3]=1; while (x) { if (x&1) { memset(s3,0,sizeof(s3)); for (i=0;i<4;i++) for (j=0;j<4;j++) for (k=0;k<4;k++) s3[i][j]+=s1[i][k]*s2[k][j]; for (i=0;i<4;i++) for (j=0;j<4;j++) s2[i][j]=s3[i][j]%m; } memset(s3,0,sizeof(s3)); for (i=0;i<4;i++) for (j=0;j<4;j++) for (k=0;k<4;k++) s3[i][j]+=s1[i][k]*s1[k][j]; for (i=0;i<4;i++) for (j=0;j<4;j++) s1[i][j]=s3[i][j]%m; x>>=1; } for (i=0;i<4;i++) { a+=s2[i][0]; b+=s2[i][1]; c+=s2[i][2]; d+=s2[i][3]; } return a+b+c+d; } int main() { int l,p; while (~scanf("%d%d",&l,&m)) { if (l==0) {printf("0\n");continue;} if (l==1) {printf("%d\n",2%m);continue;} if (l==2) {printf("%d\n",4%m);continue;} l-=3; p=f(l)%m; printf("%d\n",p); } return 0; }
第二种
#include<cstdio> #include<cstring> using namespace std; int m,a[10]; int s1[5][5],s2[5][5],s3[5][5]; int f(int x) { int i,j,k; x-=4; memset(s1,0,sizeof(s1)); s1[0][3]=1; s1[1][0]=1; s1[1][3]=1; s1[2][1]=1; s1[3][2]=1; s1[3][3]=1; for (i=0;i<4;i++) for (j=0;j<4;j++) { if (i==j) s2[i][j]=1; else s2[i][j]=0; } while (x) { if (x&1) { memset(s3,0,sizeof(s3)); for (i=0;i<4;i++) for (j=0;j<4;j++) for (k=0;k<4;k++) s3[i][j]+=(s1[i][k]*s2[k][j])%m; for (i=0;i<4;i++) for (j=0;j<4;j++) s2[i][j]=s3[i][j]%m; } memset(s3,0,sizeof(s3)); for (i=0;i<4;i++) for (j=0;j<4;j++) for (k=0;k<4;k++) s3[i][j]+=(s1[i][k]*s1[k][j])%m; for (i=0;i<4;i++) for (j=0;j<4;j++) s1[i][j]=s3[i][j]%m; x>>=1; } return a[1]*s2[0][3]+a[2]*s2[1][3]+s2[2][3]*a[3]+a[4]*s2[3][3]; } int main() { int l,p; while (~scanf("%d%d",&l,&m)) { a[1]=2;a[2]=4;a[3]=6;a[4]=9; if (l<=4) {printf("%d\n",a[l]%m);continue;} p=f(l); printf("%d\n",p%m); } return 0; }
相关文章推荐
- 【读书笔记】UIFont-动态下载系统提供的多种中文字体网址
- 【读书笔记】UIFont-动态下载系统提供的多种中文字体网址
- ActiveAndroid Guide
- iOS UILabel镂空特效
- iOS UILabel镂空特效
- iOS UILabel镂空特效
- Android BlueTooth通信
- MUI - 解决bug: iphone页面保存时popPicker自动弹出来
- ERROR 1366 (HY000): Incorrect string value: '\xE4\xBD\xA0\xE5\xA5\xBD' for column 'name' at row 1
- @RequestParam @RequestBody @PathVariable
- iOS学习笔记2-使用Audio Queues录音,取得实时PCM数据
- UITableView 实现类似btn单选功能
- Selenium2学习-019-WebUI自动化实战实例-017-获取浏览器类型
- ui automation viewer 工具
- 解析iptables中SNAT和MASQUERADE之间的区别
- navigationItem中加入UISegmentedControl
- IOS开发 UITableView empty 简单实用
- the constructor AlertDialog.Builder(new View.OnClickListener() ) is undefined
- pcDuino使用问题总结
- UIViewController的生命周期及iOS程序执行顺序