您的位置:首页 > 产品设计 > UI/UE

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: