HDoj 2604 queuing
生活随笔
收集整理的這篇文章主要介紹了
HDoj 2604 queuing
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
| 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. ??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. |
| 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????mf?mm? fm? ff
2???? 1??? 1??? 1? ?1?
3???? 1??? 2??? 2?? 1
令mf、mm、fm、ff分別為a,b,c,d;
可以看出a(n)=b(n-1); b(n)=b(n-1)+c(n-1); c(n)=a(n-1)+d(n-1);d(n)=a(n-1);
由于題目限制簡單地遞推會T,不能滿足要求
這里把abcd看作一個矩陣
b? c?? *?? 1? 1??? =?? b+c(b')??? b(a')
a??d? 1??0???? a+d(c')??? a(d')
1??1 * b? a = b+c(b')?? a+d(c')
1? 0 ? c? d b(a') a(d')
通過右乘、左乘(1,1,1,0)矩陣;達到遞推的效果
然后用二分快速冪的方法解題
代碼如下:
?
#include<stdio.h> #define L(i) for(int i=0;i<2;i++) int m; void multip3(int t[2][2]){int x[2][2];L(i) L(j){x[i][j]=0;L(k) x[i][j]+=(t[i][k]*t[k][j])%m;x[i][j]=x[i][j]%m;}L(i)L(j) t[i][j]=x[i][j]; } void mulleft(int a[2][2],int t[2][2]){int x[2][2];x[0][0]=a[0][0]; x[0][1]=a[1][0];x[1][0]=a[0][1]; x[1][1]=a[1][1];L(i) L(j){a[i][j]=0;L(k)a[i][j]+=(x[j][k]*t[k][i])%m;a[i][j]=a[i][j]%m;} } void mulright(int a[2][2],int t[2][2]){int x[2][2];x[0][0]=a[0][0]; x[0][1]=a[1][0];x[1][0]=a[0][1]; x[1][1]=a[1][1];L(i)L(j){a[i][j]=0;L(k)a[i][j]+=(x[i][k]*t[k][j])%m;a[i][j]=a[i][j]%m;} }int z[30][2][2]; int main(){int L;while(scanf("%d%d",&L,&m)!=EOF){int x=0;if(L==0) printf("0\n");else if(L==1) printf("%d\n",2%m);else if(L==2) printf("%d\n",4%m);else{L-=2;int xxx=0;if(L%2==1) {xxx=1;}L/=2;int t[2][2]={1,1,1,0};for(int i=0;i<=25;i++){L(j)L(k) z[i][j][k]=t[j][k];multip3(t);}int k=0,xx=1;int x1[2][2]={1,1,1,1};if(xxx){x1[0][0]=2;x1[0][1]=1;x1[1][0]=2;x1[1][1]=1;}while(L!=0){if(L%2==1) {mulright(x1,z[k]);mulleft(x1,z[k]);}L/=2;k++;}xx=(x1[0][0]+x1[1][0]+x1[0][1]+x1[1][1])%m;printf("%d\n",xx);}}return 0; }第一次寫這種題目
很開心~~~
轉載于:https://www.cnblogs.com/liujiaqi-Boke/p/5475539.html
總結
以上是生活随笔為你收集整理的HDoj 2604 queuing的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 屏幕尺寸,分辨率,ppi换算分析
- 下一篇: DPI、PPI、DP、PX 的详细计算方